Java keyword This, super use summary

Source: Internet
Author: User

Lava
Date: 2007-03-01
MSN:[email protected]
Disclaimer: Original works, Unauthorized, declined reprint!
for a long time did not summarize the knowledge learned, found a lot of unfamiliar, in order to consolidate the foundation, on the knowledge to do a systematic summary, if there is not perfect or incorrect place, also ask you to broaden the speech, common progress!
java keyword This, super use summary First, this
The Java keyword This can only be used in a method method body. When an object is created, the Java Virtual Machine (JVM) assigns a pointer to the object that refers to itself, which is the name of this pointer. Therefore, this can only be used in non-static methods in the class, static methods and static code blocks must never appear in this, which is explained in the article "Java keyword static, final use summary". And this is only associated with a particular object, not with a class, and different objects of the same class have different this. A comprehensive example using this is given below to illustrate the problem: Package org.leizhimin;Public class Test6 {private int number;
Private String username;
private String password;
private int x = +;Public Test6 (int n) {
Number = n; This can also be written as: this.number=n;
}Public Test6 (int i, string username, string password) {
The member variable has the same name as the parameter, the member variable is masked, and the member variable is accessed in the way "this. Member variable".
This.username = Username;
This.password = password;
}//Default construction method with no parameters
Public Test6 () {
This (0, "Unknown", "empty"); Another constructor method is called through this
}Public Test6 (String name) {
This (1, name, "null"); Another constructor method is called through this
}Public static void Main (String args[]) {
Test6 T1 = new Test6 ();
Test6 t2 = new Test6 ("visitor");
T1.outinfo (t1);
T2.outinfo (T2);
}private void Outinfo (Test6 t) {
System.out.println ("-----------");
System.out.println (T.number);
System.out.println (T.username);
System.out.println (T.password);
f (); This can be written as: This.f ();
}private void F () {
The local variable has the same name as the member variable, the member variable is masked, and the member variable is accessed in the way "this. Member variable".
int x;
x = this.x++;
SYSTEM.OUT.PRINTLN (x);
System.out.println (this.x);
}

Returns a reference to the current instance
Private Test6 getself () {
return this;
}
}The results of the operation are as follows:
-----------
0
Unknown
Empty
100
101
-----------
0
Tourists
Empty
100
101 look at the example above to illustrate the circumstances underwhich this is required:
First, the use of this call another constructor method, with the hair is this (parameter list), this is only in the construction method of the class, other places can not be used.
Second, in the case of a function parameter or local variable in a function with the same name as a member variable, the member variable is masked, and the member variable needs to be referenced by the "this. Member variable name" method to access the member variable. Of course, in the absence of the same name, you can directly use the name of the member variable, instead of this, use is not wrong, hehe.
Third, in the function, it is necessary to refer to the current object of the class that the letter belongs to, directly with this. In fact, these usage summary is from the "This is a pointer to the object itself" this sentence of a deeper understanding of the word, rote or easy to forget and easy to make mistakes, to understand! second, Super Super key is similar to this, is a masked member variable or member method or becomes visible, or is used to refer to a masked member variable and member member method.
However, super is used in subclasses in order to access the masked members in the immediate parent class, and note that the immediate parent class is the closest superclass on the class. Here is an example of a comprehensive use of super, there are two classes: A Father class, a subclass of the Father class son, through these two classes fully demonstrates the use of super, the code is:Package org.leizhimin;Public class Father {
Public String v= "Father";
Public String x= "outputs the public member variable x!!! of the Father class";

Public Father () {
System.out.println ("Father constructor method is called!");
}

Public Father (String v) {
this.v= "Father class with Parametric construction Method!";
}
public void Outinfo () {
System.out.println ("Father's Outinfo method is called");
} Public static void Main (string[] args) {
TODO automatically generate method stubs
}
} Package org.leizhimin;Public class Son extends father{Public String v= "Son";

Public Son () {
Super (); Call the constructor of the superclass, which can only be placed in the first row.
System.out.println ("Son parameterless constructor method is called!");
Super (); The wrong one must be placed at the front of the construction method body.
}

Public Son (String str) {
Super (STR);
System.out.println ("Son with parameter constructor method is called!");
}
Overriding the superclass member Method Outinfo ()
public void Outinfo () {
System.out.println ("Son's Outinfo () method is called");
}

public void Test () {

String v= "haha haha!"; The local variable v covers the member variable V and the Super class variable V

SYSTEM.OUT.PRINTLN ("------1-----");
System.out.println (v); Output local variable V
System.out.println (THIS.V); Output (subclass) member Variable V
System.out.println (SUPER.V); Output Super class member variable V

SYSTEM.OUT.PRINTLN ("------2-----");
SYSTEM.OUT.PRINTLN (x); Output Super class member variable V, subclass inherits from
System.out.println (super.x); Output Super class member variable V

SYSTEM.OUT.PRINTLN ("------3-----");
Outinfo (); Call the Outinfo () method of the subclass
This.outinfo (); Call the Outinfo () method of the subclass
Super.outinfo (); Call the Outinfo () method of the parent class
}

public static void Main (string[] args) {
New Son (). Test ();

}
}sub-class son run result: Father constructor method is called!
Son parameterless constructor method is called!
------1-----
haha haha!
Son
Father
------2-----
The public member variable x for the Father class is output!!!
The public member variable x for the Father class is output!!!
------3-----
Son's Outinfo () method is called
Son's Outinfo () method is called
Father's Outinfo method is calledDescription: In order to illustrate the use of super, the actual design of the class is generally as private as possible. In the above example, the following summarizes the use of super:
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 this, super, but only to understand the principles of the principle, will not fall into the trap! References
Thinking in Java
JAVA2 Reference Daquan
There are still no books to remember.

Java keyword This, super use summary

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.