In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use this and super in Java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to use this and super in Java.
There are two very special variables in Java: this and super, both of which do not need to be declared before use. The this variable is used inside a member function and points to the current object, which refers to the object that is currently executing the method. Super variables are constructors that point directly to the superclass and are used to reference variables and methods in the superclass. So they are very useful variables, so I'd like to introduce how to use this and super.
1 、 this
Let's look at a piece of code first:
Class PersonInformation
{
String name,gender,nationality,address
Int age
Void PersonInformation (String pairname p_age. String string pendant gender.String packs nationalityjournal String packs address.int int)
{
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 PersonInformation () function indicates that the member variables of the object can be accessed directly, and it is not allowed to define two local variables with the same name in the same scope. If you really want the member variable of the class to have the same name as the parameter of the method or the local variable defined by the method itself, you need to think of a way to distinguish the member variable from the method parameter or local variable with the same name, which uses the this variable. Next 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 parameter.
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
}
}
From the previous example, we can see that this,this must be used in this constructor when the method weight is used to point to the object instance that refers to the currently executing method. The type of this variable is always the class containing the previous executing method. In the above example, we want to distinguish the parameter name from the member variable name. Writing as name=name is obviously not allowed, when the parameter or local variable name has the same name as the class member variable. Because the parameter or local variable has a high priority, the parameter or local variable name in the method body will hide the member variable with the same name, so in order to value the member variable, you must use this to indicate the current object.
Sometimes when we have full access to the current object instead of a single instance object, we can also use this and take advantage of the toString () method in Java (which returns a string that describes the object). If any object is passed to the System.out.println method, this method calls the object's toString method and prints the result string, so We can use the following method System.out.println (this) to print out the current state of any inherent parameters of the method.
Another use of this is the first statement of the constructor, which is in the form of this (parameter table), which calls another relative constructor of the same class. Take a look at 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 will be called automatically.
It can be seen that proficiency in this is very important in the process of Java programming.
2 、 super
In Java, it is sometimes encountered that the member variable or method in the subclass has the same name as the member variable or method in the superclass (sometimes called the parent class). Because the member variable or method name in the subclass has a high priority, the member variable or method with the same name in the subclass hides the member variable or method of the superclass, but if we want to use this member variable or method in the superclass, we need to use super Please look at the class below.
Class Country
{
String name
Void value ()
{
Name= "China"
}
}
In the following subclass, the member variables and methods of the self-class hide the member variables name and method value () of the superclass.
Class City extends Country
String name
Void value ()
{
Name= "Hefei"
Super.value ()
System.out.println (name)
System.out.println (super.name)
}
To reference the member variable name and the method value () in the superclass in the subclass, we use super,super.name and super.value () in the code
So the result displayed is
Hefei
China
If we want to use the constructor of the superclass, we should use the form of super (argument list).
At this point, I believe you have a deeper understanding of "how to use this and super in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.