Super is a reserved word in the Java language, used to point to the super class of a class.
Assume that a class variable boolean gender is defined in the Teacher class;
In the method of the subclass, gender should refer to the gender variable of the subclass. If you want to refer to the gender variable of the superclass, you must use the super.genderthis object. In the constructor of the class, you need to initialize the domain of the object. At this time If the parameter has the same name as the class variable, the name of the class variable will be masked by the parameter name.
You must know the current object name before you can use the object name to reference the object's fields.
Copy the code code as follows:
public DotLoc(double XX,double YY,double ZZ)
{
X=XX;Y=YY;Z=ZZ;
}
If the parameter has the same name as the class variable name
Copy the code code as follows:
public DotLoc(double X,double Y,double Z)
{
this.X=X;this.Y=Y;this.Z=Z;
}
Another example:
Use super in a Java class to refer to components of a base class.
Example:
TestInherit.java:
Copy the code code as follows:
import java.io.* ;
class FatherClass {
public int value;
public void f() {
value = 100;
System.out.println("FatherClass.value=" + value) ;
}
}
class ChildClass extends FatherClass {
public int value;
public void f() {
super.f();
value = 200;
System.out.println("ChildClass.value=" + value);
System.out.println(value);
System.out.println(super.value);
}
}
public class TestInherit {
public static void main(String args[]) {
ChildClass cc = new ChildClass();
cc.f();
}
}