this
this is an object of itself, representing the object itself. It can be understood as:A pointer to the object itself。
The usage of this in Java can be roughly divided into 3 types:
1. Ordinary direct reference
No need to elaborate on this; this is equivalent to pointing to the current object itself.
2. When formal parameters have the same name as members, use this to distinguish:
Example
Output:
初始化年龄:10 Harry's age is 12
It can be seen that here age is the formal parameter of the GetAge member method, and this.age is the member variable of the Person class.
3. Referring to constructors
This will be discussed together with super, see below.
super
super can be understood as a pointer to its super (parent) class object, and this super class refers to the closest parent class.super also has three usages:
1. Ordinary direct reference
Similar to this, super is equivalent to pointing to the parent class of the current object, so you can usesuper.xxxto reference members of the parent class.
2. When member variables or methods in the subclass have the same name as those in the parent class
Example
Output:
Shanghai China
It can be seen that here both the parent class's method and the parent class's variable are called. If the parent class method value() is not called and only the parent class variable name is called, then the parent class name value will be the default value null.
3. Referring to constructors
- super(parameters): Call a constructor 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).
Example
Output:
父类·无参数构造方法: A Person. 子类·调用父类”无参数构造方法“: A chinese coder. 父类·含一个参数的构造方法: A person's name is codersai 子类·调用父类”含一个参数的构造方法“: his name is codersai 父类·含一个参数的构造方法: A person's name is codersai 子类·调用父类”含一个参数的构造方法“: his name is codersai 子类:调用子类具有相同形参的构造方法:his age is 18
From this example, we can see that super and this can be used to call the parent class's constructor and other forms of constructors in this class respectively.
In the example, the third constructor of the Chinese class calls the second constructor of this class, and the second constructor calls the parent class. Therefore, the parent class's constructor must be called first, then the second constructor of this class, and finally the third constructor is rewritten.
Similarities and differences between super and this
- super(parameters): Call a constructor in the base 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)
- super: It references members in the direct parent class of the current object (used to access member data or functions of the parent class hidden in the direct parent class. When the base class and derived class have the same member definitions, e.g., super.variableName super.memberFunctionName(actual parameters)). this: It represents the name of the current object (where ambiguity is easy to occur in the program, use this to indicate the current object; if the function's formal parameters have the same name as member data in the class, use this to indicate the member variable name).
- Calling super() must be written as the first line of the subclass constructor, otherwise compilation will fail. The first statement of every subclass constructor implicitly calls super(). If the parent class does not have a constructor of this form, an error will be reported during compilation.
- super() and this() are similar. The difference is that super() calls the parent class's constructor from the subclass, while this() calls other methods within the same class.
- Both super() and this() must be placed as the first line in the constructor.
- Although this can be used to call one constructor, it cannot call two.
- this and super cannot appear at the same time in a constructor, because this will inevitably call another constructor, and that other constructor will inevitably have a super statement. Therefore, having the same statement in the same constructor loses the meaning of the statement, and the compiler will not pass it.
- Both this() and super() refer to objects, so they cannot be used in a static environment. Including: static variables, static methods, static statement blocks.
- In essence, this is a pointer to this object, while super is a Java keyword.
Original link: https://www.cnblogs.com/hasse/p/5023392.html