[Java] InnerClass inner class

Java allows the definition of a class to be located inside another class, the former being the inner class and the latter being called the outer class

InnerClass generally defines the complete name when referring to an external class in its class or statement block

 

Member Inner Class & Local Inner Class

-Static member inner class

-Non-static member inner class

-Anonymous inner class

 

Static inner class member inner class local inner class

// Compared to the inner class, this is an outer class 
public  class OuterClass {
     public  int a;
     int b;
     protected  int c;
     private  int d; 

    static  int e; 
    
    public  void memberMethod () { 
        System.out.println ( " Member methods of outer classes! " ); 

        // Member inner classes can create instances 
        InnerClass innerClass = new InnerClass ();
         // Static inner classes can create instances 
        StaticInnerClass staticInnerClass = new StaticInnerClass (); 
    } 

    public  static void staticMethod () { 
        System.out.println ( "Static method of external class!" ); 
    } 

    // Member internal class and static internal class can both be abstract abstract or final, and can be accessed and modified by 4 kinds of permissions 

    // member internal Class does not allow any static existence 
    class InnerClass {
         // Member internal class can modify all access permissions 
        public  int a;
         int b;
         protected  int c;
         private  int d; 

        // Member internal class can not write static methods
         // static void staticMethod () {} 

        // Member inner class can declare member method 
        void memberMethod () {
             // Member inner class can call member method of outer class
            OuterClass. This .memberMethod (); 

            // The static method of the inner class of the member can also be called 
            OuterClass.staticMethod (); 
        } 

        // The inner class of the member can be constructed by 
        InnerClass () {} 

        // Access to each field 
        public  void checkField ( int a) {
             // Formal parameter a 
            a = 50 ;
             // Field of this member's inner class a 
            this .a = 100 ;
             // Field of outer class a 
            OuterClass. this .a = 150 ; 
        } 
    } 

    // static inner classes 
    static  classStaticInnerClass {
         // Static inner class can decorate all access permissions 
        public  int a;
         int b;
         protected  int c;
         private  int d; 

        // Static inner class can declare member methods 
        void memberMethod () {
             // Can call static methods of outer classes 
            OuterClass.staticMethod (); 

            // Member method of outer class is not OK
             // OuterClass.this.memberMethod (); 
        } 

        // Static inner class can declare static method 
        static  void staticMethod () {
             // Can call static method of outer class 
            OuterClass .staticMethod (); 

            //The member method of the outer class doesn't work
             // OuterClass.this.memberMethod (); 
        } 

        public  void fieldCheck ( int a) {
             // Formal parameter field 
            a = 100 ; 

            // Static inner class field 
            this .a = 150 ; 

            // External Class fields can only access static methods and static fields 
            OuterClass.e = 300 ; 
        } 

        // Static inner classes can also be constructed by 
        StaticInnerClass () {} 
    } 

    public  void classMethod () {
         // Local inner classes do not have access to modify, only do Used in this method, you can set final to modify the local inner class 

        // The local variables accessed by the inner class must be Final
         //The scope of x survives in this method.
         // When the method call ends and the method pops, the x variable is destroyed. 
        final  int x = 123 ; 
        
        // Local internal class 
        class LocalInnerClass { 

            // But the local internal class instance is still Only now, still keep the reference point of the x variable
             // if you call this method after the local method is destroyed, you can no longer access the x variable, this is a BUG problem
             // but JDK8 has implicitly expressed the x variable Is final modified, x must be a constant presence
             // that means if x is destroyed, but its value does not change, it can still be given by access 
            
            // The reason is that the scope of the instance of the local inner class is greater than The scope of this variable 
            public  int returnX () {
                 // x + = 100; x variable cannot be modified 
                return x; 
            } 
        } 
    } 
    
    //The usage of local internal classes can be used to return an instance that implements an interface. 
    Public Comparable getComparable () {
         class LocalInnerClass implements Comparable {
             // Local internal class rewrite interface method 
            @Override
             public  int compareTo (Object o) {
                 return 0 ; 
            } 
        } 
        // Return to external call 
        return  new LocalInnerClass (); 
    } 
}
Three internal classes

 

Static internal class and member internal class external instance creation and access

public  class ClassTest {
     public  static  void main (String [] args) {
         //   Instantiate static inner class 
        OuterClass.StaticInnerClass staticInnerClass = new OuterClass.StaticInnerClass ();
         //   Member method of static inner class 
        staticInnerClass.memberMethod ();
         //   Static method of static inner class 
        OuterClass.StaticInnerClass.staticMethod (); 

        // Instant   member inner class, you must have an instance of the outer class before 
        OuterClass outerClass = new OuterClass (); // First create an outer instance 
        OuterClass.InnerClass innerClass = outerClass. new InnerClass (); //Then call the internal initialization through the external instance.
         //   You can also directly anonymous the external class object, call the inner class to initialize 
        OuterClass.InnerClass innerClass2 = new OuterClass (). New InnerClass (); 

        // Member inner class calls the member method 
        innerClass.memberMethod (); 
    } 
}
Instantiation and access

 

Anonymous inner class

public  class ClassTest {
     public  static  void main (String [] args) { 
        
        AnonymousInnerClass anonymousInnerClass = new AnonymousInnerClass () {
             // This is the class body of the anonymous inner class, anonymous, as the name implies, there is no class name 
            
            // method to rewrite the interface 
            @ Override
             public  void aicMethod () { 
                System.out.println ( "This is the anonymous inner class" );     
            } 
            
            // Anonymous inner class can only be declared this time, and cannot be saved like the displayed class
             // used to create to implement a An interface can either inherit an abstract class or inherit an ordinary class
             // suitable for implementing interfaces with only one method, such as multi-threaded Runnable 
            
            //Note that the thread also has a Callable 
        }; 
    } 
} 

// Declare an interface and an abstract method 
interface AnonymousInnerClass {
     void aicMethod (); 
}
Anonymous inner class

 

Guess you like

Origin www.cnblogs.com/mindzone/p/12724933.html