2021-03-13

Analysis of the 4 internal classes

First of all, we must first understand the main components of the java class.

Members of the class:

1. Attributes

普通属性
静态属性

2. Method

普通方法
静态方法

3. Constructor

4. Code block

普通代码块
静态代码块

5. Internal category:

	在一个类的内部在写一个类-内部类
    (1).普通成员内部类  普通属性  
    (2).静态成员内部类  静态属性    查看源码
	(3).局部内部类    局部变量
	(4).匿名内部类   匿名对象 参照  

The role of the internal class:
1. The function of the external class can be expanded and can not be accessed by the outside world
. 2. Indirect realization of multiple inheritance. Both the internal class and the external class inherit one class

(1). Ordinary member inner class:

Ordinary member inner class:
1. The outer class can only be modified with public default
2. The inner class can be modified by four permission modifiers
3. The inner class cannot have static attributes but there can be static constants
4. The inner class cannot have Static method
5. The resource attribute in the external class can be used directly in the internal class. Both static and non-static methods can be used
. 6. The resource of the internal class can be used externally. The
internal class object can be created for the non-static resource to access the
internal class name of the static resource. Attribute name
7. Cannot directly create internal class objects in static methods of external classes

8. The way to create internal class objects directly
externally External class name. Internal class name variable name = external class object. new internal class object();
Person.Inner inner = new Person().new Inner();

9. The naming rules of bytecode files generated after internal class compilation
External class name Internal class name Person Internal class name PersonThe unit class name P E R & lt S O n- Inner
interior also generates class files independent bytecode

(2). Static member inner class:

Static member inner class:
1. In the declaration method, write the static class class name { } in the outer class. 2. There can be static attributes in the inner class. 3. The non-static resources in the outer class cannot be used directly in the inner class. The outer class object needs to be created. in order to use 4. You can create an internal class object within a static method of the outer class can call the Central African static inner classes through internal resource class object to call the internal class name inside the class static resources 5. create an inner class object directly in the outer class outside of class Name. Inner class name Variable name = new Outer class name. Inner class name (); Variable name: inner class object Outer.Inner inner = new Outer.Inner(); 6. The inner class will generate independent bytecode file naming Method External class name $ internal class name 7. Will direct creation of an external class object trigger the initialization of a static internal class? If the resources in the internal class are not used , the initial recognition of the internal class will not be performed. If only the resources in the internal class are used It will not trigger the initialization of the external class














(3). Local inner class:

Local internal class: Refer to local variables
1. How to declare a local internal class
Create a new class in the method
2. The status of the local internal class is relatively low. What resources of the external class are used depends on the method.
Static methods can use the static resources in the external class.
Non-static The method can use all resources in the external class
3. Local internal classes cannot have static properties but can have static constants

4. To use the resources in the inner class, you need to execute the method of the outer class to create the object of the
inner class through the inner class object. Related resources

5. Local internal classes will also generate independent bytecode files. The naming method of bytecode files is
external class name $ serial number internal class name serial number starts from 1
Outer$1Inner
Outer$2Inner

6. When the method in the local internal class uses the local variable of the external class, the local variable will be automatically modified by final
jdk1.8 automatically added final

(4). Anonymous inner class:

Anonymous inner class: inner class
without a name
What does it do:
save code

How to create an anonymous inner class:

 new(){
    
    }
 new 接口(){
    
    }
    1.创建了一个没有名字的匿名子类
    2.创建了该匿名子类对象

Note:
1. An anonymous inner class is a local inner class without a name.
2. What resources can be used by the anonymous inner class depends on whether the method is static.
Static methods can only use static resources.
Non-static methods can use all resources
3. Anonymous internals The class uses the local variable of the method, this variable will be final modified
4. Anonymous internal class will also generate a separate bytecode file
Naming method External class name $ serial number
Test2$1.class
Test2$2.class
Test2$3.class

Example:

public class Test2 {
    
    
    static String  name = "Test2";
    int age  =10;

    public static void main(String[] args) {
    
    
        int num = 10;
        //创建了一个Person的匿名子类 并且完成了该匿名子类对象的创建
        new Person() {
    
    
        }.eat();
        new Person("李白", 20) {
    
    
        }.eat();

        new Animal() {
    
    
            @Override
            void eat() {
    
    
                System.out.println(name);
                System.out.println("小猫吃饭");
                System.out.println(num);
            }
        }.eat();

        new Fly() {
    
    
            @Override
            public void fly() {
    
    
                System.out.println("超人飞");
            }
        }.fly();


    }
}

interface Fly {
    
    
    void fly();
}

abstract class Animal {
    
    

    abstract void eat();
}

class Person {
    
    

    String name;
    int age;


    public void eat() {
    
    
        System.out.println(name + "Person eat");
    }

    public Person() {
    
    
    }

    public Person(String name, int age) {
    
    
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
    
    
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

Guess you like

Origin blog.csdn.net/qq_37698495/article/details/114764279