"return name;" 和 "return this.name" 有什么区别
what is difference in "return name;" and "return this.name"
我正在尝试 Jacco 测试,我能够从名为 Student
的 class 测试 getStudentId
,它具有:
public String getStudentId() {
return studentId;
}
当我尝试测试另一个名为 Product
的 class 时,我得到一个错误 - 两者之间的唯一区别在于 getX
方法。 Product
的getName
方法是:
public String getName() {
return this.name;
}
错误消息显示:
constructor Product in class Product cannot be applied to given types
您有可能将 studentID 设置为 public 变量。每当您使用 this.whatever 到 return 来自 getX 函数的变量时,this.暗示它是一个私有变量。 studentID 很可能是 public,这就是为什么你在它前面没有 'this.' 的情况下逃脱了。
关键字 this
引用了对象的实例 您当前所在的 。想象一下有这样一个 class:
public class A {
private String property;
public void changeProperty(String property) {
this.property = property
}
}
方法外的变量名属性没有歧义,引用了classA
的成员变量。但它在方法 changeProperty
内部是模棱两可的,因为还有名为 属性.
的参数
Java如何解决这个冲突?如果你只是输入 property
你将总是引用范围更小的对象,所以方法的参数而不是成员变量。通过使用 this.property
你可以再次引用成员变量。
如果您的对象中没有此类冲突,就像在您的示例中一样,那么您不需要 this 语句并且 this.name
与 name
.
然而,为了防止非常讨厌的错误,在引用成员变量时总是可以使用 this
,这是一种很好的做法。想象一下,你将来创建一个有这样一个名称冲突的方法而忘记了成员变量,哎呀你很容易创建一个难以调试的错误。
有些程序员甚至走得更远,总是给成员变量起不同于参数的名称,以防止此类名称冲突。例如成员变量通常被命名为:
- mProperty 或
- _属性
请注意,方法 this(...)
引用了自己对象的构造函数。它可以在构造函数中用于将任务传递给另一个构造函数,如:
public class A {
public A(String fileName) {
this(new File(fileName), true);
}
public A(File file) {
this(file, true);
}
public A(File file, boolean doSomething) {
// Code ...
}
}
类似地,还有关键字 super
引用 parent-class。例如:
public class A {
protected String property;
}
public class B extends A {
private String property;
public void foo() {
// Property of B
System.out.println(property);
// The same
System.out.println(this.property);
// Property of A
System.out.println(super.property);
}
}
这个关键字也可以用来引用parent-constructor或者父class.
的其他方法
所以总而言之,它只是关于解决这样的名称冲突。
现在我们知道了,很容易看出您发布的代码不包含错误。
当您使用 this.name 时,您使用的是在 class 中定义的属性,即属性名称。但是,当您仅使用名称时,它可以是代码中如此调用的任何变量,甚至是属性。示例:
public String getName(){
String name = "Mery";
this.name = "Jacob";
return name;
}
此方法return 值"Mery"。如果你输入 return this.name 那么你 return 值 "Jacob".
我正在尝试 Jacco 测试,我能够从名为 Student
的 class 测试 getStudentId
,它具有:
public String getStudentId() {
return studentId;
}
当我尝试测试另一个名为 Product
的 class 时,我得到一个错误 - 两者之间的唯一区别在于 getX
方法。 Product
的getName
方法是:
public String getName() {
return this.name;
}
错误消息显示:
constructor Product in class Product cannot be applied to given types
您有可能将 studentID 设置为 public 变量。每当您使用 this.whatever 到 return 来自 getX 函数的变量时,this.暗示它是一个私有变量。 studentID 很可能是 public,这就是为什么你在它前面没有 'this.' 的情况下逃脱了。
关键字 this
引用了对象的实例 您当前所在的 。想象一下有这样一个 class:
public class A {
private String property;
public void changeProperty(String property) {
this.property = property
}
}
方法外的变量名属性没有歧义,引用了classA
的成员变量。但它在方法 changeProperty
内部是模棱两可的,因为还有名为 属性.
Java如何解决这个冲突?如果你只是输入 property
你将总是引用范围更小的对象,所以方法的参数而不是成员变量。通过使用 this.property
你可以再次引用成员变量。
如果您的对象中没有此类冲突,就像在您的示例中一样,那么您不需要 this 语句并且 this.name
与 name
.
然而,为了防止非常讨厌的错误,在引用成员变量时总是可以使用 this
,这是一种很好的做法。想象一下,你将来创建一个有这样一个名称冲突的方法而忘记了成员变量,哎呀你很容易创建一个难以调试的错误。
有些程序员甚至走得更远,总是给成员变量起不同于参数的名称,以防止此类名称冲突。例如成员变量通常被命名为:
- mProperty 或
- _属性
请注意,方法 this(...)
引用了自己对象的构造函数。它可以在构造函数中用于将任务传递给另一个构造函数,如:
public class A {
public A(String fileName) {
this(new File(fileName), true);
}
public A(File file) {
this(file, true);
}
public A(File file, boolean doSomething) {
// Code ...
}
}
类似地,还有关键字 super
引用 parent-class。例如:
public class A {
protected String property;
}
public class B extends A {
private String property;
public void foo() {
// Property of B
System.out.println(property);
// The same
System.out.println(this.property);
// Property of A
System.out.println(super.property);
}
}
这个关键字也可以用来引用parent-constructor或者父class.
的其他方法所以总而言之,它只是关于解决这样的名称冲突。
现在我们知道了,很容易看出您发布的代码不包含错误。
当您使用 this.name 时,您使用的是在 class 中定义的属性,即属性名称。但是,当您仅使用名称时,它可以是代码中如此调用的任何变量,甚至是属性。示例:
public String getName(){
String name = "Mery";
this.name = "Jacob";
return name;
}
此方法return 值"Mery"。如果你输入 return this.name 那么你 return 值 "Jacob".