Javase Basics (5)-Object oriented (5.5 This and Super keywords)

Source: Internet
Author: User
Tags access properties

First, this keyword 1, description

The This keyword represents the object of the current class and can access the properties, methods, constructors of this class
Note: who calls the method, this refers to who

2. Grammar

Access properties:
this. property name = value;
SYSTEM.OUT.PRINTLN (this. Attribute name);
Access method:
This. Method name ();
To access the constructor:
This ([parameter list]);

3. Precautions

In general, if you access the members of the current class internally, you can access it directly, which is also called by this by default.
When local variables and member variables have the same name, the member variable must be called through this!

Second, Super keyword 1, description

Super represents a reference to the parent class obtained in the subclass, and can access the parent class's properties, methods, constructors

2. Grammar

Super. Property name = value;
Super. Method name (parameter list);
Super (parameter list); only in the first sentence of the constructor

3. Precautions

① when a subclass member has the same name as a member of a parent class, in the subclass, the member of this class is accessed by default, and if a member of the parent class needs to be accessed, a super access
② when the parent class does not have a parameterless constructor, the subclass must explicitly call the parent class's parameter constructor through super

4. Features

①super first accesses the members of the immediate parent class, if not all the way up, until the object class
② if the parent class and the upper-level indirect parent have a member of the same name, the access follows the nearest principle

Third, this and super contrast
  Access property or method Access constructor
this object of the current class

First find Span style= "COLOR: #ff0000" > this class has the member, if any, direct access,

If you do not continue to find the parent class until you find

This (parameter list);

The

can only be placed in the first sentence of the constructor to access the

super reference to parent class

First find Direct parent class /span> If there is a member, if any, direct access,

If you do not continue looking up other parent classes until you find the

Super (parameter list);

The

can only be placed in the first sentence of the constructor to access

Javase Basics (5)-Object-oriented (5.5 This and Super keywords)

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.