To illustrate the use of this and supper in the form of an example
In the construction method, if you want to use the parent class method, you can access the methods in the parent class in a way that supper. Method names.
In a class, if the variable name of the method is the same as the variable name in the class, you can use this. Variable name to represent the variable in the method (otherwise the variable is automatically treated as a variable in the class)
Class persion{
public int C;
private String name;
private int age;
protected void SetName (String name) {
This.name=name;
}
protected void Setage (int age) {
This.age=age;
}
protected void print () {
System.out.println ("name=" +name+ "age=" +age);
}
}
public class Testpersion extends persion{
public void print () {
System.out.println ("This supper demo");
Super.print ();
}
public static void Main (String args[])
{
Testpersion tsd=new testpersion ();
Tsd.setname ("Robert");
Tsd.setage (22);
Tsd.print ();
}
}
Java this and super