Java this super use method details

  • 2020-04-01 01:26:26
  • OfStack

Super is a reserved word in the Java language that points to the superclass of a class.
Suppose a class variable Boolean gender is defined in the Teacher class;
In the method of 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 super.genderthis object in the constructor of the class to initialize the field of the object. In this case, if the parameter is the same as the class variable, 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 field with the object name
 
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
 
public DotLoc(double X,double Y,double Z) 
{ 
this.X=X;this.Y=Y;this.Z=Z; 
} 

Such as:
Use super in Java classes to reference the components of the base class.
Example:
TestInherit. Java:
 
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() ; 
} 
} 

Related articles: