Two special variables in Java this and super
There are two very special variables in Java: this and super. These two variables do not need to be declared before use. This variable is used inside a member function and points to the current object. The current object refers to the object that calls the currently executing method. The super variable points directly to the constructor of the super class and is used to reference variables and methods in the super class. So they are all very useful variables. Now I would like to introduce how to use this and super.
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 You will find that the method prompt of this object in the PersonInformation() function can directly access the member variables of the object, and it is not allowed to define two local variables with the same name in the same scope. If you really want to make the class If a member variable has the same name as a method parameter or a local variable defined by the method itself, you need to think of a way to distinguish the member variable from the method parameters or local variables with the same name. This requires the use of the this variable. Now 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;
this. name = name; It can be seen that this must be used in the constructor. This is used in the method body to point to the object instance that refers to the currently executing method. The type of this variable is always the class containing the previously executed method. In the above example, we need to distinguish It is obviously not allowed to write parameter name and member variable name as name=name. 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 name or local variable in the method body The variable name will hide the member variable with the same name. Therefore, to value the member variable, you must use this to explicitly indicate the current object.
Sometimes we encounter this situation. We fully access the current object instead of accessing an individual instance object. We can also use this and use the toString() method in Java (it can return a description of this Object's string) If any object is passed to the System.out.PRintln method, this method calls the toString method of this object and prints out the result string. Therefore, we can use the following method System.out.println(this ), to print out the current status of any intrinsic method parameters.
There is another usage of this, which is the first statement of the constructor. Its form is this (parameter list). This constructor will call another relative constructor of the same class. Please see 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"), UserInfo(String) will be called automatically.
The above is in Java For the contents of the two special variables this and super, please pay attention to the PHP Chinese website (www.php.cn) for more related articles

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



According to news from this site on January 6, Gigabyte’s RTX4080SUPER, RTX4070TiSUPER and RTX4070SUPER graphics cards have been exposed and are expected to be released at 0:00 on January 9th. As shown in the picture above, the logo of the Nvidia RTX40SUPER graphics card has changed. SUPER is all uppercase, while the RTX20 series has lowercase super. RTX20superlogo This site summarizes the revealed specifications and prices of the RTX40SUPER graphics card as follows: RTX4080SUPER is equipped with 10240CUDA core and 16GB23Gbps256bitGDDR6X video memory, priced at US$999 (currently about 7153 yuan) or US$1199 (currently about 7153 yuan)

A colleague got stuck due to a bug pointed by this. Vue2’s this pointing problem caused an arrow function to be used, resulting in the inability to get the corresponding props. He didn't know it when I introduced it to him, and then I deliberately looked at the front-end communication group. So far, at least 70% of front-end programmers still don't understand it. Today I will share with you this link. If everything is wrong If you haven’t learned it yet, please give me a big mouth.

This article will help you interpret the vue source code and introduce why you can use this to access properties in various options in Vue2. I hope it will be helpful to everyone!

1. static Please look at the following program first: publicclassHello{publicstaticvoidmain(String[]args){//(1)System.out.println("Hello, world!");//(2)}} Have seen this Segment programs are familiar to most people who have studied Java. Even if you have not learned Java but have learned other high-level languages, such as C, you should be able to understand the meaning of this code. It simply outputs "Hello, world" and has no other use. However, it shows the main purpose of the static keyword.

To understand covariance and contravariance, we must first introduce: According to the Liskov substitution principle, if C is a subclass of P, then P can replace C, that is, Pp=newC(); C inherits from P, denoted as C

Flexible use of this keyword in jQuery In jQuery, the this keyword is a very important and flexible concept. It is used to refer to the DOM element currently being manipulated. By rationally using this keyword, we can easily operate elements on the page and achieve various interactive effects and functions. This article will combine specific code examples to introduce the flexible use of this keyword in jQuery. Simple this example First, let's look at a simple this example. Suppose we have a

What is this? The following article will introduce you to this in JavaScript, and talk about the differences between this in different calling methods of functions. I hope it will be helpful to you!

1. this keyword 1. Type of this: Which object is called is the reference type of that object 2. Usage summary 1. this.data;//Access attribute 2. this.func();//Access method 3.this( );//Call other constructors in this class 3. Explanation of usage 1.this.data is used in member methods. Let us see what will happen if this is not added classMyDate{publicintyear;publicintmonth;publicintday; publicvoidsetDate(intyear,intmonth,intday){ye
