java internal class summary

[README]

1. This paper summarizes four internal classes of Java, including

  • Member inner class: a non static class defined inside an outer class; Internal classes of members cannot exist independently, such as composition in UML;
  • Static internal class: a static class defined inside an external class; Static classes are closely related to external classes, but they can exist independently, such as aggregation in UML;
  • Local inner class: an inner class defined inside a method; Its scope is within the method;
  • Anonymous inner class: as the name suggests, an inner class without a name is usually used to implement an interface without a custom constructor;;

2. This paper gives the relevant test cases;

3. Functions of internal classes

  • 1. The internal class has its own implementation, which is helpful for the external class to realize multi inheritance; Multiple internal classes are defined in an external class, and each internal class inherits a parent class to realize multiple inheritance in disguise;
  • 2. Improve code cohesion;

[1] Introduction to internal classes

[1.1] member internal class

1) Member inner class: a non static class defined inside an outer class; Internal classes of members cannot exist independently;

2) Code structure

/**
 * @Description Member inner class
 * @author xiao tang
 * @version 1.0.0
 * @createTime 2021 November 21
 */
public class Topic78 {
    private String name;
    public Topic78(String name) {
        this.name = name;
    }
    // Member inner class
    public class MemberInnerClass {
        private int age;
        public MemberInnerClass(int age) {
            this.age = age;
        }
        // Inside the external class definition, use the external class. this pointer to point to the reference to the external class object
        public String hello() {
            return "hello " + Topic78.this.name;
        }
    }
}

3) Test case

public class MemberInnerClassTest {
    public static void main(String[] args) {
        Topic78 topic78 = new Topic78("lisi");
        // Create member inner class
        // In the external class definition, use the external class object. new pointer to point to the reference to the external class object
        Topic78.MemberInnerClass memberInnerClass = topic78.new MemberInnerClass(18);
        System.out.println(memberInnerClass.hello()); // hello lisi
    }
}

Note: the inner class of the member uses two pointers: the outer class object. new and the outer class. this;

  1. External class object. new pointer: refers to a reference to an external class object; (used when creating internal class objects)
  2. External class. this pointer: refers to a reference to an external class object; (used when the inner class uses the properties of the outer class object)

[1.2] static internal class

1) Definition: static internal class, static class defined inside external class; Static classes are closely related to external classes, but they can exist independently, such as aggregation in UML;

2) Code structure

/**
 * @Description Static inner class
 * @author xiao tang
 * @version 1.0.0
 * @createTime 2021 November 21
 */
public class Topic77 {
    /**
     * @description Static inner class
     * @author xiao tang
     * @date 2021/11/21
     */
    public static class StaticInnerClass {
        String name;
        public StaticInnerClass(String name) {
            this.name = name;
        }
        public void sayHello() {
            System.out.println("hello " + name);
        }
    }
}

3) Test case:;

/**
 * @Description TODO
 * @author xiao tang
 * @version 1.0.0
 * @createTime 2021 November 21
 */
public class StaticInnerClassTest {
    public static void main(String[] args) {
        Topic77.StaticInnerClass staticInnerClass = new Topic77.StaticInnerClass("zhangsan");
        staticInnerClass.sayHello();  // hello zhangsan
    }
}

[1.3] local internal class

1) Definition: local internal class, internal class defined inside the method;

  • Its scope: the current method block and all members accessing the external class; But it cannot be defined and used outside the current method;

2) Code structure

/**
 * @Description Local inner class
 * @author xiao tang
 * @version 1.0.0
 * @createTime 2021 November 21
 */
public class LocalInnerClassTest {
    private String name;
    public LocalInnerClassTest(String name) {
        this.name = name;
    }
    public void sayHello() {
        // Define a local class inside a method
        // Its scope: the current method block and all members accessing the external class; (it cannot be defined and used outside the current method)
        class LocalInnerClass {
            public String hello() {
                return "hello " + name;
            }
            public String nice2MeetYou() {
                return "nice to meet you";
            }
        }
        LocalInnerClass localInnerClass = new LocalInnerClass();
        System.out.println(localInnerClass.hello());
        System.out.println(localInnerClass.nice2MeetYou());
    }
    
    public static void main(String[] args) {
        new LocalInnerClassTest("lisi").sayHello();
    }
}

3) Test case:

ditto;

hello lisi
nice to meet you

[1.4] anonymous inner class

1) Definition: anonymous internal class. As the name suggests, an internal class without a name is usually used to implement an interface without a user-defined constructor;

2) Code structure: implement Runnable interface by concurrent programming

/**
 * @Description Anonymous Inner Class 
 * @author xiao tang
 * @version 1.0.0
 * @createTime 2021 November 21
 */
public class AnonymousInnerClassTest {
    public static void main(String[] args) {
        // The Runnable interface is implemented through anonymous internal classes to simplify the code
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("hello world");
            }
        };
        runnable.run();
    }
}

Tags: Java

Posted on Sat, 20 Nov 2021 21:47:17 -0500 by jjjamie