Home >Java >JavaBase >Several uses of this keyword in java

Several uses of this keyword in java

王林
王林Original
2019-11-13 16:29:255436browse

Several uses of this keyword in java

The usage of this keyword is as follows:

1. Call the method of this class

public String introYourself() {
 return this.whoAreU() + this.haoOldAreU();
}

2. Call the attribute of this class

public void changeMyName(String name) {
 this.name = name;
}

3. Call other constructors of this class

public UserExample(String name) {
 this(name, -1);
}

4. Call the same-name method of the parent class or other specified classes

public String whoAreSuper() {
 return "super is " + UserExample.this.whoAreU() + ". ";
}

5. Hidden calling, without specifying the scope, java will search for variables or methods upwards in the entire class scope

public String whoAmI() {
   return whoAreU();
  }

Recommended tutorial: Java tutorial

The above is the detailed content of Several uses of this keyword in java. For more information, please follow other related articles on the PHP Chinese website!

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