The object-oriented (anonymous inner class) of the first season of getting Started with Java

Source: Internet
Author: User

1. Understanding anonymous Inner class/* Anonymous inner class is a simplified way of writing inner classes. Premise: There is a class or interface where the class can be either a concrete class or an abstract class. Anonymous inner class format: New class name or interface name () {overriding method;} This represents a subclass of what is the nature of the object? is an anonymous object that inherits the class or implements the interface's subclass. */interface Inter {public abstract void Show ();p ublic abstract void Show2 ();} Class Outer {public void method () {//The interface has only one method in it, only show ()/*new Inter () {public void Show () {////The method of overriding an interface or abstract class inside an anonymous inner class. System.out.println ("Show");}}. Show (); *///two methods when/*new Inter () {public void Show () {System.out.println ("show");} public void Show2 () {System.out.println ("Show2");}}. Show (); new Inter () {public void Show () {System.out.println ("show");} public void Show2 () {System.out.println ("Show2");}}. Show2 (); *///if there are many ways, it will be very troublesome. So choose to use parameters to receive this object, and then use the object to invoke the method on it. Inter i = new Inter () {//Interface implementation subclass object to interface, polymorphic public void Show () {System.out.println ("show");} public void Show2 () {System.out.println ("Show2");}};/ /note here the semicolon i.show (); I.show2 ();}} Class InnerClassDemo6 {public static void main (string[] args) {Outer o = new Outer (); O.method ();}} 2, anonymous internal class in the development of the use of a case for a simple demonstration: interface person {   &nbsP;public abstract Void Study ();}       Class Persondemo {    public void Method (person p) {//This way, as described earlier, when the parameter type is a reference type. See Blogs (http://blog.csdn.net/qq_32059827/article/details/51344046)         p.study ();     }}//Implementation Class class Student implements person {    public Void study () {    & nbsp;   system.out.println ("Good study, Day up");    }}class InnerClassTest2 {     public static void Main (string[] args) {        //test         persondemo PD = new Persondemo ();        person p = new Student ();         pd.method (P);        system.out.println ("--------------------");                 //use of anonymous internal classes in development        //The results are exactly the same as above, using anonymous inner classes to save memory space. Using scenarios, the method is used less often.         pd.method (New person () {            public void Study () {                 System.out.println ("Good study, Day up");            }         });    }}

Object-Oriented (anonymous inner class)

for the first season of getting Started with Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.