Java learning road-internal classes

Java learning road-internal classes

Overview

In Java, a class can be defined in another class or in a method. Such a class is called an inner class.

Inner classes in a broad sense generally include these four types: member inner classes, local inner classes, anonymous inner classes, and static inner classes.

One, member inner class

Define a class B in class A, then class A is called an outer class, and class B is called a member inner class.

The member inner class can unconditionally access all the member attributes and member methods of the outer class (including private members and static members), but the outer class wants to access the members of the member inner class must first create an object of the member inner class, and then point to this object To access the properties of the inner class.

public class Demo {
    
    
    public static void main(String[] args)  {
    
    
        A a1 = new A();
        a1.testA();
    }
}

class A {
    
    
    private int x = 0;
    
    // 内部类B
    class B {
    
    
        int y=1;
        
        // 访问外部类的属性
        public void testB() {
    
    
            System.out.println(x);
        }
    }

    public void testA() {
    
    
        B b1 = new B();
        b1.testB();
        // 访问内部类的属性
        System.out.println(b1.y);
    }
}

When the member inner class has a member variable or method with the same name as the outer class, the hidden phenomenon will occur, that is, the members of the member inner class are accessed by default. If you want to access a member of the same name of an external class, you need to access it in the following form:

外部类.this.成员变量
外部类.this.成员方法

Example

public class Demo {
    
    
    public static void main(String[] args)  {
    
    
        A a1 = new A();
        a1.testA();
    }
}

class A {
    
    
    String name = "外部类";

    // 内部类B
    class B {
    
    
        String name = "内部类";

        // 访问外部类的属性
        public void test() {
    
    
            System.out.println(name);
            System.out.println(A.this.name);
        }
    }

    void testA() {
    
    
        B b1 = new B();
        b1.test();
    }
}

The member inner class is dependent on the outer class, that is, if you want to create an object of the member inner class, the premise is that there must be an object of the outer class.

public class Demo {
    
    
    public static void main(String[] args)  {
    
    
        // 初始化一个 A 对象
        A a1 = new A();

        // 方式1
        A.B b1 = a1.new B();

        // 方式2
        A.B b2 = a1.getBInstance();
    }
}

class A {
    
    
    private B b = null;

    public B getBInstance() {
    
    
        if(b == null)
            b = new B();
        return b;
    }

    // 内部类B
    class B {
    
    
        String name = "内部类";
    }
}

Two, local internal class

A local inner class is a class defined in a method or a scope. Access to local inner classes is limited to methods or scopes.

Local inner classes are similar to local variables and cannot have public, protected, private, and static modifiers.

public class Demo {
    
    
    public static void main(String[] args)  {
    
    
        class A {
    
    
            String name = "局部内部类";
        }

        A a1 = new A();
        System.out.println(a1.name);
    }
}

Three, anonymous inner class

As long as a class is abstract or an interface, the methods in its subclasses can be implemented using anonymous inner classes.

An anonymous inner class is the only class without a constructor. Because it has no constructor, the scope of use of anonymous inner classes is limited. Generally speaking, anonymous inner classes are used to inherit other classes or implement interfaces.

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        Person p = new Person() {
    
    
            public void run() {
    
    
                System.out.println("Eating");
            }
        };
        p.run();
    }
}

abstract class Person {
    
    
    public abstract void run();
}

Four, static inner class

A static inner class is also a class defined in another class. This class is a static class and does not need to rely on the instantiation of an outer class to make calls.

public class Demo {
    
    
    public static void main(String[] args)  {
    
    
        A.B b1 = new A.B();
        b1.testB();
    }
}

class A {
    
    
    // 内部类B
    static class B {
    
    
        String name = "静态内部类";

        // 访问外部类的属性
        public void testB() {
    
    
            System.out.println(name);
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_43580193/article/details/112642106