1.this
This is an implicit parameter, which is the constructed object, and we access the instance domain in the same way as this.
Public Employee (String name,double salary)
{
This.name=name;
This.salary=salary;
}
Here's a brief look at the difference between an implicit parameter and a display parameter, which usually appears before the method name and represents an object. Display parameters are typically located in a method
The value in parentheses after the name. Let's look at the following statement, number007 is an implicit parameter, and 5 is a display parameter.
Number007.raisesalary (5);
Another function of this is to invoke the other constructors of the same class as this (...), and we look at the following example:
Public Employee (double s)
{
Call Employee (string,double)
This ("employe#" +nextid,s);
nextid++;
}
When we call new Employee (6000), the employee (double) constructor calls the employee (String,double) constructor.
2.super
Don't assume that super and this reference are similar concepts, this is a reference to an object that represents an object, and super is just a special keyword that instructs the compiler to call a superclass method.
class Tiger {int ages;//age int hight;//Body Height public Tiger () {PR int (); } public void print () {System.out.println ("I'm a" + Age + "Year" + hight + "ruler high tiger!");} public class Javantiger extends Tiger {public Javantiger () {super (); Call the parent class parameterless constructor} public static void Main (string[] args) {new Javantiger (); }}
above is an example of a super () call to the parent class parameterless constructor, but if the constructor of the subclass does not explicitly call the constructor of the superclass, The constructor for the superclass default (without parameters) is automatically called. Note that the Java compiler will report an error if the superclass does not have
Class Tiger { int aged;//age int hight;//Body Height public Tiger (int ages) { this.age = ages; Print (); } public void print () { System.out.println ("I'm a" + Age + "Year" + hight + "ruler high tiger!");} public class Javantiger extends Tiger {public Javantiger () { super (1); Call the parent class constructor with arguments } public static void Main (string[] args) { new Javantiger ();} }
The above is an example of a super call with a parametric constructor.
Finally, most importantly, this and super cannot appear in the subclass's constructor at the same time, and Java stipulates that this and super must appear in the first row of the constructor.
This and super in Java