29. The members of the class of object-oriented ----- inner classes

  In Java, allows the definition of a class is located inside another class, the former is called internal type, which is called an external class.

1. The inner class classification

Members of the inner class (static and non-static) and a local inner class (the method, the code block, the constructor)

2. The members of the inner class to understand

On the one hand, as a member of the outer class:

   > Call an external class structure
   > can be static modification
   > may be modified in four different permissions

On the other hand, as a class:

   > Class defines the properties, methods, and other constructors
   > final modification may be expressed such can not be inherited. The implication, do not use final, it can be inherited
   > abstract may be modified

Features:

  • All member properties and methods class members inside unconditional access to the outer class is not private and static influence
  • When the property or method members of the same name inside the class and outside the class, hidden phenomenon occurs. Call directly within the class is a member properties and methods within the class. Need "external class" .this. Member variable / method member
  • If you need to access the outer class inner class, you need to generate internal objects, to access internal class object.
  • Members of the inner class is attached to the outer class existence, if you need to access internal members of the class, then you need to create an external class.
  • As the members of the inner class existence that members of the outer class, so the class can be modified using private public protect modifier modified no authority to represent the problem of access to the internal. The internal and external can not.

public  class Test {
     public  static  void main (String [] args) {
         // create a static member within the class object 
        Person.Inner2 Inner2 = new new Person.Inner2 (); 

        // Create a nonstatic member inside the object class 
        Person p = new new the Person (); 
        Person.Inner1 inner1 . = P new new Inner1 (); 
        inner1.show (); // name = Zhang, Age = 23 is 
        inner2.show (); // static member inner class 
    } 
} 

class the Person { 
    String name; 
    int Age; 

    // non-static member inner class 
    class{Inner1
         public  void Show () { 
            name = "Sally" ; 
            Age = 23 is ; 
            System.out.println ( "name =" + name + ", Age =" + Age); 
        } 
    } 

    // static member inner class 
    static  class Inner2 {
         public  void Show () { 
            System.out.println ( "static member inner class" ); 
        } 
    } 

    public  void eAT () { 
        System.out.println ( "eat" ); 
    } 
}

3. How to call an external structure of class members inside the class?

class the Person { 
    String name = "Bob" ;
 public  void EAT () { 
} 
// non-static member inner class 
    class Bird { 
        String name = "Rhododendron" ;
         public  void the display (String name) { 
            System.out.println (name) ; // shaped process parameters 
            System.out.println ( the this .name); // attribute inner class 
            System.out.println (the Person. the this .name); // attribute outer class
         // Person.this.eat (); 
        } 
    } 
}

4. Local inner class

Features:

  • You can not use access modifier keywords
  • Local action interval within only declared

 

 The anonymous inner classes

Features:

  • Anonymous inner classes can not use the static modifier and rights
  • Anonymous no internal constructor. By generating a new object reference to XXX.
  • Anonymous inner classes belonging to a local inner class, all of the local conditions inside similarly restricted interior remain valid anonymous

 

 

 

 

Author: Java beauty

Date: 2020-03-30

Guess you like

Origin www.cnblogs.com/897463196-a/p/12596162.html