Internal and external type access classes to each other

Internal categories:

① static inner class can have non-static method

② When the internal class has a static method or static member variables, it must be static inner classes

 

1, external access to internal class categories:

   Static inner classes are modified: direct new

    Inner in = new Inner();

   Internal static class is not modified: it must first new instance of the outer class, then the class of the new internal

    Inner in = new Outer().new Inner();

2, the inner class to access external class :( outer class .this. Variables)

3, the outer internal classes and methods to access each class:

① The method of the external static non-static methods and classes of non-static test inside each other voice access category:

   voice test access to the new outer new class then the class, method and then transferred

  

public class Outerclass {
    class Inner{
        public void voice(){
            System.out.println("voice()");
        }
    }
    public static void test(){
        new Outerclass().new Inner().voice();
    }
    public static void main(String[] args) {
      //主函数调用test方法
       test();13     }
}

 

  voice test access outer class .this. method (external reference type holding)

public class Outerclass {
    class Inner{
        public void voice(){
            Outerclass.this.test();
        }
    }
    public static void test(){
        System.out.println("test()");
    }
    public static void main(String[] args) {
    //主函数调用voice()
        Inner in = new Outerclass().new Inner();
        in.voice();
    }
}

 

Each non-static method calls between voice test ② non-static methods and classes outside static inner classes

  voic access test   

public class Outerclass {
    static class Inner{
        public void voice(){
            new Outerclass().test();
        }
    }
    public void test(){
        System.out.println("test()");
    }
    public static void main(String[] args) {
    //主函数调用voice()方法
        new Outerclass.Inner().voice();
    }
}

  test access voice

public  class OuterClass {
     static  class Inner {
         public  void Voice () { 
            System.out.println ( "Voice ()" ); 
        } 
    } 
    public  void Test () {
      // . 1, other types of access to external static inner class class non-static method
        // new new Outerclass.Inner () Voice ();.
      // 2, Test method Outerclass herein in internal access static class non-static method 
       new new inner () Voice ();. 
    } 
    public  static  void main (String [] args) {
      // main function calls the test method 
        new new OuterClass () test ();. 
    } 
}

Transfer: https://www.cnblogs.com/rgever/p/8902758.html

Guess you like

Origin www.cnblogs.com/51python/p/11483134.html