Cross call between the internal and external classes Class

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

 

General internal class member variable position outside the class, like this:

1 public class Outer {
2     class Inner{
3         
4     }
5 }

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, internal access to external class categories: (external .this class variables)

 

Copy the code
 1 public class Outer {
 2     int x = 9;
 3     class Inner{
 4         int x = 8;
 5         public void test(){
 6             int x = 7;
 7             System.out.println(x);
 8             System.out.println(this.x);
 9             System.out.println(Outer.this.x);
10             test1();
11         }
12     }
13     
14     private void test1(){
15         System.out.println("test");
16     }
17     public static void main(String[] args) {
18         Inner in = new Outer().new Inner();
19         in.test();
20     }
21 }
Copy the code

 

 

 

Output: 7,8,9, test

Analysis: seventh row in the eighth row I believe we would have no problems, the ninth line of output was 9, indicating that access to the outer class variables, and test output describes the inner class access to the outer class method test1

to sum up:

  9 output is the reason: because the inner class to hold a reference to the outer class, format: External class name .this

  The reason can be called private method is: Because they are so you can call in a class in the Outer

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:

   ! Test -----> voice outer first new class then the new class, and then transferred Method

Copy the code
 1 public class Outerclass {
 2     class Inner{
 3         public void voice(){
 4             System.out.println("voice()");
 5         }
 6     }
 7     public static void test(){
 8         new Outerclass().new Inner().voice();
 9     }
10     public static void main(String[] args) {
      //主函数调用test方法
11 test();13 } 14 }
Copy the code

Output: voice ();

 
 

   !! voice -----> test outside the class .this. Method (holding outer class reference)

Copy the code
 1 public class Outerclass {
 2     class Inner{
 3         public void voice(){
 4             Outerclass.this.test();
 5         }
 6     }
 7     public static void test(){
 8         System.out.println("test()");
 9     }
10     public static void main(String[] args) {
    //主函数调用voice()
11 Inner in = new Outerclass().new Inner(); 12 in.voice(); 13 } 14 }
Copy the code

 Output: test ();

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

 ! voice------>test

Copy the code
 1 public class Outerclass {
 2     static class Inner{
 3         public void voice(){
 4             new Outerclass().test();
 5         }
 6     }
 7     public void test(){
 8         System.out.println("test()");
 9     }
10     public static void main(String[] args) {
    //主函数调用voice()方法
11 new Outerclass.Inner().voice(); 12 } 13 }
Copy the code

Output: test ();

!!  test----->voice

Copy the code
. 1  public  class OuterClass {
 2      static  class Inner {
 . 3          public  void Voice () {
 . 4              System.out.println ( "Voice ()" );
 . 5          }
 . 6      }
 . 7      public  void Test () { 
      //. 1, other types of access to the external internal static non-static method in class class
. 8 // new new Outerclass.Inner () Voice ();.
      // 2, Test method Outerclass herein in internal access static class non-static method
       new inner (). Voice ();
. 9 } 10 public static void main (String [] args) {
      // main function calls the test method
11 new Outerclass().test(); 12 } 13 }
Copy the code

Output: voice ();

 

4. Summary:

External access to internal class categories: the object must be established within the class

Inner class to access external classes: an inner class can directly access the external members of the class include private members as references to external class holds inner class

 

Exception: in the process of internal write class outer class (i.e., the position of the local variable)

  1, the internal class can be defined externally variables / constants

  2, can only be final / abstract modification

  3, can only access is final / abstract modified variables

  4, can directly access the external members of the class, as also holds a reference to the outer class

  

Original Address: https://www.cnblogs.com/rgever/p/8902758.html

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

 

General internal class member variable position outside the class, like this:

1 public class Outer {
2     class Inner{
3         
4     }
5 }

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, internal access to external class categories: (external .this class variables)

 

Copy the code
 1 public class Outer {
 2     int x = 9;
 3     class Inner{
 4         int x = 8;
 5         public void test(){
 6             int x = 7;
 7             System.out.println(x);
 8             System.out.println(this.x);
 9             System.out.println(Outer.this.x);
10             test1();
11         }
12     }
13     
14     private void test1(){
15         System.out.println("test");
16     }
17     public static void main(String[] args) {
18         Inner in = new Outer().new Inner();
19         in.test();
20     }
21 }
Copy the code

 

 

 

Output: 7,8,9, test

Analysis: seventh row in the eighth row I believe we would have no problems, the ninth line of output was 9, indicating that access to the outer class variables, and test output describes the inner class access to the outer class method test1

to sum up:

  9 output is the reason: because the inner class to hold a reference to the outer class, format: External class name .this

  The reason can be called private method is: Because they are so you can call in a class in the Outer

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:

   ! Test -----> voice outer first new class then the new class, and then transferred Method

Copy the code
 1 public class Outerclass {
 2     class Inner{
 3         public void voice(){
 4             System.out.println("voice()");
 5         }
 6     }
 7     public static void test(){
 8         new Outerclass().new Inner().voice();
 9     }
10     public static void main(String[] args) {
      //主函数调用test方法
11 test();13 } 14 }
Copy the code

输出:voice();

 

   !!  voice----->test        外类.this.方法(持有的外部类的引用)

Copy the code
 1 public class Outerclass {
 2     class Inner{
 3         public void voice(){
 4             Outerclass.this.test();
 5         }
 6     }
 7     public static void test(){
 8         System.out.println("test()");
 9     }
10     public static void main(String[] args) {
    //主函数调用voice()
11 Inner in = new Outerclass().new Inner(); 12 in.voice(); 13 } 14 }
Copy the code

 输出:test();

  ②外部类的非静态方法test和静态内部类中的非静态方法voice之间的相互调用

 ! voice------>test

Copy the code
 1 public class Outerclass {
 2     static class Inner{
 3         public void voice(){
 4             new Outerclass().test();
 5         }
 6     }
 7     public void test(){
 8         System.out.println("test()");
 9     }
10     public static void main(String[] args) {
    //主函数调用voice()方法
11 new Outerclass.Inner().voice(); 12 } 13 }
Copy the code

输出:test();

!!  test----->voice

Copy the code
 1 public class Outerclass {
 2     static class Inner{
 3         public void voice(){
 4             System.out.println("voice()");
 5         }
 6     }
 7     public void test(){
      //1、其他类访问外部类中的静态内部类的非静态方法
8 // new Outerclass.Inner().voice();
      //2、此处的Outerclass中的test方法访问静态内部类中的非静态方法
       new Inner().voice();
9 } 10 public static void main(String[] args) {
      //主函数调用test方法
11 new Outerclass().test(); 12 } 13 }
Copy the code

Output: voice ();

 

4. Summary:

External access to internal class categories: the object must be established within the class

Inner class to access external classes: an inner class can directly access the external members of the class include private members as references to external class holds inner class

 

Exception: in the process of internal write class outer class (i.e., the position of the local variable)

  1, the internal class can be defined externally variables / constants

  2, can only be final / abstract modification

  3, can only access is final / abstract modified variables

  4, can directly access the external members of the class, as also holds a reference to the outer class

  

Guess you like

Origin www.cnblogs.com/jpfss/p/10993126.html