Java-this, super keyword (careful analysis)

Source: Internet
Author: User

First, this keyword

The This keyword can be used only in the method body, and when an object is created, the JVM assigns a veteran pointer to this object, which is this. Therefore this can only be used in non-static methods, static methods and static code blocks must not be used this,this only with specific objects, not associated with classes, different objects of the same class have different this.

Give me a chestnut:

1  Packagecn.zpoor.Test;2 /**3  * @authorSchrödinger's Cat4 * Test this*/5  Public classMain {6     PrivateString name;7     Private intAge ;8     PrivateString sex;9     Private inti = 33;Ten     //constructor for a parameter One      PublicMain (intN) { AAge = N;//can be written as This.age = age; -     } -      the     //A constructor with two parameters -      PublicMain (intage,string name,string Sex) { -         //Member variables and parameters with the same name, member variables are masked, and this is used to access member variables -          This. Name =name; +          This. Sex =sex; -     } +      A     //default constructor at      PublicMain () { -          This(0,NULL,NULL);//This invokes another construction method and can only be placed on the first row.  -     } -      -      PublicMain (String name) { -          This(0, name, "Man"); in     } -      to      +     Private voidinfo (Main m) { -System.out.println ("-------------"); the System.out.println (m.name); * System.out.println (m.age); $ System.out.println (m.sex);Panax Notoginseng          This. f ();//writing can be written as this.f (); -     } the      +     Private voidf () { A         //local variables and member variables are the same, member variables are masked, and this is used to access member variables the         inti; +i = This. i++; - System.out.println (i); $System.out.println ( This. i); $     } -      -     //returns a reference to the current instance the      -     PrivateMain getself () {Wuyi         return  This; the     } -      Wu      -      Public Static voidMain (string[] args) { AboutMain M1 =NewMain (); $Main m2 =NewMain ("Zpoor"); - M1.info (M1); - M2.info (m2); -     } A } +  the /*------------- - NULL $ 0 the NULL the  - the  the the ------------- - Zpoor in 0 the Mans the  - About  the the  the */

Don't Panic (cool analysis):

One, call another construction method through this, the This (argument list), only in the constructor, not elsewhere, and to be placed in the first row of the construction method

In the case of the parameter of the method or the name of the local variable and member variable in the method, the member variable is masked and the member variable is referenced by the this. Member variable. If you don't have the same name, you don't have to.

In the method, referring to the current object of the class to which the method belongs, use this

Summary: This is a pointer to the object itself.

Second, super keyword

Super is similar to this, is a masked member variable or member method, or refers to a masked member variable and member method, usually occurs in the inheritance, super in the subclass, used to access the members of the parent class masked

Give me a chestnut:

1  Packagecn.zpoor.Test;2 /**3  * @authorSchrödinger's Cat4 * Test Super*/5  Public classFather {6      PublicString Value = "Father";7      PublicString x = "Member variable in father";8     9      PublicFather () {TenSystem.out.println ("Father constructor is called"); One     } A      -      PublicFather (String Value) { -          This. Value = "Father the constructor with parameters has run"; the     } -      -      Public voidinfo () { -System.out.println ("The Father info method is called"); +     } -      Public Static voidMain (string[] args) { +         NewSon (). Test (); A     } at } -  - classSonextendsFather { -      PublicString Value = "Son"; -      -      PublicSon () { in         Super();//call the constructor of the parent class, only the first row must be placed -System.out.println ("Son's no-argument method called")); to     } +      -      PublicSon (String name) { the         Super(name); *System.out.println ("The constructor of the son with arguments is called"); $     }Panax Notoginseng      -     //overriding the parent class info method the      Public voidinfo () { +System.out.println ("Son's info method is called")); A     } the      +      Public voidTest () { -String Value = "Old iron, striking not?" ";//local variables override member variables and super class member variables $SYSTEM.OUT.PRINTLN ("-----1-----"); $System.out.println (Value);//output local variable value -System.out.println ( This. Value);//output sub-class member variables -System.out.println (Super. Value);//output the member variables of the parent class the          -SYSTEM.OUT.PRINTLN ("------2-----");WuyiSYSTEM.OUT.PRINTLN (x);//output Super class member variable x, subclass inherited from theSystem.out.println (Super. x);//output Cao Lei member variable x -          WuSystem.out.println ("--------3------"); -info ();//Call the Info method of the subclass About          This. info ();//Call the Info method of the subclass $         Super. info ();//Call the Info method of the superclass -     } - } -  A /* + The Father constructor is called the the no-argument method of son is called - -----1----- $ old iron, striking?  the Son the Father the ------2----- the member variables in the Father - member variables in the Father in --------3------ the Son's Info method is called the Son's Info method is called About the Father Info method is called the */

Summary (the hand with the smoke trembling slightly):

Subclass constructor method to invoke the constructor of the parent class, super (parameter list), the parameter is not required, but the first line is certain.

Second, when the sub-class local variables or sub-class local variables and the parent class member variable name, that is, the child class variable overrides the parent class variable, super. Member variable to refer to the parent class member variable, if there is no overwrite, it is not necessary, but it is better to do so, this is a good habit.

Third, when the member method of the child class overrides the Member method of the parent class, the method definition of the subclass is the same as the method of the parent class (override, Overwrite), super. The method name (parameter list) to invoke the member variable of the parent class.

No, the smoke is hot.

Java-this, super keyword (careful analysis)

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.