Inner class concept

Posted by Revan on Thu, 27 Jan 2022 10:32:39 +0100

Classification: anonymous internal classes are the focus

Member inner class

Member internal class definition format:

Modifier  class External class name{

        Modifier  class Internal class name{

                //Method body

        }

        //Method body

}

Indirect method: in the method of the external class, use the internal class, and then main just calls the method of the external class.

Direct method: Formula

External class name Internal class name object name = new external class name () New internal class name ();

Outer.Inner duixiang = new Outer().new Inner();
//If duplicate names occur, the format is: external class name this. External class member variable name
public class Outer{
    int num = 10;//External class member variable
    public class Inner{
        int num = 20;
        public void methodInner(){
            int num = 30;
            System.out.println(num);//30. Local variable, proximity principle
            System.out.println(this.num);//20. Member variables of internal classes
            System.out.println(Outer.this.num);//10. Member variables of external classes
        }
    }
}

Local inner class (including anonymous inner class)

  1. If a class is defined inside a method, it is a local inner class. "Local" can only be used by the current method. It cannot be used outside this method.

    Define format:

    Modifier  class External class name{
    
            Modifier return value type external class method name (parameter list){
                class Local internal class name{
    
                }
    
            }
    
    }

    If you want the method to access the local variable of the method, the local variable must be "valid final", which can be omitted from java 8.

    reason:

The new object is in the heap.

Local variables follow the method and are on the stack.

After the method is run, it is immediately out of the stack, and the local variables disappear immediately.

The new object will continue to exist in the heap until the garbage collection disappears.

Anonymous Inner Class


If the implementation class of the interface (or the subclass of the parent class) only needs to be used once,
In this case, you can omit the definition of this class and use anonymous inner class instead.

Definition format of anonymous inner class:
Interface name object name = new interface name (){
/ / override all abstract methods
};

For the format "new interface name () {...}" To parse:
1. new represents the action of creating an object
2. The interface name is the interface that the anonymous inner class needs to implement
3. {...} This is the content of the anonymous inner class

In addition, pay attention to several issues:
1. Anonymous internal classes can only be used once when creating objects.
If you want to create an object multiple times and the content of the class is the same, you need to use a separately defined implementation class.
2. Anonymous objects can only be called once when [calling method].
If you want the same object to call multiple methods, you must give the object a name.
3. Anonymous inner class omits [implementation class / subclass name], but anonymous object omits [object name]
Emphasize: anonymous inner class and anonymous object are not the same thing!!!

public class DemoMain {

    public static void main(String[] args) {

        // Use anonymous inner classes, but not anonymous objects. The object name is called objA
        MyInterface objA = new MyInterface() {
            @Override
            public void method1() {
                System.out.println("Anonymous inner class implements the method! one hundred and eleven-A");
            }

            @Override
            public void method2() {
                System.out.println("Anonymous inner class implements the method! two hundred and twenty-two-A");
            }
        };
        objA.method1();
        objA.method2();
        System.out.println("=================");

        // The anonymous inner class is used, and the object name is omitted, which is also an anonymous object
        new MyInterface() {
            @Override
            public void method1() {
                System.out.println("Anonymous inner class implements the method! one hundred and eleven-B");
            }

            @Override
            public void method2() {
                System.out.println("Anonymous inner class implements the method! two hundred and twenty-two-B");
            }
        }.method1();
        // Because the anonymous object cannot call the second method, you need to create another anonymous object of the anonymous inner class
        new MyInterface() {
            @Override
            public void method1() {
                System.out.println("Anonymous inner class implements the method! one hundred and eleven-B");
            }

            @Override
            public void method2() {
                System.out.println("Anonymous inner class implements the method! two hundred and twenty-two-B");
            }
        }.method2();
    }

}


public interface MyInterface {

    void method1(); // Abstract method

    void method2();
}

public class MyInterfaceImpl implements MyInterface {
    @Override
    public void method1() {
        System.out.println("Implementation class overrides the method! one hundred and eleven");
    }

    @Override
    public void method2() {
        System.out.println("Implementation class overrides the method! two hundred and twenty-two");
    }
}


 

tip: permission modifier:

public > protected > (default) > private

When defining a class, permission modifier rules:

  1. External class: public /(default)
  2. Member inner class: all
  3. Local inner class: no modifier can be written
  4. For internal use and external use, at will; For external use, internal class objects are required.             

Topics: Java