The difference between this and super keywords


1. The difference between this and super keywords 1. Why use super?
The child class wants to access the parent class method or variable.
The subclass calls the parent class's construction method.

this :
this keyword can only be used in the method body of a method, and represents a reference to "the object that calls the method". When an object is created, the Java Virtual Machine (JVM) will assign a pointer to the object that refers to itself, and the name of this pointer is this. Therefore, this can only be used in non-static methods in classes, and this must not appear in static methods and static code blocks.

super : The
super keyword is similar to this. It makes the member variables and member methods of the hidden parent class visible, or it is used to refer to the member variable and member methods of the hidden parent class.

Two, usage scenarios:
1. Use super and this to call member variables and methods

Sometimes you will encounter member variables or methods in a subclass with the same name as a member variable or method in the parent class. Because the member variable or method name of the subclass has a high priority, the member variable or method of the same name in the subclass hides the member variable or method of the parent class, so super is needed. I can’t read the code, try to understand it myself

class Country{
    
    
    String name;
    void value() {
    
    
        name = "china";
    }
}
public class City extends Country{
    
    
    String name;
    void value() {
    
    
        name = "Xian";
        super.value();
        //不调用此方法时,super.name返回的是父类的成员变量的值null
        System.out.println(name);
        System.out.println(super.name);
    }
    public static void main(String[] args) {
    
    
        City c=new City();
        c.value();
    }
}
//运行结果:
//Xian
//china

In order to refer to the member variable name and method value() of the parent class in the subclass, super, super.name and super.value() are used in the code. If super.value() is not called, super.name returns the default value of the member variable of the parent class to null. When calling this method, the super.value() method assigns the member variable name to China, and then uses super.name to call the parent class The value of the member variable. In addition, it should be noted that super.name calls the value of the member variable of the parent class .

class Country{
    
    
    String name="shanxi";
    String value(String name) {
    
    
        name = "china";
        return name;
    }
}

public class City extends Country{
    
    
    String name;
    String value(String name) {
    
    
        name = "Xian";
        super.value("失败");
        //不调用此方法时,super.name返回的是父类的成员变量的值null
        System.out.println(name);
        System.out.println(super.name);
        return name;
    }
    public static void main(String[] args) {
    
    
        City c=new City();
        c.value("成功");
    }
//Xian
//shanxi

At this time, the value returned by super.name is the value shanxi of the parent class member variable, and the super.value() method at this time does not work.

2. Use super and this to call the constructor


class Person{
    
    
    public static void prt(String s){
    
    
        System.out.println(s);
    }
    Person(){
    
    
        prt("A Person");
    }
    Person(String name){
    
    
        prt("A Person name is:"+name);
    }
}


public class Chinese extends Person{
    
    
    Chinese(){
    
    
        super();//调用父类的无参构造方法(1)
        prt("A chinese");//(4)

    }
    Chinese(String name){
    
    
        super(name);//调用父类具有相同形参的构造方法(2)
        prt("his name is:"+name);
    }
    Chinese(String name,int age){
    
    
        this(name);//调用当前子类中具有相同形参的构造方法(3)
        prt("his age is:"+age);

    }

    public static void main(String[] args) {
    
    
        Chinese cn=new Chinese();
        cn=new Chinese("zhiwen");
        cn=new Chinese("zhiwen",38);

    }

//A Person
//A chinese
//A Person name is:zhiwen
//his name is:zhiwen
//A Person name is:zhiwen
//his name is:zhiwen
//his age is:38

In this code, this and super are no longer connected to a method or member with "." as before, but are directly followed by appropriate parameters, so its meaning also has a new change. Adding a parameter after super indicates the construction method used to call the parameter of the same form in the parent class , such as (1) and (2). Adding a parameter after this means that the constructor of the current class with the same formal parameters is called , as in (3). Of course, in the various overloaded construction methods of Chinese, the general usage of this and super can still be used, such as (4), you can replace it with "this.prt" (because it inherits the prt in the parent class Method) or use "super.prt" (because it is a method in the parent class and can be accessed by subclasses), he can still run, but this seems a bit superfluous.

3. If you want to refer to super in the subclass's construction method, you must put super in the first line of the method

class Base{
    
    
    Base(){
    
    
        System.out.println("Base");
    }
}
public class Checket extends Base{
    
    
    Checket(){
    
    
        super();//调用父类的构造方法,一定要放在这个方法的首个语句
        System.out.println("Checket");
    }

    public static void main(String[] args) {
    
    
        Checket c=new Checket();

    }
//Base
// Checket
}

If you want to use super to call the method of the parent class structure, but it is not placed in the first line, then the statement before super must be a statement that you want to complete certain behaviors , but use super to call the structure of the parent class Method, then all the modifications made before are back to the previous, that is, it has become the construction method of the parent class again.

Summary :
1. Can not appear in the same constructor at the same time to call other constructors
super (parameter): call a certain constructor in the parent class, it should be the first statement in the constructor.
this (parameter): Invoke another form of construction method in this class, which should be the first statement in the construction method.
this() and super() cannot appear in the same construction method at the same time, because this will inevitably call other construction methods, and there must be super statements in other construction methods, and the same statement in the same construction method will be lost If the meaning of the statement is changed, the compilation will not pass.
2. The difference between calling the constructor:
super: refers to the members in the direct parent class of the current object, used to access the hidden member variables or methods in the direct parent class. It is used when the parent class and the subclass have the same members, such as super. variable name or super. member method name (actual parameter).
this: represents the name of the current object, where ambiguity is likely to occur in the program, this should be used to indicate the current object. If the shape of the method participates in the member variable of the class with the same name, then this is required to indicate the member variable name.
3. The prerequisites are different: the
super keyword must have inheritance before it can be used; the
this keyword can be used without inheritance.
4. Others:
calling super() must be written in the first line of the subclass construction method, otherwise the compilation will not pass. The first statement of the construction method of each subclass is an implicit call to super(). If
there is no such construction methodin the parent class, anerror will be reported during compilation.

Both this and super refer to objects, so neither can be used in static methods.

Guess you like

Origin blog.csdn.net/m0_46551861/article/details/112261242