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)