当前位置:网站首页>The difference between this and super and their respective functions

The difference between this and super and their respective functions

2022-06-25 12:33:00 mercenary's L

1. Attribute distinction :
this Access the properties in this class , If this class does not have this property, continue to search from the parent class .super Access properties in the parent class .
2. Differences in methods :
this Access the methods in this class , If this class does not have this method, continue to search from the parent class .super Access methods in the parent class .
3. Differences in structure :
this Call this class to construct , Must be placed on the first line of the constructor .super Call the parent class construct , Must be placed on the first line of the subclass constructor .
4. Other differences :
this Represents the current object .super Cannot represent the current object
A、this. Variables and super. Variable
this. Variable The variable of the current object ;
super. Variable The variables in the parent class are called directly .
B、this( Parameters ) and super( Parameters ) Method
this( Parameters ) call ( forward ) Is the constructor in the current class ;
super( Parameters ) Used to confirm which constructor in the parent class to use .

Two 、 Be careful :
1) When initializing a subclass that has a parent class , The constructor of the parent class also executes , And takes precedence over the constructor of the subclass ; Because the first line in the constructor of each subclass has a default implicit statement super();
2)this() and super() Can only be written on the first line of the constructor ;
3)this() and super() Cannot exist in the same constructor . First of all ,this() and super() Must be written on the first line of the constructor ; second ,this() Statement calls another constructor of the current class, and there must be a constructor of the parent class in this other constructor , Reuse super() Call the constructor of the parent class again , It is equivalent to calling the constructor of the parent class twice , The compiler will not pass ;
4)this and super Cannot be used for static Decorated variable , Method , Code block ; because this and super All refer to the object ( example )


One 、this

  this It's an object of its own , Represents the object itself , It can be understood as : A pointer to the object itself .

  this The usage of is in java In general, it can be divided into 3 Kind of :

  1、 A common direct quote

  this It points to the current object itself .

  2、 Parameter and member name are the same , use this To distinguish between :

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

  3. Reference the constructor of this class

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

Two 、super

  super Can be understood as pointing to their own super ( Father ) A pointer to a class object , And this superclass refers to the nearest parent class .

  super There are also three uses :

  1、 A common direct quote

   And this similar ,super Equivalent to a reference to the parent of the current object , In this way, you can use super.xxx To reference the members of the parent class .

  2、 When a member variable or method in a subclass has the same name as a member variable or method in a parent class , use super Distinguish

class Person{
    protected String name;
 
    public Person(String name) {
        this.name = name;
    }
 
}
 
class Student extends Person{
    private String name;
 
    public Student(String name, String name1) {
        super(name);
        this.name = name1;
    }
 
    public void getInfo(){
        System.out.println(this.name);      //Child
        System.out.println(super.name);     //Father
    }
 
}
public class Test {
 
    public static void main(String[] args) {
       Student s1 = new Student("Father","Child");
       s1.getInfo();
 
    }
 
}

  3、 Reference parent constructor

  super( Parameters ): Call one of the constructors in the parent class ( Should be the first statement in the constructor ).

  this( Parameters ): Call another form of constructor in this class ( Should be the first statement in the constructor ).

3、 ... and 、super and this Similarities and differences :

  • super:  It references members in the immediate parent class of the current object ( It is used to access member data or functions hidden in the direct parent class , When the base class and the derived class have the same member definition, such as :super. Variable name     super. Member function by name ( Actual parameters )
  • this: It represents the current object name ( It is easy to produce ambiguity in the procedure , You should use this To indicate the current object ; If the parameter of a function has the same name as the member data in the class , In this case, it is necessary to this To indicate the member variable name )
  • super() and this() similar , The difference is that ,super() The method of calling the parent class in the subclass ,this() Call other construction methods of this class in this class .
  • super() and this() All need to be placed in the first line of the construction method .
  • Although it can be used this Call a constructor , But you can't call two .
  • this and super Cannot appear in a constructor at the same time , because this It's bound to call other constructors , Other constructors must have super The existence of sentences , So there are the same statements in the same constructor , You lose the meaning of the sentence , The compiler won't pass either .
  • this() and super() It's all about the object , therefore , None of them can be in static Use in the environment . Include :static Variable ,static Method ,static Sentence block .
  • essentially ,this Is a pointer to this object ,  However super It's a Java keyword .
原网站

版权声明
本文为[mercenary's L]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200530000304.html

随机推荐