Two special variables in variable java this and super2001-04-26 Wayne Yesky has two very special variables in Java: This and super, both of which need not be declared before they are used. The this variable is used inside a member function to point to the current object, which refers to the object that invokes the currently executing method. A super variable is a constructor that directly points to a superclass to refer to variables and methods in the superclass. So they're all very useful variables, and here's how this and super are used. 1, this let us look at a piece of code first: class Personinformation {String name,gender,nationality,address; int age; void Personinformation (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 hint for this object in the Personinformation () function can directly access the member variables of the object, and in the same scope, it is not permissible to define local variables with two identical names. If you do want to make a member variable of a class with the same name as a parameter of a method or a local variable of its own definition, you need to think of a method that distinguishes a member variable from a method parameter or local variable with the same name, which is used to the this variable. I would like to rewrite the above code so that each parameter of the constructor for the Personinformation class has the same name as the object member variable, and the initial value of the member variable is given by the argument. Class Personinformation {String name,gender,nationality,address; int age; void Personinformation (String name,string gender,string nationality,string address,int age) {this.name=name; This.gender=gendEr this.nationality=nationality; this.address=address; This.age=age; } In the previous example, as we can see, the constructor must use This,this in the method weight to point to the object instance that references the currently executing method, the type of this variable is always the class that contains the former execution method, in the example, we want to distinguish between the parameter name and the member variable name. Writing Name=name is clearly not allowed, in the case of a parameter or local variable name that has the same name as a class member variable, because the argument or local variable has a higher precedence, so that the parameter name or local variable name in the method body hides the member variable with the same name, so for the value name member variable, You must use this to indicate the current object. Sometimes this happens when we have full access to the current object instead of accessing an individual instance object, we can also use this and take advantage of the ToString in Java () Method (It can return a string that describes the object) if you pass any object to the System.out.println method, this method calls the object's ToString method and prints out the result string, so we can use the following method to System.out.println (this) to print out any current state of the intrinsic parameters of the method. This is also the use of the first statement of the constructor, which is the form this (parameter table), which calls another relative constructor of the same class. Consider the following example: 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. Visible, mastering this is very important in the Java programming Process 2, super in Java, sometimes you also encounter a member variable or method in a subclass that has the same name as a member variable or method in a superclass (sometimes called a parent), because the member variable or method name in the subclass has a higher precedence, so the subclassThe member variables and methods of the same name in the class hide the member variables or methods of the superclass, but if we want to use this member variable or method in the superclass, this makes it necessary to use super, see the following class. Class Country {String name; void value () {name= "a"; The member variables and methods of the superclass have hidden the member variable name and method value () from the class, class city extends Country String name, in the subclass below. void value () {name= "Hefei"; Super.value (); SYSTEM.OUT.PRINTLN (name); System.out.println (Super.name); In order to reference the member variable name and method value () in the superclass in the subclass, we used Super,super.name and Super.value () in the code, so the result shown is Hefei China if we want to use the superclass constructor You should use the form of super (parameter list).
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.