我什么时候应该使用“这个”在课堂上? [英] When should I use "this" in a class?

查看:216
本文介绍了我什么时候应该使用“这个”在课堂上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个是指当前对象。但我不知道什么时候才真正需要使用它。例如,如果我在某些方法中使用 x 而不是 this.x ,会有什么区别吗?可能 x 会引用一个考虑方法的本地变量?我的意思是只在这种方法中看到的变量。

I know that this refers to a current object. But I do not know when I really need to use it. For example, will be there any difference if I use x instead of this.x in some of the methods? May be x will refer to a variable which is local for the considered method? I mean variable which is seen only in this method.

this.method()怎么样?我可以用吗?我应该用它吗?如果我只使用 method(),默认情况下它不会应用于当前对象吗?

What about this.method()? Can I use it? Should I use it. If I just use method(), will it not be, by default, applied to the current object?

推荐答案

关键字主要用于三种情况。第一个也是最常见的是在setter方法中消除变量引用的歧义。第二种是当需要将当前类实例作为参数传递给另一个对象的方法时。第三种方法是从构造函数中调用备用构造函数。

The this keyword is primarily used in three situations. The first and most common is in setter methods to disambiguate variable references. The second is when there is a need to pass the current class instance as an argument to a method of another object. The third is as a way to call alternate constructors from within a constructor.

案例1:使用 this 消除变量引用的歧义。在Java setter方法中,我们通常传入一个与我们试图设置的私有成员变量同名的参数。然后我们将参数 x 分配给 this.x 。这清楚地表明您将参数name的值赋给实例变量name。

Case 1: Using this to disambiguate variable references. In Java setter methods, we commonly pass in an argument with the same name as the private member variable we are attempting to set. We then assign the argument x to this.x. This makes it clear that you are assigning the value of the parameter "name" to the instance variable "name".

public class Foo
{
    private String name;

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

案例2:使用作为传递给另一个对象的参数。

Case 2: Using this as an argument passed to another object.

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:使用调用备用构造函数。在评论中, trinithis 正确地指出了的另一种常见用法。当你有一个单独的类的多个构造函数时,你可以使用 this(arg0,arg1,...)来调用你选择的另一个构造函数,前提是你在你的构造函数的第一行。

Case 3: Using this to call alternate constructors. In the comments, trinithis correctly pointed out another common use of this. When you have multiple constructors for a single class, you can use this(arg0, arg1, ...) to call another constructor of your choosing, provided you do so in the first line of your constructor.

class Foo
{
    public Foo() {
        this("Some default value for bar");

        //optional other lines
    }

    public Foo(String bar) {
        // Do something with bar
    }
}

我还看过这个用来强调实例变量被引用的事实(没有消除歧义的需要),但在我看来这是一种罕见的情况。

I have also seen this used to emphasize the fact that an instance variable is being referenced (sans the need for disambiguation), but that is a rare case in my opinion.

这篇关于我什么时候应该使用“这个”在课堂上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