Super is a reserved word in the Java language that is used to point to superclass classes.
Suppose a class variable Boolean gender is defined in the teacher class;
In the subclass method 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 class's constructor to initialize the object's domain, if the argument and class variable have the same name, The name of the class variable will be masked by the parameter name,
You must know the current object name to refer to the object's domain with the object name
Copy 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 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 Java classes to refer to the composition of the base class.
Instance:
Testinherit.java:
Copy 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 ();
}
}