【发布时间】:2011-01-25 13:24:45
【问题描述】:
我知道this 指的是当前对象。但我不知道什么时候我真的需要使用它。例如,如果我在某些方法中使用x 而不是this.x,会有什么不同吗?可能是x 将引用所考虑方法的本地变量?我的意思是只有在这种方法中才能看到的变量。
this.method() 呢?我可以使用它吗?我应该使用它。如果我只使用method(),会不会默认应用到当前对象?
【问题讨论】:
我知道this 指的是当前对象。但我不知道什么时候我真的需要使用它。例如,如果我在某些方法中使用x 而不是this.x,会有什么不同吗?可能是x 将引用所考虑方法的本地变量?我的意思是只有在这种方法中才能看到的变量。
this.method() 呢?我可以使用它吗?我应该使用它。如果我只使用method(),会不会默认应用到当前对象?
【问题讨论】:
java中的“this”关键字用于引用当前类对象。
“this”关键字在java中有6种用法
【讨论】:
有很多好的答案,但还有另一个非常小的理由将this 放在任何地方。如果您尝试从普通文本编辑器(例如记事本等)打开源代码,使用this 将使阅读更加清晰。
想象一下:
public class Hello {
private String foo;
// Some 10k lines of codes
private String getStringFromSomewhere() {
// ....
}
// More codes
public class World {
private String bar;
// Another 10k lines of codes
public void doSomething() {
// More codes
foo = "FOO";
// More codes
String s = getStringFromSomewhere();
// More codes
bar = s;
}
}
}
使用任何现代 IDE 都非常清楚,但使用常规文本编辑器阅读这将是一场噩梦。
在您使用编辑器的“查找”功能之前,您将很难找到foo 所在的位置。然后你会出于同样的原因对getStringFromSomewhere() 尖叫。最后,在你忘记s是什么之后,bar = s会给你最后一击。
对比一下:
public void doSomething() {
// More codes
Hello.this.foo = "FOO";
// More codes
String s = Hello.this.getStringFromSomewhere();
// More codes
this.bar = s;
}
foo 是在外部类Hello 中声明的变量。getStringFromSomewhere() 也是在外部类中声明的方法。bar 属于World 类,而s 是在该方法中声明的局部变量。当然,每当您设计某物时,您都会创建规则。所以在设计你的 API 或项目时,如果你的规则包括“如果有人用记事本打开所有这些源代码,他或她应该朝自己的脑袋开枪”,那么你完全可以不这样做 /em>。
【讨论】:
关于William Brendel的帖子和dbconfessions的问题,关于案例2。这是一个例子:
public class Window {
private Window parent;
public Window (Window parent) {
this.parent = parent;
}
public void addSubWindow() {
Window child = new Window(this);
list.add(child);
}
public void printInfo() {
if (parent == null) {
System.out.println("root");
} else {
System.out.println("child");
}
}
}
在与对象建立父子关系时,我见过这种用法。但是,请注意,为了简洁起见,它被简化了。
【讨论】:
this 在构建器模式中很有用。
public class User {
private String firstName;
private String surname;
public User(Builder builder){
firstName = builder.firstName;
surname = builder.surname;
}
public String getFirstName(){
return firstName;
}
public String getSurname(){
return surname;
}
public static class Builder {
private String firstName;
private String surname;
public Builder setFirstName(String firstName) {
this.firstName = firstName;
return this;
}
public Builder setSurname(String surname) {
this.surname = surname;
return this;
}
public User build(){
return new User(this);
}
}
public static void main(String[] args) {
User.Builder builder = new User.Builder();
User user = builder.setFirstName("John").setSurname("Doe").build();
}
}
【讨论】:
@William Brendel 的回答很好地提供了三种不同的用例。
用例 1:
this 上的官方 java 文档页面提供了相同的用例。
在实例方法或构造函数中,this 是对当前对象的引用——正在调用其方法或构造函数的对象。您可以使用 this 从实例方法或构造函数中引用当前对象的任何成员。
它涵盖了两个例子:
将此与字段一起使用和将其与构造函数一起使用
用例 2:
本文未引用的其他用例:this 可用于同步多线程应用程序中的当前对象,以保护数据和方法的关键部分。
synchronized(this){
// Do some thing.
}
用例 3:
Builder 模式的实现依赖于使用this 来返回修改后的对象。
参考这篇文章
【讨论】:
this 关键字主要用于三种情况。第一个也是最常见的是在 setter 方法中消除变量引用的歧义。第二个是当需要将当前类实例作为参数传递给另一个对象的方法时。第三种是作为从构造函数中调用备用构造函数的一种方式。
案例 1: 使用 this 消除变量引用的歧义。在 Java setter 方法中,我们通常传入一个与我们试图设置的私有成员变量同名的参数。然后我们将参数x 分配给this.x。这清楚地表明您正在将参数“name”的值分配给实例变量“name”。
public class Foo
{
private String name;
public void setName(String name) {
this.name = name;
}
}
案例 2: 使用 this 作为参数传递给另一个对象。
public class Foo
{
public String useBarMethod() {
Bar theBar = new Bar();
return theBar.barMethod(this);
}
public String getName() {
return "Foo";
}
}
public class Bar
{
public void barMethod(Foo obj) {
obj.getName();
}
}
案例 3: 使用 this 调用备用构造函数。在 cmets 中,trinithis 正确指出了 this 的另一个常见用法。当一个类有多个构造函数时,您可以使用this(arg0, arg1, ...) 调用您选择的另一个构造函数,前提是您在构造函数的第一行这样做。
class Foo
{
public Foo() {
this("Some default value for bar");
//optional other lines
}
public Foo(String bar) {
// Do something with bar
}
}
我还看到this 用于强调引用实例变量这一事实(无需消除歧义),但在我看来这种情况很少见。
【讨论】:
this(arg1, arg2, ...)。
this 的另一个常见用法,因此我将其添加到我的答案中。我认为这没有任何问题,因为最终结果是总体上更好的答案,这正是 SO 的目的。我也尽可能地给予信任,就像我在 trinithis 的情况下所做的那样。
this 仅在确实需要消歧时才使用,就像我上面的 setter 示例一样。当然,编码风格和“最佳实践”可能会因您询问的对象而有很大差异,但总的来说,我建议选择合理的模式并坚持下去。一致性,即使只是在单个代码库内部,也有助于提高可读性和可维护性。
以下是在java中使用'this'关键字的方法:
this关键字引用当前类实例变量this() 调用当前类构造函数this关键字返回当前类实例this关键字作为方法参数https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
【讨论】:
this 是对当前对象的引用。它在构造函数中用于区分同名的本地和当前类变量。例如:
public class circle {
int x;
circle(int x){
this.x =x;
//class variable =local variable
}
}
this 也可用于从另一个构造函数调用一个构造函数。例如:
public class circle {
int x;
circle() {
this(1);
}
circle(int x) {
this.x = x;
}
}
【讨论】:
确保使用当前对象的成员。在关注线程安全的情况下,某些应用程序可能会更改错误的对象成员值,因此应该将其应用于成员,以便使用正确的对象成员值。
如果您的对象不关心线程安全,则没有理由指定使用哪个对象成员的值。
【讨论】:
“this”在从另一个构造函数调用时也很有用:
public class MyClass {
public MyClass(String foo) {
this(foo, null);
}
public MyClass(String foo, String bar) {
...
}
}
【讨论】:
当有两个变量一个实例变量和另一个同名的局部变量时,我们使用这个。引用当前执行对象以避免名称之间的冲突。
【讨论】:
this 的第二个重要用途(除了像许多答案已经说过的那样用局部变量隐藏)是从嵌套的非静态类访问外部实例时:
public class Outer {
protected int a;
public class Inner {
protected int a;
public int foo(){
return Outer.this.a;
}
public Outer getOuter(){
return Outer.this;
}
}
}
【讨论】:
this 不会影响生成的代码 - 它是编译时运算符,无论有没有它生成的代码都是相同的。当你必须使用它时,取决于上下文。例如,正如您所说,当您有隐藏类变量的局部变量并且您想要引用类变量而不是本地变量时,您必须使用它。
编辑:“生成的代码将是相同的”我的意思当然是,当局部范围内的某些变量不隐藏属于类的变量时。因此
class POJO {
protected int i;
public void modify() {
i = 9;
}
public void thisModify() {
this.i = 9;
}
}
两种方法的结果代码相同。不同之处在于,如果某些方法声明了同名的局部变量
public void m() {
int i;
i = 9; // i refers to variable in method's scope
this.i = 9; // i refers to class variable
}
【讨论】:
Google turned up a page on the Sun site that discusses this a bit.
关于变量你是对的; this 确实可以用来区分方法变量和类字段。
private int x;
public void setX(int x) {
this.x=x;
}
但是,我真的讨厌这种约定。给两个不同的变量赋予字面上相同的名称是错误的秘诀。我更喜欢以下内容:
private int x;
public void setX(int newX) {
x=newX;
}
结果相同,但不会出现错误,即当您真正打算引用 x 时,您不小心引用了 x。
至于将其与方法一起使用,您对效果是正确的;无论有没有它,你都会得到相同的结果。你能用吗?当然。你应该使用它吗?由你决定,但鉴于我个人认为这是毫无意义的冗长,不会增加任何清晰度(除非代码中塞满了静态导入语句),我不倾向于自己使用它。
【讨论】:
您只需要使用this - 大多数人只使用它 - 当存在同名的重叠局部变量时。 (例如 Setter 方法。)
当然,使用 this 的另一个很好的理由是它会导致智能感知在 IDE 中弹出:)
【讨论】:
如果我在某些方法中使用“x”而不是“this.x”会有什么不同吗?
通常不会。但有时它会有所不同:
class A {
private int i;
public A(int i) {
this.i = i; // this.i can be used to disambiguate the i being referred to
}
}
如果我只使用“method()”,会不会默认应用到当前对象?
是的。但如果需要,this.method() 会澄清调用是由该对象进行的。
【讨论】:
唯一需要使用this. 限定符是当前范围内的另一个变量共享相同名称并且您想要引用实例成员(如William 所述)。除此之外,x 和 this.x 之间的行为没有区别。
【讨论】:
this.x 让您的代码阅读更清晰一些,代码的可维护性/可读性也是您应该考虑的一个因素...
除非你有重叠的变量名,否则它真的只是为了在你阅读代码时清晰。
【讨论】:
this 关键字时,它只是不必要的样板代码,使代码更难阅读。
this 不会使代码更难阅读 lmao。