Two special variables in Java this and superwayne
There are two very special variables in Java: this and super, these two variables are not required before use. The THIS variable is used inside the member function, pointing to the current object, the current object refers to the object that calls the current executing method. The Super variable is directed to the constructor of the superclass, which is used to reference variables and methods in the superclass. So they are all very useful variables, below I want to introduce this and super use method.
1, THIS
Let us first see a piece of code:
Class Personification
{
String name, gender, nationality, address;
Int agec;
Void personformation (string p_name, string p_gender, string p_nationality, string p_address, int p_age)
{
Name = p_name;
Gender = p_gender;
Nationality = p_nationality;
Address = p_address;
Age = p_age;
}
} You will find that the method of this object in the personification () function prompts you to directly access the member variable of the object, and in the same range, the local variables that define two identical names are not allowed, if they really want to make The parameters of the members variables and methods of the class or method of the method you define the same name yourself. If you need a way to distinguish the member variables with the same name or partial variable, this is to use the THIS variable. Below I want to rewrite the above code, so that each parameter of the constructor of the PersonInformation class has the same name as the object member variable, and the initial value of the member variable is given by the parameters.
Class Personification
{
String name, gender, nationality, address;
Int agec;
Void Personification (String Name, String Gender, String National, String Address, Int Age)
{
THIS.NAME = Name;
THIS.GENDER = GENDER;
. ..nationality = nationality;
THIS.ADDRESS = Address;
THIS.AGE = AGE;
}
}
In the previous example, we can see that this must be used in this constructor, this THIS is used by the method body weight to point to the object instance of the reference current is performing the method, the THIS variable is always for the class, on the pre-execution method. In the example, we have to distinguish between parameters Name and member variable Name, write to Name = Name Obviously, when the parameter or local variable name is the same as the class member variable, due to the high priority of the parameters or local variables, The parameter name or partial variable name in the body will hide the same name of the member variable, so for the value name member variable, you must use this to indicate the current object.
Sometimes we will encounter this situation, we fully access the current object, rather than accessing an alias instance object, we can also use this, and use the toString () method in Java (it can return a description of this object String) If any object is passed to the System.Println method, this method calls the TSTRING method of this object, and prints the result string, so we can use the following method system.out.println (this), To print any current state of the method inherent parameters. This is also a usage, which is the first statement of constructing function. Its form is this (parameter table), which calls another relative constructor of the same class. Please see the example below:
Class userInfo
{
Public userInfo (String name)
{
THIS (Name, Anewserialnumber);
}
Public UserInfo (String Name, int Number)
{
Username = name;
Usernumber = Number;
}
}
If you call UserInfor NewInfotable = New UserInfo ("Wayne Zheng", the userinfo (String Name, int Number) constructor is automatically invoked.
It can be seen that the proficiency of this is very important in the Java program design process.
In Java, sometimes Member variables or methods in subclasses or holy (sometimes known as parent classes) are the same name, because members variables in subclasses or high priorities are high, so The same names and methods in subclasses hide the superclass member variable or method, but if we want to use this member variable or method in the superclass, you need to use Super, see the following class.
Class Country
{
String name;
void value ()
{
Name = "China";
}
}
In the subclass of the following, self-class members variables and methods hide the superclass of members variables Name and method value (),
Class City Extends Country
String name;
void value ()
{
Name = "hefei";
Super.Value ();
System.out.println (Name);
System.out.println (super.name);
} In order to reference member variables in the superclass in subclasses Value (), we use Super, Super.Name and Super.Value in the code;
So the result of the display is
Hefei
China
If we want to use a superclass constructor, you should use the SUPER (parameter list).