1. Anonymous internal class
Anonymous classes cannot be referenced because they cannot have names. They must be declared as part of the new statement at creation. This requires another form of new statement, as shown below: New <class or interface> <class subject> in this form of new statement declares a new anonymous class, it extends a given class or implements a given interface. It also creates a new instance of that class and returns it as the result of the statement. The class to be extended and the interface to be implemented are the operands of the new statement, followed by the subject of the anonymous class. If an anonymous class is extended to another class, its subject can be a member of the class, override its method, and so on, which is the same as any other standard class. If an anonymous class implements an interface, its subject must implement the interface method.
Considerations for anonymous internal classes
Note that the declaration of anonymous classes is implemented at compilation and instantiation is performed at runtime. This means that a new statement in the For Loop will create several instances of the same Anonymous class, rather than creating several instances of different anonymous classes.
When using anonymous internal classes, remember the following principles:
· No constructor is allowed for anonymous internal classes.
· Anonymous internal classes cannot define any static members, methods, and classes.
· Anonymous internal classes cannot be public, protected, private, or static.
· Only one instance of the anonymous internal class can be created.
· An anonymous internal class must be behind New and be used to implicitly implement an interface or implement a class.
· Because the anonymous internal class is a local internal class, all restrictions on the local internal class take effect.
· Internal classes can only access static variables or static methods of external classes.
1 2 Interface Nonameclass { 3 4 Void Print (); 5 6 } 7 8 9 Class Noinnerclass { 10 11 12 Public Nonameclass show (){ 13 14 Return New Nonameclass (){ 15 16 17 @ Override 18 19 Public Void Print (){ 20 21 System. Out. println ("Hello World" ); 22 23 } 24 25 }; 26 27 } 28 29 } 30 31 32 Public Class Nonameinnerclass { 33 34 Public Static Void Main (string [] ARGs ){ 35 36 Noinnerclass innerclass = New Noinnerclass (); 37 38 Innerclass. Show (). Print (); 39 40 } 41 42 }
2. static internal classes (Nested classes) (only internal classes can be defined as staticclass)
Static internal classes can only call static variables and static methods of external classes
Static or non-static members can be defined in static internal classes.
1 Class Innerclass { 2 3 Private Static Int A = 7 ; 4 5 Public Static Void Show () 6 7 { 8 9 System. Out. println ("show" ); 10 11 } 12 13 Public Static Class Staticinner { 14 15 16 Public Void Info () 17 18 { 19 20 System. Out. println (); 21 22 Show (); 23 24 } 25 26 } 27 28 } 29 30 31 Public Class Innerclasstest 32 33 { 34 35 Public Static Void Main (string [] ARGs ){ 36 37 Innerclass. staticinner innerclass = New Innerclass. staticinner (); 38 39 Innerclass.info (); 40 41 } 42 43 }
3. Local internal class
Member internal classes can call static and non-static variables and methods of external classes
However, the static method cannot be defined in the internal class of the member.
1 Class Localclass { 2 3 Private Int A = 1 ; 4 5 Private Static Int B = 2 ; 6 7 Void Info (){ 8 9 Final Int C = 4 ; 10 11 Class Inner { 12 13 Int A = 3 ; 14 15 16 Void Print (){ 17 18 System. Out. println (); // Internal class variables 19 20 System. Out. println (localclass. This . ); // External variables 21 22 System. Out. println (B ); // Static variables of external classes 23 24 System. Out. println (C ); // Only the final variables in the method can be accessed. 25 26 } 27 28 } 29 30 New Inner (). Print (); 31 32 } 33 34 } 35 36 37 Public Class Localinnerclass { 38 39 Public Static Void Main (string [] ARGs ){ 40 41 Newlocalclass (). Info (); 42 43 } 44 45 }
4. Member internal class
Member internal classes can call static and non-static variables and methods of external classes
However, the static method cannot be defined in the internal class of the member.
Class Memberclass { Private Int A = 2 ; Private Static Int B = 3 ; Private Static Void Show () {system. Out. println ( "Show" );} Class Innermember { Private Int A = 45 ; Void Info () {system. Out. println (B); system. Out. println (); // According to the proximity principle, a is the variable in the current scope and cannot obtain a of the external class. System. Out. println (memberclass. This . ); // In this way, you can access the cognominal variables of the external class. Show ();}}} Public Class Memberinnerclass { Public Static Void Main (string [] ARGs) {memberclass. innermember = New Memberclass (). New Innermember (); // Some people may wonder why it is different from static internal class instantiation. // This is because static internal classes belong to Classes just like static methods, while member Internal classes belong to objects. New Memberclass ().New Innermember () obtains the Instance Object innermember.info () ;}} of each object about innermember ();}}