Summary of the use of this and super keywords in Java


1. The difference between the use of this and super

the usage of this Number of lines of relevant code
1. In this class 区分同名的成员变量和局部变量(call the member variables of this class) 50、52
2. In a class with inheritance relationship, 区分子类和父类的同名方法(this calls the member method of this class) 57
3. In the construction method of this class调用其它构造方法 20、46
super Number of lines of relevant code
1. In the parent class and the child class 区分同名的成员变量(call the member variables of the parent class) 52、54
2. In the parent class and the child class 区分同名的成员方法(super calls the member method of the parent class) 59
3. In the subclass construction method调用父类构造方法 39
public class TestThisAndSuper {
    
    
    public static void main(String[] args) {
    
    
        //调用Zi类的有参构造方法
        Zi z = new Zi(33);
    }
}

class Fu{
    
    //父类
    //Fu类的 成员变量
    int num = 11;

    //Fu类的 无参构造方法
    public Fu(){
    
    
        System.out.println("这是Fu类无参构造方法");
    }

    //Fu类的 有参构造方法
    public Fu(int num){
    
    
        //调用 Fu类的无参构造方法
        this();
        System.out.println("这是Fu类有参构造方法");
    }

    //Fu类的 成员方法
    public void method(){
    
    
        System.out.println("这是Fu类成员方法");
    }
}


class Zi extends Fu{
    
    //子类

    //Zi类的 成员变量
    int num = 22;

    //Zi类的 无参构造方法
    public Zi(){
    
    
        //调用 Fu类的有参构造方法
        super(20);
        System.out.println("这是Zi类无参构造方法");
    }
    //Zi类的 有参构造方法
    public Zi(int num){
    
    

        //调用 Zi类的无参构造方法
        this();
        System.out.println("这是Zi类有参构造方法");

        //打印 局部变量(构造方法的参数)
        System.out.println("Zi类中的局部变量:"+num+"(构造方法的参数)");
        //打印 Zi类中的成员变量
        System.out.println("Zi类中的成员变量:"+this.num);
        //打印 Fu类的成员变量
        System.out.println("Fu类中的成员变量:"+super.num);

        //调用 Zi类中的method方法
        this.method();
        //调用 Fu类中的method方法
        super.method();

    }

    //重写 Fu类的method方法
    @Override
    public void method(){
    
    
        System.out.println("这是Zi类成员方法");
    }

}

Insert picture description here


After reading this sentence, read on:Constructor is not a memberThe constructor is used to initialize the object

2. Why this() and super() must be in the first line of the constructor

1. The 保证数据加载的完整性construction method also contains code (such as assignment and conditional judgment, etc.), after all, the construction method is also called a method.
2. For inheritance, the prerequisite for the subclass to get all the members of the parent class is that the parent class must be initialized first when creating the subclass object.
Simple understanding,Is to ensure the integrity of the initialization (initialization is completed and then perform other operations), to maintain the rationality of the code logic and structure

3. Why can't use this and super keywords in static methods

1. this和super都是代表对象的引用The life cycle of a static method is the same as the life cycle of the class it is in. The object may not exist after the static method is loaded.
2. 静态方法优先于对象的存在If you use the this and super keywords in a static method, it is possible that the object is used without being defined.
in conclusion,The static method is there but the object does not necessarily exist, so this and super are not allowed to be used in static methods.

4. Why should we call the constructor in the constructor

1. can be inWrite code in the constructor to perform some operations(For example, assignment and conditional judgment, etc.), after all, construction methods are also called methods.
2. For inheritance,The child class does not inherit from the parent class构造方法, The subclass will only inherit the member attributes and member methods of the parent class. In order to call the member attributes and member methods of the parent class, the subclass must call the super() or its overloaded form in the constructor of the parent class.
【note】
The subclass constructor calls super() by default.
If several constructor calls constitute a loop, the compiler will not compile and pass!

5. How to call the construction method of the two parent classes

The problem that can be solved with code does not need text ↓ ↓ ↓

class Fu{
    
    
    public Fu(){
    
    
    	System.out.println("这是Fu类无参构造方法");
    }
    public Fu(int num){
    
    
        //调用 Fu类的无参构造方法
        this();
        System.out.println("这是Fu类有参构造方法");
    }
}

class Zi extends Fu{
    
    
    public Zi(){
    
    
        //调用 Fu类的有参构造方法
        super(20);
        System.out.println("这是Zi类无参构造方法");
    }
}

You see, in the parameter construction method of the parent class, use this() to call the parameterless construction method, and call the parameter construction method of the parent class in the subclass construction method to achieve it. Just create the subclass object and use it.
【to sum up】
Due to the limitation of the compiler, a construction method can only use this() or super() once, only借助另外一个构造方法来实现多个构造方法的调用

6. Why can't call the constructor in the member method

Just like this() and super() can only be in the first line of the construction method , just for 对象的初始化一气呵成.

Guess you like

Origin blog.csdn.net/weixin_44580492/article/details/106245423