这个关键字及其工作原理
this keyword and how it works
我有一个关于在方法中使用关键字 this 和变量范围的问题。
总的来说,我知道如何使用这个关键字,但是当观察到下面所有 3 个选项在方法 balance 中的相同结果时,我感到困惑。
问题是,什么是 option 的正确实现以及为什么它以相同的结果对待所有选项。
是不是说如果方法balance中没有局部变量,这个关键字就被忽略了?
非常感谢!
选项#1
public int balance(int balance) {
this.age = this.age + balance;
return age;
}
选项#2
public int balance(int balance) {
age = age + balance;
return age;
}
选项#3
public int balance(int balance) {
age = age + balance;
return this.age;
}
代码
package com;
public class Elephant {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
if (age > 0) {
this.age = age;
}
}
public int balance(int balance) {
age = age + balance;
return age;
}
public int getAge() {
return age;
}
public Elephant(String name, int age) {
this.name = name;
if (age > 0) {
this.age = age;
}
}
}
package com;
import java.util.Scanner;
public class MainClass {
public static void main(String arg[]) {
Elephant e1 = new Elephant("Elephant1: ", 7);
System.out.printf("Elephant name: %s age: %s \n", e1.getName(), e1.getAge());
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
e1.balance(i);
System.out.printf("Entered deposit for e1: %d \n", i);
System.out.printf("Balance for e1: %s", e1.getAge());
}
}
所有 3 个选项的结果都相同:
大象名称:Elephant1:年龄:7
11
为 e1 输入存款:11
e1 的余额:18
在Java中,this
始终是对当前对象的引用。无需显式提及this
即可访问当前对象的对象属性和方法。您想要使用哪一个(提及或不提及 this
),通常只是一个清晰度和编码风格指南的问题。
另见:
- What is the accepted style for using the
this
keyword in Java?
- When should I use “this” in a class?
- Using the this Keyword(Oracle Java 文档)
除了需要从其实例方法内部传递或存储对对象的引用的情况外,在解析不合格的名称需要应用消歧规则时,您还需要关键字 this
。
例如,这段代码需要它:
public void setAge(int age) {
if (age > 0) {
this.age = age;
}
}
标识符age
可以引用成员字段age
或参数age
。编译器应用参数和局部变量 "shadow" 字段具有相同名称的规则来消除歧义。这就是为什么您必须在作业中使用 this
作为 age
前缀;否则,不会分配该字段。
另一方面,您的 balance
方法根本不需要关键字 this
,因为它没有歧义名称解析。
我有一个关于在方法中使用关键字 this 和变量范围的问题。 总的来说,我知道如何使用这个关键字,但是当观察到下面所有 3 个选项在方法 balance 中的相同结果时,我感到困惑。 问题是,什么是 option 的正确实现以及为什么它以相同的结果对待所有选项。 是不是说如果方法balance中没有局部变量,这个关键字就被忽略了?
非常感谢!
选项#1
public int balance(int balance) {
this.age = this.age + balance;
return age;
}
选项#2
public int balance(int balance) {
age = age + balance;
return age;
}
选项#3
public int balance(int balance) {
age = age + balance;
return this.age;
}
代码
package com;
public class Elephant {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
if (age > 0) {
this.age = age;
}
}
public int balance(int balance) {
age = age + balance;
return age;
}
public int getAge() {
return age;
}
public Elephant(String name, int age) {
this.name = name;
if (age > 0) {
this.age = age;
}
}
}
package com;
import java.util.Scanner;
public class MainClass {
public static void main(String arg[]) {
Elephant e1 = new Elephant("Elephant1: ", 7);
System.out.printf("Elephant name: %s age: %s \n", e1.getName(), e1.getAge());
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
e1.balance(i);
System.out.printf("Entered deposit for e1: %d \n", i);
System.out.printf("Balance for e1: %s", e1.getAge());
}
}
所有 3 个选项的结果都相同: 大象名称:Elephant1:年龄:7 11 为 e1 输入存款:11 e1 的余额:18
在Java中,this
始终是对当前对象的引用。无需显式提及this
即可访问当前对象的对象属性和方法。您想要使用哪一个(提及或不提及 this
),通常只是一个清晰度和编码风格指南的问题。
另见:
- What is the accepted style for using the
this
keyword in Java? - When should I use “this” in a class?
- Using the this Keyword(Oracle Java 文档)
除了需要从其实例方法内部传递或存储对对象的引用的情况外,在解析不合格的名称需要应用消歧规则时,您还需要关键字 this
。
例如,这段代码需要它:
public void setAge(int age) {
if (age > 0) {
this.age = age;
}
}
标识符age
可以引用成员字段age
或参数age
。编译器应用参数和局部变量 "shadow" 字段具有相同名称的规则来消除歧义。这就是为什么您必须在作业中使用 this
作为 age
前缀;否则,不会分配该字段。
另一方面,您的 balance
方法根本不需要关键字 this
,因为它没有歧义名称解析。