JAVA_SE basics-52. Anonymous internal class

Source: Internet
Author: User

JAVA_SE basics-52. Anonymous internal class

 

 

Java anonymous internal class summary:

 

Internal class with no name. Is the simplified form of internal classes. Generally, this form can be used only once. An anonymous internal class is actually an anonymous subclass object. To define an anonymous internal class: A prerequisite is that the internal class must inherit a class or implement an interface.

 

 

Example 1: Do not use anonymous internal classes to implement Abstract METHODS
package day10;abstract class Fu {    public abstract void run();} class Zi extends Fu {    public void run() {        System.out.println(run....);    }} public class AnonymousClass01 {    public static void main(String[] args) {        Zi p = new Zi();        p.run();    }}
Running result: run ....
We can see that we use the Zi class to inherit the Fu class and then implement an instance of Zi. If the Zi class is used only once, isn't it very troublesome to write it into an independent class?
At this time, an anonymous internal class is introduced.



Example 2: basic implementation of anonymous internal classes
package day10;abstract class Fu {    public abstract void run();} public class AnonymousClass01 {    public static void main(String[] args) {      new Fu(){      public void run(){      System.out.println(run....);      }      }.run();    }}
Running result: run ....

We can see that the methods in the abstract class Fu are implemented in braces so that the writing of a class can be omitted and anonymous internal classes can also be applied to interfaces.

Instance 3: Use an anonymous internal class on the Interface
package day10;interface Fu {    public abstract void run();} public class AnonymousClass01 {    public static void main(String[] args) {      new Fu(){      public void run(){      System.out.println(run....);      }      }.run();    }}
Running result: run ....

As shown in the preceding example, as long as a class is abstract or an interface, methods in its subclass can be implemented using anonymous internal classes.


Example 4: call two methods in the anonymous internal class (two methods)
Method 1:
package day10;interface Fu {    public abstract void run();    public abstract void sleep();} public class AnonymousClass01 {    public static void main(String[] args) {     Fu x = new Fu(){      public void run(){      System.out.println(run....);      }      public void sleep(){      System.out.println(sleep....);      }      };      x.run();      x.sleep();    }}
Running result: run... sleep ....
Call the run () and sleep () Methods of the referenced object respectively!


Method 2:
package day10;interface Fu {    public abstract Fu run();    public abstract void sleep();} public class AnonymousClass01 {    public static void main(String[] args) {     new Fu(){      public Fu run(){      System.out.println(run....);        return this;      }      public void sleep(){      System.out.println(sleep....);      }      }.run().sleep();          }}
Running result: run... sleep ....
After the run () method is executed, the Fu class object is returned to change the last. run () of the anonymous object to the object, and then. sleep () is Fu. sleep. The above two methods can also be used in the inherited state

 


 

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.