reproduced: http://blog.csdn.net/dyllove98/article/details/8826232
In Java, this usually refers to the current object, and Super refers to the parent class. When you want to refer to something of the current object, such as a method of the current object, or a member of the current object, you can use this for this purpose, and, of course, another use of this is to invoke another constructor of the current object, which will be discussed immediately. If you want to quote something from the parent class, it's not super. Since this and the super have so many similar traits and innate relationships, we're here to discuss it, hoping to help you differentiate and master two of them.
In the general method
The most common scenario is that a parameter name in your method has the same name as a member of the current object, so you need to explicitly use the This keyword to indicate that you want to use a member, using the "this. Member name" instead of the one that is the formal parameter. In addition, you can use the "this. Method name" to refer to a method of the current object, but this is not necessary, you can directly use the method name to access the method, the compiler will know that you want to call the one. The following code demonstrates the use of the above:
Public classdemothis{PrivateString name;Private intage;demothis (String name,intAge ) {setName (name);//You can add this to invoke the method, like this: This.setname (name); but that's not necessary.Setage (age); This. Print ();} Public voidsetName (String name) { This. name=name;//Here you must indicate that you want to reference the member variable} Public voidSetage (intAge ) { This. age=Age ;} Public voidprint () {System.out.println ("Name=" +name+ "age=" +age);//This is not required in this line because there is nothing that will cause confusion} Public Static voidMain (string[] args) {demothis dt=NewDemothis ("Kevin", "22");}}
This code is very simple and you should be able to understand it without explaining it. In the constructor you see Using This.print (), you can completely replace it with print (), which works the same way. Here we modify this program to demonstrate the use of super.
classperson{ Public intC;PrivateString name;Private intAge ;protected voidsetName (String name) { This. name=name;}protected voidSetage (intAge ) { This. age=Age ;}protected voidprint () {System.out.println ("Name=" +name+ "age=" +Age );}} Public classDemosuperextendsperson{ Public voidprint () {System.out.println ("Demosuper:");Super. Print ();} Public Static voidMain (string[] args) {demosuper ds=Newdemosuper ();d s.setname ("Kevin");d S.setage (22);d s.print ();}}
In Demosuper, the redefined Print method overridden The Print method of the parent class, first doing something of its own, and then invoking the overridden method of the parent class. The output illustrates this point:
Demosuper:
Name=kevin age=22
The use of this method is more commonly used. In addition, if a member of the parent class can be accessed by a class, you can use it like this, using the "super. Member name in the parent class" way, but often you do not have access to the member names in the parent class.
In the constructor
A constructor is a special method that is called automatically when an object is initialized. In the constructor, this and super also have a variety of ways to use the above, and it has a special place, see the following example:
classperson{ Public Static voidprt (String s) {System.out.println (s);} Person () {PRT ("A person.");} Person (String name) {PRT (' A person name ' is: ' +name);}} Public classChineseextendsPerson{chinese () {Super();//calling the parent class constructor (1)PRT ("A Chinese.");//(4)}chinese (String name) {Super(name);//calling a constructor with the same formal parameters as the parent class (2)PRT ("His name is:" +name);} Chinese (String name,intAge ) { This(name);//call the constructor that currently has the same formal parameter (3)PRT ("His age is:" +Age );} Public Static voidMain (string[] args) {Chinese CN=NewChinese (); CN=NewChinese ("Kevin"); CN=NewChinese ("Kevin", 22);}}
In this program, this and super are no longer used as before. Connect a method or member, but follow the appropriate parameters directly thereafter, so the meaning of it changes. Super is used to invoke constructors that have the same form in the parent class, such as 1 and 2. This is followed by a constructor that currently has the same parameters, such as 3. Of course, in the various overloaded constructors of Chinese, this and super are still available in various usages of the general method, such as 4 where you can replace it with "this.prt" (because it inherits that method from the parent class) or "Super.prt" (because it is a method in the parent class and can be accessed by the class), it works correctly. But it seems to be a little superfluous flavor.
The difference between this and super in Android