Java: this keyword and super keyword

statement

1) Part of the content of this article is compiled from the information on the Internet. If you accidentally infringe on everyone's rights, you can still contact the blogger to delete it.

2) The blogger is Mengxin on the road. If there is any inappropriateness in the article, please point it out and make progress together, thank you.

this keyword

The description of this in the book:
this represents a reference to an object, which points to the object that is executing the method. In particular, in the construction method, when calling other construction methods through the this keyword, it must be placed on the first line, otherwise the compiler will report an error. And in the construction method, other construction methods can only be called once through this.

this is an object of itself, which represents the object itself, which can be understood as: a pointer to the object itself.

The usage of this can be roughly divided into three types in Java:

1. Ordinary direct reference to
this is equivalent to pointing to the current object itself.

2. Participating members have the same name, use this to distinguish

Instance

class Person {
    
    
    private int age = 10;
    public Person(){
    
    
    System.out.println("初始化年龄:"+age);
}
    public int GetAge(int age){
    
    
        this.age = age;
        return this.age;
    }
}
 
public class test1 {
    
    
    public static void main(String[] args) {
    
    
        Person Harry = new Person();
        System.out.println("Harry's age is "+Harry.GetAge(12));
    }
}

operation result

初始化年龄:10
Harry's age is 12

3. Reference constructor
this (parameter): call another form of constructor in this class (should be the first statement in the constructor).

Depending on where this appears, this is used as the default reference of an object in two situations:

① Reference the object being initialized by the constructor in the constructor. Note: this is the object being initialized. You can call attribute fields.

②Reference the object calling the method in the method. Note: this is the object that calls this method, and the method of this method can be called.

this usage

  1. Call this class method
  2. Call this class attribute
  3. Call other construction methods of this class
  4. Call the method of the same name of the parent class or other designated classes, which is born to avoid ambiguity
  5. Hidden call, without specifying the scope, java will look up the variable or method in the whole class scope

super keyword

The super keyword in java is a reference variable used to refer to the parent object. If the constructor does not explicitly call the parent class's constructor, the compiler will automatically add a default super() method call to it. If the parent class has no default no-argument constructor, the compiler will report an error, and the super() statement must be the first clause of the constructor.

Super can be understood as a pointer to one's own super (parent) class object, and this super class refers to the closest super class to oneself.

There are three uses of super:

1. Ordinary direct reference
is similar to this, super is equivalent to pointing to the parent class of the current object, so you can use super.xxx to refer to the members of the parent class.

2. The member variable or method in the subclass has the same name as the member variable or method in the parent class

Instance

class Country {
    
    
    String name;
    void value() {
    
    
       name = "China";
    }
}
  
class City extends Country {
    
    
    String name;
    void value() {
    
    
        name = "Shanghai";
        super.value();      //调用父类的方法
        System.out.println(name);
        System.out.println(super.name);
    }
  
    public static void main(String[] args) {
    
    
       City c=new City();
       c.value();
    }
}

operation result

Shanghai
China

As you can see, both the method of the parent class and the variables of the parent class are called here. If the parent class method value() is not called, but only the parent variable name is called, the parent class name value is the default value null.

3. Reference constructor
super (parameter): call a certain constructor in the parent class (it should be the first statement in the constructor).

Guess you like

Origin blog.csdn.net/weixin_46263782/article/details/107604884