The use of this and super in Java

Source: Internet
Author: User

First, basic knowledge

1, super (parameter): Call a constructor in the base class (should be the first statement in the constructor)

2, this (parameter): Invoke another form of constructor in this class (should be the first statement in the constructor)

3, Super: It refers to members of the immediate parent class of the current object (used to access member data or functions in the parent class that is hidden in the immediate parent class, when the base class has the same member definition as the derived class)

such as: Super. Variable name

Super. member function name (argument)

4, this: it represents the current object name (in the program is easy to produce two semantics, you should use this to indicate the current object; If the member data in the function's form-participation class has the same name, you need this to indicate the member variable name)

Second, application examples

Class Point

{private int x,y;

Public point (int x,int y)

{

This.x=x; This represents the current object name

This.y=y;

}

public void Draw ()

{

}

Public Point ()

{

This (0,0); This (parameter) calls another form of constructor in this class

}

}

Class Circle extends Point

{

private int radius;

Public Circle (int x0,int y0, int R)

{

Super (X0,Y0); Super (parameter) calls one of the constructors in the base class

Radius=r;

}

public void Draw ()

{

Super. Draw (); Super it references members in the immediate parent class of the current object

Drawcircle ();

}}

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.