Java keywords: this, super usage summary, uper

Source: Internet
Author: User

Java keywords: this, super usage summary, uper
Author: lava
Date: 2007-03-01
MSN: leizhimin@126.com
Statement: original works are reprinted without authorization!
I haven't summarized my knowledge for a long time and found that many of them are unfamiliar. In order to consolidate the foundation, I will make a systematic summary of my knowledge. If there are any imperfections or errors, we also invite you to make public comments and make common progress!
Java keywords: this, super usage Summary I. this
The Java keyword "this" can only be used in methods and methods. After an object is created, the Java Virtual Machine (JVM) assigns a pointer to the object to reference itself. The pointer name is this. Therefore, this can only be used in non-static methods in the class. this is definitely not allowed in static methods and static code blocks, this is a clear explanation in the article "Java keyword static and final usage summary. In addition, this is only associated with a specific object, but not the class. Different objects of the same class have different this. Here is a comprehensive example of using this to illustrate the problem: package org. leizhimin; public class Test6 {private int number;
Private String username;
Private String password;
Private int x = 100; 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 variables have the same name as the parameter. The member variables are blocked and accessed using the "this. member variable" method.
This. username = username;
This. password = password;
} // The construction method without parameters by default
Public Test6 (){
This (0, "unknown", "null"); // call another constructor using this
} Public Test6 (String name ){
This (1, name, "null"); // call another constructor using this
} Public static void main (String args []) {
Test6 t1 = new Test6 ();
Test6 t2 = new Test6 ("Tourist ");
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 blocked and accessed using the "this. member variable" method.
Int x;
X = this. x ++;
System. out. println (x );
System. out. println (this. x );
}
 
// Return the reference of the current instance
Private Test6 getSelf (){
Return this;
}
} The running result is as follows:
-----------
0
Unknown
Null
100
101
-----------
0
Visitors
Null
100
101 look at the example above to explain under what circumstances you need to use this:
First, call another constructor using this (parameter list). this is only used in the constructor of the class and cannot be used in other places.
2. When the name of a function parameter or local variable in the function is the same as that of a member variable, the member variable is blocked. To access the member variable, you must use "this. to reference member variables. Of course, without the same name, you can directly use the name of the member variable instead of this. If you use this name, it is no error.
Third, when the function needs to reference the current object of the class to which the function belongs, use this directly. In fact, these usage summaries come from a deeper understanding of the phrase "this is a pointer to the object itself". It is easy to forget and make mistakes, so you must understand it!Ii. superThe key of super is similar to this. It is a blocked member variable or member method or becomes visible, or used to reference the blocked member variable and member method.
However, super is used in subclass to access the blocked members in the direct parent class. Note that it is a direct parent class (that is, the nearest superclass above the class ). The following is an example of the comprehensive use of super. There are two classes: one Father class and one Father class sub-class Son. These two classes fully demonstrate the usage of super. Here is the code: package org. leizhimin; public class Father {
Public String v = "Father ";
Public String x = "output the Father class public member variable x !!! ";
 
Public Father (){
System. out. println ("Father constructor is called! ");
}
 
Public Father (String v ){
This. v = "Father class with parameter constructor! Running .";
}
Public void outinfo (){
System. out. println ("Father outinfo method called ");
} Public static void main (String [] args ){
// TODO automatically generates 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 ("the Son construction method without parameters is called! ");
// Super (); // The error must be placed at the top of the constructor body.
}
 
Public Son (String str ){
Super (str );
System. out. println ("Son has a parameter constructor called! ");
}
// Overwrite the superclass member method outinfo ()
Public void outinfo (){
System. out. println ("Son outinfo () method called ");
}
 
Public void test (){

String v = "Hahaha! "; // The local variable v overwrites the member variable v and the super variable v.

System. out. println ("------ 1 -----");
System. out. println (v); // output the local variable v.
System. out. println (this. v); // output (subclass) member variable v
System. out. println (super. v); // output the superclass member variable v

System. out. println ("------ 2 -----");
System. out. println (x); // output the superclass member variable v, inherited by the subclass.
System. out. println (super. x); // output the superclass 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 running result: the Father constructor is called!
Son's non-parameter constructor is called!
------ 1 -----
Hahahaha!
Son
Father
------ 2 -----
Output The public member variable x of the Father class !!!
Output The public member variable x of the Father class !!!
------ 3 -----
Son's outinfo () method is called
Son's outinfo () method is called
Father outinfo method is called to describe how to use the super method. In actual design, the Father outinfo method is generally private as much as possible. Through the example above, the usage of super is summarized below:
1. To call the constructor of the parent class in the subclass constructor, use the "super (parameter list)" method to call the constructor. The parameter is not required. Note that the "super (parameter list)" statement can only be used in the first row of the subclass construction method body.
2. When the local variable in the subclass method or the member variable of the subclass has the same name as the parent member variable, that is, when the subclass local variable overwrites the parent member variable, use "super. the member variable name to reference the parent class member variable. Of course, if the parent class member variables are not overwritten, you can also use "super. member variable name" to reference the parent class member variables, but this is unnecessary.
Third, when the member methods of the subclass override the member methods of the parent class, that is, the subclass and the parent class have the same method definition (but the method body can be different). At this time, use "super. method (Parameter List) to access the parent class. The usage of this and super is nothing more than this. Only by understanding the principles of this will we not fall into the trap! References
Thinking in Java
Java2 reference
I can't remember any other books.

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.