Java this and super

Source: Internet
Author: User

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

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.