Chapter 18 object oriented advanced five - internal class details of JavaSE topic

Posted by beachdaze on Fri, 11 Feb 2022 21:37:17 +0100

1. Internal class overview

(1) Inner class: the inner part of a class is completely nested with another structure. Those nested are called inner classes, and those nested with other classes are called outer classes.

  • Internal class: the fifth largest member of our class;
    • Five members: attribute, method, constructor, code block and internal class;
  • The biggest feature of internal class: it can directly access private attributes, reflecting the inclusion relationship between classes;

(2) Basic grammar

class Outer{ //External class
    class Inner{ //Inner class
        // The Inner class serves only the Outer class and can be regarded as a special member of the Outer class
    }
}

class Other{ //Other external classes 
}

(3) Internal class classification

  • Defined in a local location of an external class (such as within a method)
    • Local internal class: there is a class name;
    • Anonymous internal class: no class name, key, important, important (say important things three times);
  • Defined on the member location of the external class:
    • Member inner class: no static modification;
    • Static inner class: use static modification;

2. Member inner class

  • Member inner class: defines the member position of the outer class

    • matters needing attention:
      • I. the inner class of a member can access all properties and methods of the outer class;
      • II. If an external class wants to access the properties and methods of a member's internal class, it must first instantiate the member's internal class;
      • III. static attributes and methods cannot be contained in the internal class of a member.
      • Ⅳ. Calling method: write method instantiation call
  • Syntax format

public class InnerClassTest {
    public class InnerClassA {    
    }
}
  • example
// 1. Member internal class definition
class Outer01{
    private  int n1 = 100;
   public String name = "Li Si";
   class Inneer{ // Member inner class
       public void say(){
           // Access to external classes: non static properties / methods
           System.out.println("n1 = "+n1 + "name = "+name);
       }
   }

   public void  t1(){
       // Using member inner classes
       Inneer inneer = new Inneer();
       inneer.say();
   }
}

// 2. main method call
public class hello {
    public static void main(String[] args) {
        Outer01 outer01 = new Outer01();
        outer01.t1();
    }
}
  • Operation results

3. Static inner class

  • Static inner class: add a static keyword to the inner class of the member

    • Scope: static inner classes can only access static member variables and methods (including private static) of external classes, which is the opposite of inner member classes
  • Syntax format

public class InnerClassTest {
    static class InnerClassA {    
    }
}
  • example
// 1. Member internal class definition
class Outer01{
   private static int n1 = 100;
   public static String name = "Li Si";
   class Inneer{ // Member inner class
       public void say(){
           // Access to external classes: non static properties / methods
           System.out.println("n1 = "+n1 + "name = "+name);
       }
   }

   public void  t1(){
       // Using member inner classes
       Inneer inneer = new Inneer();
       inneer.say();
   }
}

// 2. main method call
public class hello {
    public static void main(String[] args) {
        Outer01 outer01 = new Outer01();
        outer01.t1();
    }
}
  • Operation results

4. Local inner class

  • Local internal class: it is defined in the local location (method / code block) of the external class and has a class name;

    • 1. Definition location: in method / code block;
    • 2. Scope: in method / code block;
  • example

// 1. Local internal class definition
class Outer01{
    private  int n1 = 100;
    public void m1(){//External class method
        //1. The local inner class is defined in the local position of the outer class, usually inside the method;
        //2. Local inner class is still a class in essence;
        //3. The access modifier cannot be added, but it can be modified with final;
        //4. Local internal classes can directly access all members of external classes, including private ones;
        //5. Scope: only in the method or code block that defines it, Inner01 scope is limited to m1 method
        class Inner01{
            public void f1(){ //Inner class method
                System.out.println("n1=" + n1);
            }
        }
        //6. Local inner classes can inherit
        class Inner02 extends Inner01{
        }
        //7, the external class can create internal class objects in the method, and then call the internal class method.
        Inner02 i2 = new Inner02();
        i2.f1();
    }
}

// 2. main method call
public class hello {
    public static void main(String[] args) {
        Outer01 outer01 = new Outer01();
        outer01.m1();
    }
}
  • Operation results

5. Anonymous inner class

  • Business scenario: it is widely used in the underlying framework and future Android development. It is generally used to embed SDK data callback to listen for events;

  • Anonymous inner class: it is defined in the local location of the external class, such as in the method body, and there is no class name;

    • Meaning: anonymous inner class belongs to local inner class, and it is a local inner class without name. It is usually used with anonymous objects
  • Syntax format

new Class or interface(parameter list){
    Class body
};

(1) Anonymous inner class based on interface

  • Example: use the interface and create an object to use once
// 1. Create interface

interface IA{ // Interface
    public void cry();
}

// 2. Create an external class
class Dog {
    private int n1 = 10; //attribute
    public void  method(){
        //Create anonymous inner class
        IA dog = new IA(){
            @Override
            public void cry(){
                System.out.println("Anonymous internal class barking......");
            }
        };
		// Print run type
        System.out.println("Run type class name:"+dog.getClass());
        // Call the object method instantiated by the internal class
        dog.cry();
    }
}

// 3. The main method calls an external class
public class hello {
    public static void main(String[] args) {
       Dog dog = new Dog();
       dog.method();
    }
}
  • Operation results

  • The underlying implementation of anonymous inner classes
class Dog$1 implements IA(){
	@Override
	public void cry(){
		System.out.println("Anonymous internal class barking......");
	}
};

(2) Anonymous inner class based on class

  • example:
// 1. Create class

class IA{ // Create class
    public IA(String name){};  // constructor 
    public void test(){};       // method
}

// 2. Create an external class
class Dog {
    private int n1 = 10; //attribute
    public void  method(){
        //Create anonymous inner class
        IA ia = new IA("Zhang San"){
            @Override
            public void test(){
                System.out.println("Anonymous inner class overridden test method");
            }
        };
        // Print the name of the instantiated internal class
        System.out.println("IA Instantiated object name:"+ia.getClass());
        ia.test();
    }
}

// 3. main method call
public class hello {
    public static void main(String[] args) {
       Dog dog = new Dog();
       dog.method();
    }
}

  • Operation results

Topics: Java Back-end