Java Object Oriented Programming Part 4 - internal classes

Keywords: Java

Java Object Oriented Programming Part 4 - internal classes

1. Concept of inner class

When the definition of a class appears in the class body of another class, the class is called Inner, and the class in which the Inner class is located is called Outer.

Contents in class: member variable, member method, constructor, static member, constructor and static code block, internal class

2. Meaning of inner class

When the value of a class is only to serve a class separately, the class can be defined as the internal class in the served class, which can hide the implementation details of the class and easily access the private members of the external class without providing public get and set methods.

3. Classification of internal classes

3.1. Member internal class

Directly put the definition of one class in the class body of another class

Access modifier  class Class name of external class {
    Access modifier  class The class name of the inner class {
    	Class body of inner class;
    }
}
public class Outer {
    //Member variable
    private int outerNum = 1;
    //Internal and external repeated variables
    private int commonNum = 2;
    //Static member variable
    private static int staticOuterNum = 3;

    /**
     * Member method
     */
    public void outerMethod() {
        System.out.println("I'm an external class outerMethod method");
    }

    /**
     * Static method
     */
    public static void staticOuterMethod() {
        System.out.println("I'm an external class staticOuterMethod Static method");
    }

    class Inner{

        //Member variable
        private int innerNum = 1;
        //Internal and external repeated variables
        private int commonNum = 20;

        public void innerShow() {
            //When it conflicts with the external class, it directly refers to the property name, which is the member property of the internal class
            System.out.println("Internal commonNum:" + commonNum);
            //Internal classes access external properties
            System.out.println("outerNum:" + outerNum);
            //When it overlaps with the external class attribute name, you can use the external class name. this. Attribute name
            System.out.println("External commonNum:" + Outer.this.commonNum);
            System.out.println("staticOuterNum:" + staticOuterNum);
            //Methods to access external classes
            outerMethod();
            staticOuterMethod();
        }
    }

    public void outershow() {
        Inner inner = new Inner();
        inner.innerShow();
    }

    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.outershow();
    }
}

External class usage

public class Test {
    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.outershow();
        System.out.println("=======================");
        Outer.Inner inner = new Outer().new Inner();
        inner.innerShow();
    }
}
  • The inner class of a member can be any access modifier
  • There cannot be static information inside a member class
  • The inner class is also a class. It inherits, rewrites, overloads, and this and super can be used casually (it can solve the problem of multiple inheritance)
  • How the external class accesses the internal class information must be accessed after new
  • The inner class can directly use any information of the outer class. If the property or method conflicts, call the. this. Property or method of the outer class
  • Other classes need Outer.Inner inner = new Outer().new Inner();

3.2. Static internal class

The internal class decorated with the static keyword belongs to the class level

Access modifier  class Class name of external class {
    Access modifier  static class The class name of the inner class {
    	Class body of inner class;
    }
} 
public class Outer {
    //Member variable
    private int outerNum = 1;
    //Internal and external repeated variables
    private int commonNum = 2;
    //Static member variable
    private static int staticOuterNum = 3;

    static {
        System.out.println("Outer The static block of is executed");
    }

    /**
     * Member method
     */
    public void outerMethod() {
        System.out.println("I'm an external class outerMethod method");
    }

    /**
     * Static method
     */
    public static void staticOuterMethod() {
        System.out.println("I'm an external class staticOuterMethod Static method");
    }

    static class Inner{
        static {
            System.out.println("Outer.Inner The static block of");
        }

        //Member variable
        private int innerNum = 1;
        //Internal and external repeated variables
        private int commonNum = 20;

        private static int staticInnerVariable = 30;

        /**
         * Member method
         */
        public void innerShow() {
            System.out.println("innerNum:" + innerNum);
            System.out.println("Internal commonNum:" + commonNum);
            System.out.println("staticOuterNum:"+staticOuterNum);
            staticOuterMethod();
        }

        /**
         * Static method
         */
        public static void innerStaticShow() {
            //When called, the Outer class will be loaded first
            staticOuterMethod();
            System.out.println("staticOuterNum"+staticOuterNum);
        }
    }
    public static void callInner() {
        System.out.println("staticInnerNum"+Inner.staticInnerVariable);
        Inner.innerStaticShow();
    }

    public static void main(String[] args) {
        callInner();
    }
}

External use static internal classes

public class Test {
    public static void main(String[] args) {
        Outer.callInner();
        System.out.println("====================");
        Outer.Inner.innerStaticShow();
        System.out.println("====================");
        Outer.Inner inner = new Outer.Inner();
        inner.innerShow();
    }
}
  • Static inner classes can contain arbitrary information
  • Methods of static inner classes can only access information associated with static of outer classes
  • Use external class. Internal class reference = new external class. Internal class (); Then it is called with reference. Member information (properties, methods).
  • Access the static information of the internal class, and directly the external class. Internal class. Static information

3.3 local internal class

Directly put the definition of a class inside the method body

Access modifier  class Class name of external class {
    Access modifier return value type member method name (formal parameter list) {
        class The class name of the inner class {
        	Class body of inner class;
        }
    }
}
public class Outer {
    //Member variable
    private int outerNum = 1;
    //Internal and external repeated variables
    private int commonNum = 2;
    //Static member variable
    private static int staticOuterNum = 3;

    static {
        System.out.println("Outer The static block of is executed");
    }

    /**
     * Member method
     */
    public void outerMethod() {
        boolean flag=false;
        System.out.println("I'm an external class outerMethod method");
        class Inner{

            //Member variable
            private int innerNum = 1;
            //Internal and external repeated variables
            private int commonNum = 20;

            /**
             * Member method
             */
            public void innerShow() {
                System.out.println("flag:" + flag);
                System.out.println("innerNum:" + innerNum);
                System.out.println("Internal commonNum:" + commonNum);
                System.out.println("External commonNum:" + Outer.this.commonNum);
                System.out.println("staticOuterNum:"+staticOuterNum);
                staticOuterMethod();
            }
        }
        new Inner().innerShow();
    }

    /**
     * Static method
     */
    public static void staticOuterMethod() {
        System.out.println("I'm an external class staticOuterMethod Static method");
    }

    public static void main(String[] args) {
        new Outer().outerMethod();
    }
}
  • Local internal class, cannot have access modifier before class
  • Local inner classes can only be used inside methods
  • Cannot create static information
  • Local variables and parameters within a method can be accessed directly, but cannot be changed
  • You can access any information of external classes at will
  • Local variables used in methods of anonymous inner classes must be defined as final

3.4. Anonymous inner class

An inner class without a name

Interface/The parent type references the variable name = new Interface/Parent class type() { Method override };

Interface

public interface Animal {
    void speak();
}

use

public class Outer {
    public static void main(String[] args) {
		//Interface / parent type reference variable name = new interface / parent type () {override of method};
        Animal animal=new Animal() {
            @Override
            public void speak() {
                System.out.println("cat ");
            }
        };
        animal.speak();
    }
}

  • Anonymous inner classes do not have access modifiers
  • When using anonymous inner classes, the class after new must first exist. Secondly, we need to override one or some methods of the class after new
  • Anonymous inner classes have the same restrictions as local inner classes when accessing method parameters
  • Anonymous inner class has no constructor

Posted by natgopal on Thu, 02 Sep 2021 21:31:30 -0700