Summary of super keyword and this keyword

this

The this keyword represents a reference to the current object and can be used in three ways:

1. Ordinary direct reference, used inside the method

2. Distinguish between function parameter names and class member variable names

3. Call the constructor of this class

super 

The super keyword is a reference representing the parent class space (the parent class here refers to the parent class closest to the child class, the principle of proximity)

There are two usages:

1. When there are members of the same name in the subclass and the superclass, the members of the subclass are accessed by default in the subclass, and the members of the superclass can be accessed through the super keyword

2. When creating a subclass object, the constructor without parameters of the parent class will be called by default, and the constructor with parameters of the parent class can be called through super

 

Similarities and differences between this and super

different:

1. The things represented are different: the super keyword represents the reference of the parent class space, and the this keyword represents the reference of the current object

2. The premise of use is different: the premise of using the super keyword is that there must be an inheritance relationship, while the this keyword does not need

3. The difference between calling the constructor: the super keyword calls the constructor of the parent class, while the this keyword calls the constructor of this class

same

1. When calling the constructor, the first line must be unloaded, this and super cannot be used at the same time

2. Neither can be used in a static environment, including: static variables, static methods and static blocks

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324738040&siteId=291194637