Java's inner classes continued

Static inner classes

1, syntax

[Modifier] [class extends outside the external name class super class] [parent class implements external interfaces {}
    [] static class other modifiers [static inner class extends inside the static super class implements] [static inner class ] {parent interface
        member list static inner classes;
    }
    
    other members outside the class list;
}

2, Notice

(1) can contain all members of the class

(2) permission modifier: four kinds; other modifiers: abstract, final

(3) Use only static members outside of class

(4) internal static class requirements outside of the outer class:

(1) If you are using static members static inner classes
        outside the class name. Static inner class name. Static member
(2) If you are using a non-static member static inner classes
        ① create objects static inner classes
        outside the class name. static inner class name object name = new class name of the external static inner class name ([argument list]);
        ② calling non-static member through an object
        object name .xxx

3, the sample code

class Out{
    private static int i = 10;
    static class In{
        public void method(){
            System.out.println(i);//可以
        }
        public static void test(){
            System.out.println(i);//可以
        }
    }
    
    public void outMethod(){
        In in = new In();
        in.method();
    }
    public static void outTest(){
        In in = new In();
        in.method();
    }
}
class Test{
    public static void main(String[] args){
        Out.In.test();
        
        Out.In in = new Out.In();
        in.method();
    }
}

Non-static inner classes

1, syntax

[Implements external interfaces parent class modifiers] [] [class extends outside the external name class super class {}
    [] Modifier [nonstatic inner classes class extends nonstatic inner super class implements] [nonstatic inner classes Interface] {parent
        list of members within the class of non-static;
    }
    
    other members outside the class list;
}

// use non-static inner classes of non-static member
// (1) create an object outside the class
outside of class name Object name 1 = new outer class name (argument list);

// (2) to create or acquire objects non-static inner classes through the object outside the class
// create
an external class name. Non-static inner class name of the object name object name 1.new 2 = non-static inner class name (argument list );

// Get
External non-static method internal name class name of the object class name 2 = object name 1.get nonstatic inner class object (argument list).;

// (3) call it non-static members through non-static inner class
object name 2.xxx

2, the sample code

class Out{
    private static int i = 10;
    private int j = 20;
    class In{
        public void method(){
            System.out.println(i);
            System.out.println(j);
        }
    }
    
    public void outMethod(){
        In in = new In();
        in.method();
    }
    public static void outTest(){
    }
    
    public Inner getInner(){
        return new Inner();
    }
}
class Test{
    public static void main(String[] args){
        Out out = new Out();
        
        Out.In in1 = out.new In();        
        in1.method();
        
        Out.In in2 = out.getIn();    
        in2.method();
    }
}

Partial inner class

1, syntax

[Modifier] [class extends outside the external name class super class] [parent class implements external interfaces {}
    [] Return Value Type modifier method name (parameter list []) {
        [Modifier] partial inner class class [parent interface implements partial inner [class] extends partially inside super class} {
            member list locally within the class;
        }
    }    
    the other members outside the class list;
}

2, the sample code

class Out{
    private static int i = 101;
    private int j = 220;

    
    public void outMethod(){
        class In{
            public void method(){
                //...
                System.out.println(i);
                System.out.println(j);
            }
           }
        In in = new In();
        in.method();
    }
    public static void outTest(){
        final int k = 30;
       class In{
            public void method(){
                System.out.println(i);
                System.out.println(k);
            }
           }
        In in = new In();
        in.method();
    }
}

Guess you like

Origin blog.csdn.net/Brevity6/article/details/91126293