09. Summary of this and super usage in Java
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 can be roughly divided into three types in Java:
1. Common direct reference
Needless to say, this is equivalent to pointing to the current object itself.
2. The formal parameter has the same name as the member name, which is distinguished by this:
class Person { private int age = 10; public Person(){ System.out.println("Initialization age:"+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 results:
Initialization age: 10 Harry's age is 12
As you can see, here age is the formal parameter of the GetAge member method, and this.age is the member variable of the Person class.
3. Reference constructor
Put this together with super, see below.
super
Super can be understood as a pointer to its own super (parent) class object, and this super class refers to the nearest parent class.
super can also be used in three ways:
1. Common direct reference
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
class Country { String name; void value() { name = "China"; } } class City extends Country { String name; void value() { name = "Shanghai"; super.value(); //Call the method of the parent class System.out.println(name); System.out.println(super.name); } public static void main(String[] args) { City c=new City(); c.value(); } }
Operation results:
Shanghai China
You can see that both the methods of the parent class and the variables of the parent class are called here. If the parent class method value() is not called, and only the parent class variable name is called, the parent class name value is null by default.
3. Reference constructor
**Super (parameter): * * call a constructor in the parent class (should be the first statement in the constructor).
**This (parameter): * * call another form of constructor in this class (it should be the first statement in the constructor).
class Person { public static void prt(String s) { System.out.println(s); } Person() { prt("Parent class·Nonparametric construction method: "+"A Person."); }//Construction method (1) Person(String name) { prt("Parent class·Construction method with one parameter: "+"A person's name is " + name); }//Construction method (2) } public class Chinese extends Person { Chinese() { super(); // Call the parent class constructor (1) prt("Subclass·Call parent class"Nonparametric construction method": "+"A chinese coder."); } Chinese(String name) { super(name);// Call the constructor whose parent class has the same formal parameters (2) prt("Subclass·Call parent class"Construction method with one parameter": "+"his name is " + name); } Chinese(String name, int age) { this(name);// Call constructor with the same parameters (3) prt("Subclass: call the construction method of subclass with the same formal parameters: his age is " + age); } public static void main(String[] args) { Chinese cn = new Chinese(); cn = new Chinese("codersai"); cn = new Chinese("codersai", 18); } }
Operation results:
Parent class·Nonparametric construction method: A Person.Subclass·Calling parent class' parameterless constructor ': A chinese coder.Parent class·Construction method with one parameter: A person's name is codersai Subclass·Call the parent class' constructor with one parameter ': his name is codersai Parent class·Construction method with one parameter: A person's name is codersai Subclass·Call the parent class' constructor with one parameter ': his name is codersai Subclass: call the construction method of subclass with the same formal parameters: his age is 18
It can be seen from this example that super and this can be used to call the constructor of the parent class and other constructors in this class respectively. In this example, the third constructor of Chinese class calls the second constructor of this class, and the second constructor calls the parent class. Therefore, it is also necessary to call the constructor of the parent class first, then call the second constructor of this class, and finally rewrite the second constructor Three construction methods.
Similarities and differences between super and this
- Super (parameter): call a constructor in the base class (should be the first statement in the constructor)
- This (parameter): call another constructor formed in this class (should be the first statement in the constructor)
- Super: it refers to the member in the direct parent class of the current object (used to access the member data or function in the hidden parent class 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 data name (argument) this: it represents the name of the current object (where ambiguity is easy to occur in the program, this should be used to indicate the current object; if the formal parameter of the function has the same name as the member data in the class, this should be used to indicate the member variable name.)
- The call to super() must be written in the first line of the subclass constructor, otherwise the compilation will not pass. The first statement of each subclass constructor implicitly calls super(). If the parent class does not have this form of constructor, an error will be reported during compilation.
- super() is similar to this(). The difference is that super() calls the construction method of the parent class from the subclass, and this() calls other methods in the same class.
- Both super() and this() need to be placed in the first line of the constructor.
- Although you can call one constructor with this, you cannot call two.
- This and super cannot appear in one constructor at the same time, because this must call other constructors, and other constructors must also have super statements. Therefore, if there are the same statements in the same constructor, the meaning of the statements will be lost, and the compiler will not pass.
- this() and super() refer to objects, so they cannot be used in static environment, including static variables, static methods and static statement blocks.
- In essence, this is a pointer to this object, while super is a Java keyword.
example:
//The parent class Hero provides a construction method with parameters: public Hero(String name){ this.name = name; } //However, no parameter free construction method is provided. What should subclasses do? package charactor; public class Hero { public String name; protected float hp; public Hero(String name){ this.name = name; } // Deliberately does not provide a parameterless construction method // public Hero(){ // // } public static void main(String[] args) { } } //As a child class, the constructor of the parent class will be called anyway. By default, the parameterless constructor of the parent class will be called. //However, when the parent class does not have a parameterless constructor (a parameterless constructor is provided and is not shown), the subclass throws an exception because it attempts to call the parameterless constructor of the parent class. //At this time, the subclass must call the declared, existing and parameterized constructor of the parent class through super. package charactor; public class ADHero extends Hero implements AD{ public ADHero() { super("");//super("") calls the parameterized constructor Hero(String name) of the parent class; } @Override public void physicAttack() { System.out.println("Physical attack"); } }