Home  >  Article  >  Java  >  Two special variables in Java this and super

Two special variables in Java this and super

黄舟
黄舟Original
2016-12-17 11:09:361325browse

 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



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn