This and Super use summary (Java)

Source: Internet
Author: User

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:

 Public classTest6 {Private intNumber ;PrivateString username;PrivateString password;Private intx = 100;  PublicTest6 (intN) { number= N;//This can also be written as: this.number=n; }  PublicTest6 (intI, 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  PublicTest6 () { This(0, "Unknown", "empty");//Another constructor method is called through this }  PublicTest6 (String name) { This(1, name, "empty");//Another constructor method is called through this }  Public Static voidMain (String args[]) {Test6 T1=NewTest6 (); TEST6 T2=NewTest6 ("Tourist");  T1.outinfo (t1); T2.outinfo (T2); } Private voidOutinfo (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 voidf () {//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".  intx; X= This. x + +;  SYSTEM.OUT.PRINTLN (x); System.out.println ( This. x); }  //returns a reference to the current instance PrivateTest6 getself () {return  This; The result of the operation is as follows:-----------0unknown null100101-----------0Tourist Air100101

Look at the example above to illustrate the circumstances under which this is required:
First, the use of this call another constructor method, the usage is this (argument 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!

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:

1  Public classFather {2   PublicString v= "Father";3   PublicString x= "Output the public member variable x!!! of the Father class";4  5   PublicFather () {6System.out.println ("Father constructor method is called!"));7  }8  9   PublicFather (String v) {Ten    This. v= "Father class with parameter construction Method!" Run. "; One  } A   Public voidOutinfo () { -System.out.println ("Father's Outinfo method is called"); -  }  the   Public Static voidMain (string[] args) { -   //TODO automatically generate method stubs -  } - } +   -  Packageorg.leizhimin; +  Public classSonextendsfather{ A   PublicString v= "Son"; at   -   PublicSon () { -   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. -  }  in   -   PublicSon (String str) { to   Super(str); +System.out.println ("Son with parameter constructor method is called!")); -  } the  //overriding the superclass member Method Outinfo () *   Public voidOutinfo () { $System.out.println ("Son's Outinfo () method is called");Panax Notoginseng  }  -   the   Public voidTest () { +    AString v= "haha haha!";//The local variable v covers the member variable V and the Super class variable v the    +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-----"); theSYSTEM.OUT.PRINTLN (x);//output Super class member variable V, subclass inherits from -System.out.println (Super. x);//output Super class member variable vWuyi    theSYSTEM.OUT.PRINTLN ("------3-----"); -Outinfo ();//Call the Outinfo () method of the subclass Wu    This. Outinfo ();//Call the Outinfo () method of the subclass -   Super. Outinfo ();//Call the Outinfo () method of the parent class About  }  $   -   Public Static voidMain (string[] args) { -   NewSon (). Test (); -    A  } + } the   - sub-class son run result: $   theFather constructor method is called! theSon parameterless constructor method is called! the------1----- thehaha haha! - Son in Father the------2----- theThe public member variable x for the Father class is output!!! AboutThe public member variable x for the Father class is output!!! the------3----- the son's Outinfo () method is called the son's Outinfo () method is called + Father's Outinfo method is called -  

Description: 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.
Third, when a member method of a subclass overrides (Overrides) a 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 in the way "super. Method name (parameter list)".

This article transferred from: http://mougaidong-163-com.iteye.com/blog/994934

This and Super use summary (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.