Anonymous inner class of Java basics

Source: Internet
Author: User

Inner class:
Overview: There is also a class inside the class, inside that class called Inner class, outside that class is called external class.
 

Classification:
member Inner class: The inner class that is defined at the member location.
Local inner class: An inner class defined at a local location.

Format:
New class name or interface name () {
overriding all abstract methods in a class or interface;
};

Essence:
is an anonymous subclass object that inherits the class or implements the interface. The anonymous inner class is not a class, but a child class object.


The actual application of the anonymous inner class:
1) When the object method (member method) is called only once.
2) can be passed as an argument to a method.

Suggestions:
When there is only one abstract method in an interface or abstract class, you can consider using an anonymous inner class.

Code Description:

 Public classDemoClass {//External ClassString name; //member Variables        classInner1 {//member Inner class            }        //member Methods     Public voidShow () {intnum = 10;//local variables, basic typesObject obj=NewObject ();//Local variables, reference types//local inner class. Local variables, reference types        classinner2{} }}

Application Examples:

 Public Static voidMain (string[] args) {//Yes, jumpping#jump (); tune three times. //1) When the object method (member method) is called only once. Jumping JM =NewJumping () {// polymorphic@Override Public voidJump () {System.out.println ("I'll jump.");                                                         }                                                                                                   };                                                 Jm.jump ();                                                 Jm.jump ();                                                                                                            Jm.jump (); //2) can be passed as an argument to a method. ShowNewjumping () {@Ove Rride Public voidJump () {System.out.println ("I'll jump, I can pass as an argument to a method.");                                                    }                                                      });                                                                                                                                                                                }  Public Static voidShow (jumping jm) {jm.jump (); }                                                           Public InterfaceJumping {Final  Public Static intnum = 10;  Public Abstract voidJump ();}

 Public Static voidMain (string[] args) {/** Implemented by anonymous internal classes.                                  * New class name or interface name () {//Override all abstract methods in class or interface;                                                     }; */                                                       NewAnimal () { Public voideat () {System.out.println ("I am an anonymous inner class realization of the way the cat eats fish");                                              }}.eat (); }                                                         /*** Abstract class: Animal *@author * */ Public Abstract classAnimal { Public Abstract voideat ();}

Anonymous inner class of Java Foundation

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.