The use of the this,super of keywords in Java

Source: Internet
Author: User

A. This keyword introduction.

Note: This knowledge point is limited to the scope of my understanding of it.

Package com.study.java.oop;

/**
* Core point: "This is a pointer to the object itself"
* Where it appears: only in the method body of the method (and the method is only a non-static method in the Class)
* Static methods and static blocks are absolutely not available
* How to use:
* This (parameter) >>> call is the constructor of the class with parameters called the parameterless construct without a parameter call
* this. member variable name >>> call member variable of class
* @author Ys.zhangjy
*
*/
public class StudyOop02 {
Private String username;
private String password;
private int number;
private int x;

Public StudyOop02 () {
This ("null", "Empty", 20);//constructor method via this () call

}

Public StudyOop02 (String username,string ps,int x) {
This.username = username;//this. Member variable name calls a member variable of a class
This.password = PS;
this.x = x;
System.out.println (username+ "= =" +password+ "= =" +x);
}

Public StudyOop02 (String Username,int x) {
This (username, "2 parameters", x);
}

public void Outprint (StudyOop02 s) {
System.out.println ("---------------");
System.out.println (S.username);
System.out.println (S.password);
System.out.println (S.number);
System.out.println (s.x);
System.out.println ("---------------");
f ();//write THIS.F () >>> call method

}

private void F () {
int x = 200;
/** local variables and member variables with the same name this. Variable names call the class's member variables and methods
* At the same time there is a distinguishing effect **/
x = this.x++;
System.out.println (the value of the member variable x: "+this.x+" The value of the local variable x: "+x);


}

Public StudyOop02 (string username, string passwrd, int number, int x) {
Super ();
This.username = Username;
This.password = PASSWRD;
This.number = number;
this.x = x;
}

public static void Main (string[] args) {
STUDYOOP02 S1 = new StudyOop02 ();
STUDYOOP02 s2 = new STUDYOOP02 ("waistcoat", 23);
S1.outprint (S1);
S2.outprint (S2);
/** error This cannot be used in a static method **/
System.out.println (this.username = "2222222");

}

/**
* ERROR This cannot be used in static blocks
*/
/*static{
This.username = "xxx";
This.password = "1111";
This.number = "11111";
This.x = 200000;
}*/

}

Operation Result:

---------------
Empty
Empty
0
20
---------------
Value of member variable x: 21 value of local variable x: 20
---------------
Small vest
2 parameters
0
23
---------------
Value of member variable x: 24 value of local variable x: 23

Two. Super keyword use.

Parent class: Father

Package com.study.java.oop;

/**
* Where it appears: sub-category
How to use: Sub-class uses core points: subclasses must first construct the parent class before constructing and the Super keyword can only appear in the first row of the subclass construction
* Super. Variable name >>> call is a member variable in the parent class
* Super. Method name >>> call is a method in the parent class
* SUPER (Parameters) >>> call the constructor of the parent class there are parameters, no parameters, no arguments.
* @author Ys.zhangjy
*
*/
public class Father {
private String name;
Private String job;
private int age;
Public String s = "variable in parent class";

Public Father () {
System.out.println ("The parent class is called without a reference structure");
}

Public Father (String name,string job,int age) {
Super ();
THIS.name = name;
This.job = job;
This.age = age;

}

public void Test () {
System.out.println ("super. Method name >>> Call method in parent class");
}

public void Play () {

SYSTEM.OUT.PRINTLN ("Call the Play () method of the parent class");

}

}

Sub-class: Sub

Package com.study.java.oop;

public class Sub extends father{
private String name;
Private String job;
private int age;
Public String s = "Member variable in child class";
Public Sub () {
Super ();
System.out.println ("sub-class non-parametric constructs are called");
Super ("Xiaoming", "Xiaoshou", 20); Error description Super keyword can only appear in the first row of the subclass construct
}

Public Sub (String name,string job,int age) {
Super ("Xiaoming", "Xiaoshou", 20);
System.out.println ("Subclass has a reference structure called");
}

public void Play () {
SYSTEM.OUT.PRINTLN ("Call the member variable in the parent class:" +SUPER.S);
System.out.println ("Play () method called subclass");
Super.test ();
}

public void Test () {
System.out.println ("Methods in Subclasses");
}

public static void Main (string[] args) {
Sub s = new Sub ();
S.play ();

}

}

Operation Result:

The parent class is called without a parameter construct
Sub-class No-parameter constructs are called
Call a member variable in the parent class: A variable in the parent class
Calling the subclass's play () method
Super. Method name >>> Call method in parent class

Description

First, in the subclass construction method to invoke the parent class's constructor method, called with "super (parameter list)", the parameter is not necessary. It is also important to note that the "super (parameter list)" statement can only be used in the first row of the subclass in which the method body is constructed.
Second, when a local variable in a subclass method or a member of a subclass has the same name as a parent class member variable, that is, when a subclass local variable overrides the parent class member variable, a "super. Member variable name" is used to refer to the parent class member variable. Of course, if the member variables of the parent class are not overwritten, you can also refer to the parent class member variable with "super. Member variable name", but this is unnecessary.
Thirdly, when the member method of a subclass overrides the member method of the parent class, that is, the subclass and parent class have exactly the same method definition (but the method body can be different), at this point, the method of the parent class is accessed with "super. Method name (parameter list)".

The use of the this,super of keywords in Java

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.