Use of this and super

Source: Internet
Author: User
Tags extend

turn from: http://blog.csdn.net/zzp_403184692/article/details/7993742

This usage in Java can be roughly divided into 3 kinds: The Java keyword This is only used in the method method body. When an object is created, the Java Virtual Machine (JVM) assigns the object A pointer to itself, the name of this pointer. Therefore, this can only be used in Non-static methods in a class, and this is absolutely not allowed in static methods and static blocks of code, and this is only associated with a particular object, not the class, and different objects of the same class have different this.
public class Thistest {
private int i=0;
First constructor: There is an int type parameter
thistest (int i) {
this.i=i+1;//Now this represents the reference member variable I, not the function argument I
System.out.println ("Int constructor i--this.i: +i+"-"+this.i");
System.out.println ("I-1:" + (i-1) + "this.i+1:" + (this.i+1));
The results from both outputs amply demonstrate that I and this.i are not the same.
}
Second constructor: has a string parameter
Thistest (String s) {
System.out.println ("String constructor:" +s);
}
Third constructor: has an int shape parameter and a string parameter
Thistest (int i,string s) {
This (s);//this calls the second constructor
This (i);
This.i=i++;//this to reference the member variables of the class
System.out.println ("Int constructor:" +i+ "/n" + "String constructor:" +s);
}
Public Thistest increment () {
this.i++;
Return this;//Returns the current object, which belongs to (Thistest)
}
public static void Main (string[] args) {
Thistest tt0=new thistest (10);
Thistest tt1=new thistest ("OK");
Thistest tt2=new thistest ("OK again!");
System.out.println (Tt0.increment (). Increment (). Increment (). i);
Tt0.increment () returns a Thistest object that is i++ on tt0 basis.
It then returns the Thistest object based on the object returned on the i++.
}
Copy Code Run Results:
Int Constructor i--this.i:10--11
I-1:9this.i+1:12
String Constructor:ok
String Constructor:ok again!
Int constructor:21/nstring Constructor:ok again!
14 Copy the code to sum up the three uses of this:
1. Represents a reference to the current object.
2, representing the class's member variables, rather than function arguments, note that the function arguments and member variables have the same name to differentiate. In fact, this is the first use of the special case, more commonly used, so come out to emphasize.
3. Used to refer to constructors in the constructor method that satisfy the specified parameter type (in fact, the constructor method). But here you must be very careful: you can reference only one construction method and must be at the beginning.
There is also the note: This cannot be used in the static method. So even someone's definition of a static method is that there is no method for this. Although exaggerated, it is fully stated that this cannot be used in the static method.
So, in what circumstances do you need this?
First, invoke another construct method through this, with the hair is this (argument list), this is only in the class construction method, nowhere else.
Second, in the case of a function argument or a 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 in the way of "this. Member variable name". Of course, in the absence of the same name, you can use the name of the member variable directly without this.
Third, in a function, you need to refer to the current object of the class to which the letter belongs, using this directly.

Super usage

Super is placed in the first row when the constructor is used, which is equivalent to calling super and refreshing the constructor

The member of the SUPER.G () call refers to a member in the immediate parent class of the current object (used to access the member data or functions in the parent class that is hidden in the immediate parent class, when the base class has the same member definition as the derived class)

1.super applied in the constructor
Class Test1 Extend test{
Public test1 () ... {
Super ()//Call constructor of parent class
}
public test1 (int x) {
Super (x);//Invoke the constructor of the parent class, because Super also takes the row parameter with the formal parameter
}
}
2. Calling members in the parent class
Class Test1 Extend test{
Public test1 () {}
Public f () {//If there is a member in the parent class called G ();
SUPER.G ();//super refers to members in the immediate parent class of the current object
}
}

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.