40.md 4.5 KB
Newer Older
W
wizardforcel 已提交
1
# Java 中的`this`关键字
W
init  
wizardforcel 已提交
2 3 4

> 原文: [https://javabeginnerstutorial.com/core-java-tutorial/this-keyword-java/](https://javabeginnerstutorial.com/core-java-tutorial/this-keyword-java/)

W
wizardforcel 已提交
5
## `this`是什么
W
init  
wizardforcel 已提交
6

W
wizardforcel 已提交
7
`this`是 Java 中的关键字。 可以在类的*方法**构造器*内部使用。 它(`this`) 用作对当前对象的引用,当前对象的方法或构造器正在被调用。 `this`关键字可用于从实例方法或构造器中引用当前对象的任何成员。
W
init  
wizardforcel 已提交
8

W
wizardforcel 已提交
9
## `this`关键字和字段(实例变量)
W
init  
wizardforcel 已提交
10

W
wizardforcel 已提交
11
`this`关键字在处理变量隐藏时可能非常有用。 我们不能创建两个具有相同名称的实例/局部变量。 但是,创建一个实例变量&,一个具有相同名称的局部变量或方法参数是合法的。 在这种情况下,局部变量将隐藏实例变量,这称为变量隐藏。
W
init  
wizardforcel 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

### 变量隐藏示例

```java
class JBT {

	int variable = 5;

	public static void main(String args[]) {
		JBT obj = new JBT();

		obj.method(20);
		obj.method();
	}

	void method(int variable) {
		variable = 10;
		System.out.println("Value of variable :" + variable);
	}

	void method() {
		int variable = 40;
		System.out.println("Value of variable :" + variable);
	}
}
```

上面程序的输出

```java
Value of variable :10
Value of variable :40
```

W
wizardforcel 已提交
46
如您在上面的示例中看到的那样,实例变量正在隐藏,并且局部变量(或“方法参数”)的值不显示为实例变量。 要解决此问题,请使用`this`关键字,并使用一个字段指向实例变量而不是局部变量。
W
init  
wizardforcel 已提交
47

W
wizardforcel 已提交
48
### `this`关键字用于变量隐藏的示例
W
init  
wizardforcel 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

```java
class JBT {

	int variable = 5;

	public static void main(String args[]) {
		JBT obj = new JBT();

		obj.method(20);
		obj.method();
	}

	void method(int variable) {
		variable = 10;
		System.out.println("Value of Instance variable :" + this.variable);
		System.out.println("Value of Local variable :" + variable);
	}

	void method() {
		int variable = 40;
		System.out.println("Value of Instance variable :" + this.variable);
		System.out.println("Value of Local variable :" + variable);
	}
}
```

上面程序的输出

```java
Value of Instance variable :5
Value of Local variable :10
Value of Instance variable :5
Value of Local variable :40
```

W
wizardforcel 已提交
85
## `this`关键字和构造器
W
init  
wizardforcel 已提交
86

W
wizardforcel 已提交
87
`this`关键字可以在构造器内使用,以调用同一类中的另一个重载构造器。 这称为显式构造器调用。 如果一个类有两个重载的构造器,一个不带参数,另一个不带参数,则会发生这种情况。 然后`this`关键字可用于从构造器中调用带有参数的构造器,而无需使用参数。 这是必需的,因为无法显式调用构造器。
W
init  
wizardforcel 已提交
88

W
wizardforcel 已提交
89
### `this`与构造器的示例
W
init  
wizardforcel 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

```java
class JBT {

	JBT() {
		this("JBT");
		System.out.println("Inside Constructor without parameter");
	}

	JBT(String str) {
		System.out
				.println("Inside Constructor with String parameter as " + str);
	}

	public static void main(String[] args) {
		JBT obj = new JBT();
	}
}
```

The output of the above program

```java
Inside Constructor with String parameter as JBT
Inside Constructor without parameter 
```

W
wizardforcel 已提交
117
如您所见,`this`可用于调用同一类中的重载构造器。
W
init  
wizardforcel 已提交
118

W
wizardforcel 已提交
119
**注意**
W
init  
wizardforcel 已提交
120

W
wizardforcel 已提交
121 122
*   `this`关键字只能是构造器中的第一个语句。
*   构造器可以具有`this``super`关键字,但不能同时具有这两个关键字。
W
init  
wizardforcel 已提交
123

W
wizardforcel 已提交
124
## `this`关键字和方法
W
init  
wizardforcel 已提交
125

W
wizardforcel 已提交
126
`this`关键字也可以在 Methods 内部使用,以从同一类调用另一个方法。
W
init  
wizardforcel 已提交
127

W
wizardforcel 已提交
128
### `this`关键字与方法的示例
W
init  
wizardforcel 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

```java
class JBT {

	public static void main(String[] args) {
		JBT obj = new JBT();
		obj.methodTwo();
	}
	void methodOne(){
		System.out.println("Inside Method ONE");
		}

	void methodTwo(){
		System.out.println("Inside Method TWO");
		this.methodOne();// same as calling methodOne()
	}
}
```

上面程序的输出

```java
Inside Method TWO
Inside Method ONE
```

W
wizardforcel 已提交
155
## `this`关键字作为方法参数的示例
W
init  
wizardforcel 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

```java
public class JBTThisAsParameter {

	public static void main(String[] args) {
		JBT1 obj = new JBT1();
		obj.i = 10;
		obj.method();
	}

}

class JBT1 extends JBTThisAsParameter {
	int i;

	void method() {
		method1(this);
	}

	void method1(JBT1 t) {
		System.out.println(t.i);
	}
} 
```

W
wizardforcel 已提交
181
如果您正确理解了`this`关键字,那么下一步应该是从 [Java 教程](https://javabeginnerstutorial.com/core-java-tutorial/)了解 Java 中的`static`关键字。
W
init  
wizardforcel 已提交
182 183

#### 参考文献
W
wizardforcel 已提交
184
1- [官方文档](https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html)
W
init  
wizardforcel 已提交
185