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