Java Internal Class Principle Overview and Detailed Explanation of Usage Examples

  • 2021-07-09 08:14:55
  • OfStack

In this paper, the principle, overview and usage of Java internal classes are described with examples. Share it for your reference, as follows:

Overview of inner classes


/*
   Overview of inner classes :
     When a class is defined inside another class, this class is called an inner class. 
     Example: In the class A Define in 1 Category B , class B Is the inner class. 
   Internal access characteristics: 
    A: Internal classes can directly access members of external classes, including private ones. 
    B: To access the members of the inner class, an external class must create an object. 
*/
class Outer {
  private int num = 10;
  class Inner {
    public void show() {
      // Internal classes can directly access members of external classes, including private ones. 
      System.out.println(num);
    }
  }
  public void method() {
    // Symbol not found 
    //show();
    // To access the members of the inner class, an external class must create an object. 
    Inner i = new Inner();
    i.show();
  }
}
class InnerClassDemo {
  public static void main(String[] args) {
  }
}

Internal class location


/*
   Internal class location 
     Member position : A class defined at a member position is called an intra-member class. 
     Local position : A class defined in a local position is called a local inner class. 
   Member position : A class defined at a member position is called an intra-member class. 
*/
class Outer {
  private int num = 10;
  // Member position 
  /*
  class Inner {
  }
  */
  public void method() {
    // Local position 
    class Inner {
    }
  }
}
class InnerClassDemo2 {
  public static void main(String[] args) {
  }
}

How to directly access a member of a class within a member


/*
   Member inner class :
     How to access members of an inner class directly. 
     External class name . Internal class name   Object name  =  External class object . Internal class object ;
*/
class Outer {
  private int num = 10;
  class Inner {
    public void show() {
      System.out.println(num);
    }
  }
}
class InnerClassDemo3 {
  public static void main(String[] args) {
    // Requirements: I want to access Inner Class show() Method 
    // Format: External class name . Internal class name   Object name  =  External class object . Internal class object ;
    Outer.Inner oi = new Outer().new Inner();
    oi.show();
  }
}

Modifiers for classes within members:


/*
   Modifiers for classes within members: 
    private  In order to ensure the security of data, 
    static  To facilitate access to data 
       Note: External class data accessed by static inner classes must be statically decorated. 
   Case: I have 1 Individual ( People have a body, and there is a heart in the body. )
    class Body {
      private class Heart {
        public void operator() {
          System.out.println(" Cardiac bypass ");
        }
      }
      public void method() {
        if( If you're a surgeon ) {
          Heart h = new Heart();
          h.operator();
        }
      }
    }
     According to what we just explained, use it 1 Under 
    Body.Heart bh = new Body().new Heart();
    bh.operator();
    // Add private After that, you can't be visited, so, how to play ?
    Body b = new Body();
    b.method();
*/
class Outer {
  private int num = 10;
  private static int num2 = 100;
  // Inner classes are statically decorated because they can be seen as members of outer classes 
  public static class Inner {
    public void show() {
      //System.out.println(num);
      System.out.println(num2);
    }
    public static void show2() {
      //System.out.println(num);// Report an error. External class data accessed by static inner classes must be statically decorated. 
      System.out.println(num2);
    }
  }
}
class InnerClassDemo4 {
  public static void main(String[] args) {
    // Use inner classes 
    //  Qualified new static class 
    //Outer.Inner oi = new Outer().new Inner();// This access method is wrong 
    //oi.show();
    //oi.show2();
    // The access mode of the class inside the member after being statically decorated is :
    // Format: External class name . Internal class name   Object name  = new  External class name . Internal class name ();
    Outer.Inner oi = new Outer.Inner();
    oi.show();
    oi.show2();
    //show2() Another of 1 The method of calling. Because of static methods, they can be called by class name. 
    Outer.Inner.show2();
  }
}

There is no inheritance relationship between inner classes and outer classes.

Qualify this objects by external class names


/*
   Case: 
     Please fill in the blanks and output them separately 30 , 20 , 10 . 
   Note: 
    1: There is no inheritance relationship between inner classes and outer classes. 
    2: Qualified by external class name this Object 
      Outer.this
*/
class Outer {
  public int num = 10;
  class Inner {
    public int num = 20;
    public void show() {
      int num = 30;
      System.out.println(num);
      System.out.println(this.num);
      //System.out.println(new Outer().num);
      System.out.println(Outer.this.num);
    }
  }
}
class InnerClassTest {
  public static void main(String[] args) {
    Outer.Inner oi = new Outer().new Inner();
    oi.show();
  }
}

