Java This super usage method _java

Source: Internet
Author: User
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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.