Java inner classes, member class, partial classes, anonymous classes, etc.

Depending on the location within the class, the internal class can be divided into
1 internal class members
2. Local inner class

class C {
     // member inner class 
    class B { 
    } 

    public  void show1 () {
     // local inner class 
        class D { 
        } 
    } 
}

Members of the inner class

Members of the inner class access format:

An external name class internal object name = class name external object. Inside class object
. 1  class C { 
 2  
. 3    // member inner classes 
. 4     class B {
 . 5           public  void Show () 
 . 6           { 
 . 7              System.out.println ( "Hello" ); 
 . 8       } 
 . 9  } 
 10  } 
 . 11  public  class niming { 
 12 is    public  static  void main (String [] args) { 
 13 is  
14  // creates a corresponding object to access the internal members of the class of functions 
15 CB B = new new C (). new new B (); 
 16  b.show ();} 
17 }

Although the inner class can access the manner described above, but the actual development, often set to the internal private members, in order to protect the security of data, to prevent direct external use
1. Set the inner class private members

. 1  class C {
 2   // member inner classes, made private 
. 3   Private  class B {
 . 4       public  void Show () 
 . 5       { 
 . 6          System.out.println ( "Hello" );} 
 . 7      }
 . 8     public  void show1 () { 
 . 9       B B = new new B (); 
 10       b.show ();} 
 . 11     } 
 12 is    public  class niming { 
 13 is        public  static  void main (String [] args) {
 14           //Create Object Access class function corresponds to the internal member 
15           C = C new new C ();
 16          c.show1 (); 
 . 17         } 
 18 is     }
 . 19             

2. Set the inner class static class

Specific: static inner classes access to external class members can only access static members outside of class
. 1  class C { 
 2      // static member variable 
. 3      Private  static  int NUM = 20 is ; 
 . 4      // static member methods 
. 5      public  static  void Show2 () { 
 . 6          System.out.println ( "Hello World" );
 . 7       } 
 . 8      // members of the inner classes, private static set 
. 9      private  static  class B { 
 10      public  void Show () { 
 . 11          System.out.println (NUM); 
 12 is          Show2 (); 
 13 is          } 
 14     } 
 15      public  void show1 () {
 16       B B = new new B ();
 . 17       b.show (); 
 18 is      } 
 . 19  }
 20 is   public  class niming {
 21 is       public  static  void main (String [] args) {
 22 is       // Create Object access to a corresponding internal class function member 
23 is      C = C new new C (); 
 24      c.show1 (); 
 25      } 
 26 is }                     

Partial inner class

Direct access to members outside of class

Inner class access external variables must be defined as the final or static type

 1 public class Outer{ 
 2     public void inner(){
 3      final int num = 5; 
 4      class InnerClass{ 
 5         private int num = 4; 
 6         public void testNum(){ 
 7             System.out.println(this.num); 
 8         }
 9     } 
10    }
11 }        

First, the internal and external class category is in fact the same level, not because of internal class defined in the method will be finished with the process and the followers were destroyed. The problem comes, if the method of the outer class variable is not defined in the final, then when the outer class method is finished, and this will certainly be the local variables of the GC, however, a method within the class has not been executed, this when he cited external variables have been found. If defined as final, java will be a copy of this variable as a member variable built into the inner class, this is the case, because the final value modified still can not change, so this variable points to the memory area will not become.



 

Guess you like

Origin www.cnblogs.com/yz123/p/11962473.html