The problem of accessing local variables by internal classes at local positions


/*
   Local inner class 
    A: You can directly access members of external classes 
    B: In a local location, you can create an inner class object and use the local inner class function by calling the inner class method through the object 
   Precautions: 
     Considerations for accessing local variables by local inner classes ?
    A: Local inner classes must access local variables with final Modify 
    B: Why ?
       Local variables are called as the method is called and disappear as the call is completed. 
       And heap memory Inner Does not disappear immediately. So, we add final Decorate. 
       Join final After modification, this variable becomes a constant. Since it is a constant. You disappeared. 
       What I store in memory is data 20 So, I still have data in use. 
*/
class Outer {
  private int num = 10;
  public void method() {
    //int num2 = 20;
    final int num2 = 20; // Local inner classes must access local variables with final Modify 
    class Inner {
      public void show() {
        System.out.println(num);
        // Access local variables from inner classes num2;  Need to be declared as the final type 
        System.out.println(num2);//20
      }
    }
    //System.out.println(num2);
    Inner i = new Inner();
    i.show();
  }
}
class InnerClassDemo5 {
  public static void main(String[] args) {
    Outer o = new Outer();
    o.method();
  }
}

Anonymous inner class format, method call


/*
   Anonymous inner class 
     Is a simplified way to write inner classes. 
   Premise: Existence 1 Classes or interfaces 
     The class here can be concrete class or abstract class. 
   Format: 
    new  Class name or interface name (){
       Override method ;
    }
   What is the essence ?
     Yes 1 An anonymous object that inherits this class or a subclass that implements this interface. 
*/
interface Inter {
  public abstract void show();
  public abstract void show2();
}
class Outer {
  public void method() {
    Inter i = new Inter() { // Polymorphism 
      public void show() {
        System.out.println("show");
      }
      public void show2() {
        System.out.println("show2");
      }
    };
    i.show();// Yes 1 An anonymous object that inherits this class or a subclass that implements this interface. So you can call the method 
    i.show2();
  }
}
class InnerClassDemo6 {
  public static void main(String[] args) {
    Outer o = new Outer();
    o.method();
  }
}

Use of anonymous inner classes in development


/*
   Use of anonymous inner classes in development 
*/
interface Person {
  public abstract void study();
}
class PersonDemo {
  // Interface name as formal parameter 
  // What is needed here is not the interface, but the object of the implementation class of the interface 
  public void method(Person p) {
    p.study();
  }
}
// Implementation class 
class Student implements Person {
  public void study() {
    System.out.println(" Study hard , Make progress every day ");
  }
}
class InnerClassTest2 {
  public static void main(String[] args) {
    // Test 
    PersonDemo pd = new PersonDemo();
    Person p = new Student();
    pd.method(p);
    System.out.println("--------------------");
    // Use of anonymous inner classes in development 
    // The essence of an anonymous inner class is an anonymous object that inherits a class or a subclass that implements an interface 
    // Use up 1 Disappear every time 
    Person ss = new Person(){
      public void study() {
        System.out.println(" Study hard , Make progress every day ");
      }
    };
    pd.method(ss);
    // pd.method(new Person(){
      // public void study() {
        // System.out.println(" Study hard , Make progress every day ");
      // }
    // });
  }
}

Case


/*
   Case: 
     Complete the code as required 
      interface Inter { void show(); }
      class Outer { // Complement code  }
      class OuterDemo {
        public static void main(String[] args) {
           Outer.method().show();
         }
      }
       Require output in the console " HelloWorld " 
*/
interface Inter {
  void show();
  // Remember to have by default  public abstract
}
class Outer {
  // Complement code 
  public static Inter method() {
    // Subclass object  --  Subclass anonymous object 
    return new Inter() {
      public void show() {
        System.out.println("HelloWorld");
      }
    };
  }
}
class OuterDemo {
  public static void main(String[] args) {
    Outer.method().show();
    /*
      1:Outer.method() It can be seen that method() It should be Outer In 1 Static methods. 
      2:Outer.method().show() It can be seen that method() The return value of the method is 1 Objects. 
         And because of the interface Inter Among them 1 A show() Method , So I think method() The return value type of the method is 1 Interfaces. 
    */
  }
}

More readers interested in java related content can check the topics of this site: "Introduction and Advanced Tutorial of Java Object-Oriented Programming", "Tutorial of Java Data Structure and Algorithm", "Summary of Java Operating DOM Node Skills", "Summary of Java File and Directory Operation Skills" and "Summary of Java Cache Operation Skills"

I hope this article is helpful to everyone's java programming.


Related articles: