This and super in Java

Source: Internet
Author: User

1.this

This is an implicit parameter, which is the constructed object, and we access the instance domain in the same way as this.

Public Employee (String name,double salary)

{

This.name=name;

This.salary=salary;

}

Here's a brief look at the difference between an implicit parameter and a display parameter, which usually appears before the method name and represents an object. Display parameters are typically located in a method

The value in parentheses after the name. Let's look at the following statement, number007 is an implicit parameter, and 5 is a display parameter.

Number007.raisesalary (5);

Another function of this is to invoke the other constructors of the same class as this (...), and we look at the following example:

Public Employee (double s)

{

Call Employee (string,double)

This ("employe#" +nextid,s);

nextid++;

}

When we call new Employee (6000), the employee (double) constructor calls the employee (String,double) constructor.

2.super

Don't assume that super and this reference are similar concepts, this is a reference to an object that represents an object, and super is just a special keyword that instructs the compiler to call a superclass method.

 class Tiger {int ages;//age int hight;//Body Height public Tiger () {PR      int (); } public void print () {System.out.println ("I'm a" + Age + "Year" + hight + "ruler high tiger!");}    public class Javantiger extends Tiger {public Javantiger () {super ();     Call the parent class parameterless constructor} public static void Main (string[] args) {new Javantiger (); }}  
above is an example of a super () call to the parent class parameterless constructor, but if the constructor of the subclass does not explicitly call the constructor of the superclass, The constructor for the superclass default (without parameters) is automatically called. Note that the Java compiler will report an error if the superclass does not have
Class Tiger {    int aged;//age     int hight;//Body Height public     Tiger (int ages) {      this.age = ages;    Print ();     }    public void print () {         System.out.println ("I'm a" + Age + "Year" + hight + "ruler high tiger!");}     public class Javantiger extends Tiger {public         Javantiger () {         super (1);    Call the parent class constructor with arguments    } public    static void Main (string[] args) {         new Javantiger ();}     }
The above is an example of a super call with a parametric constructor.
Finally, most importantly, this and super cannot appear in the subclass's constructor at the same time, and Java stipulates that this and super must appear in the first row of the constructor.

This and super in Java

Related Article

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.