Java class members of the inner class

Internal class meaning:

Allows the definition of a class is located inside another class in Java, the former is referred to as inner classes, which are called external type.

Inner class is generally used in the definition of its class or blocks of statements, it must be given the full name to refer to it externally.

Inner class name can not be the same as the class name of the outer class that contains it.

To create an internal class object:

The external name class class name inside the external variable name = new class name () .new inner class name ();

Create a static inner class object:

. External class name class name inside the external variable name = new class name inside the class name ();

Members of the inner class:

(Internal static members and non-members of the inner class static)

Members of the inner class as a class member usage:

1. Different classes and external, Inner class may also be declared private or protected.

2. You can call the structure of the outer class.

3.Inner class can be declared as static, but this time no longer use the outer class

Non-static member variables.

Members of the inner class as the class usage:

1. The internal structure can define the properties, methods, structure and the like.

2. can be declared as an abstract class, so you can be inherited by other internal class.

3. can be declared final.

4. Generate OuterClass $ InnerClass.class bytecode file after compilation.

Precautions:

1. Non-static members inside the class members can not be declared static.

2. Only members of the outer class or static inner classes in a statement before static members.

3. The members of the outer class access members inside the class, we need to "inner class. Members" or "inner class object. Members" approach.

4. The inner member can be used directly to all members of the class of the outer class, including private data.

5. When you want the static member portion of the outer class when using the internal class, consider inner class declared as static.

Local inner class usage:

1. It can only be used in the method of statement or code block, and the first statement after use.

Any place other than the class can not be used.

2. However, it may be objects by the return value returned external use,

Return Value Type can only be partial internal super class or interface type parent.

It features partial inner class:

1. The inner class is still a separate class, inner classes after compilation will be compiled into a separate .class file,

But preceded by the class name and $ sign outside the class, as well as figures.

2. It can only be used in the method of statement or code block, and the first statement after use.

Any place other than the class can not be used.

3. Local inner class can use an external member of the class, including private.

4. Local inner class can use external local variables of methods, but must be final.

This is a statement by the local inner class period and due to different local variables.

5. Similar local variables and partial inner class status, can not use the public, protected, default, private

6. Local inner classes can not use static modification, and therefore can not contain static members

Anonymous inner classes Features:

1. anonymous inner class must inherit the parent class or implement an interface

2. anonymous inner class can have only one object

3. anonymous inner classes are only allowed polymorphic form reference

Anonymous inner classes Note:

1. anonymous inner classes can not define any static members, methods and classes, can only create an instance of an anonymous inner class.

2. An anonymous inner classes must be behind the new, which implicitly implemented using an interface or to implement a class.

. 1  public  class SyntaxDemo {
 2    public  static  void main (String [] args) {
 . 3      // test class external access to the internal members of the class 
. 4      OuterClass was OuterClass = new new OuterClass was ();
 . 5      outerClass.testInnerAccess ();    
 . 6      OuterClass.StaticInner staticInner = new new OuterClass.StaticInner ();
 . 7    }
 . 8  }
 . 9  
10  class OuterClass was {
 . 11    Private  int I;
 12 is    Private  static  Double K;
 13 is   public  void testInnerAccess () {
 14      // create internal classes Ang 
15      InnerClass InnerClass = new new InnerClass ();
 16      System.out.println (innerClass.j);
 . 17    }
 18 is  
. 19    public  static  void testAccessFromStaicInner () {
 20 is      int m = 100 ;
 21 is      System.out.println ( "testAccessFromStaicInner" );
 22 is    }  
 23 is    // by a member class plus position inside the private authority modifier to tender inner class and can only be accessed by external classes to 
24     
25    private  class InnerClass {
 26 is      int= 100 J ;
 27      // define a method to test the 
28      public  void testOuterPrivate () {
 29        System.out.println (I);
 30      }
 31 is    }
 32   
33 is         // is the modified static permission modifier    
34 is    static  class StaticInner {
 35       // member variable and static methods can not access the non-static context
 36      // Private int J = I; 
37 [      
38 is      public  void testAccess () {
 39        // static context can not access the non-static member variables and methods members
 40       
41 is        / / static members access outside the class 
42       System.out.println (K);
 43 is  
44 is        // access outer class Static method 
45        testAccessFromStaicInner ();
 46 is      }
 47    }
 48 }

Guess you like

Origin www.cnblogs.com/ZengBlogs/p/12167093.html