java 将此作为参数传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11256158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:24:42  来源:igfitidea点击:

Passing this as a parameter

javaparametersthiskeyword

提问by eosd441

I've seen code that have passed the keyword thisas an input parameter. For instance:

我见过将关键字this作为输入参数传递的代码。 例如:

getContainer(this);

What does the keyword thismean? I've heard that it refers to an instance of the class itself (or something along those lines), but how does that work?

关键字this是什么意思?我听说它指的是类本身的一个实例(或类似的东西),但它是如何工作的?

回答by Andreas Johansson

From 15.8.3 of the java specification:

从 Java 规范的 15.8.3 开始:

The keyword this may be used only in the body of an instance method, instance initializer or constructor, or in the initializer of an instance variable of a class. If it appears anywhere else, a compile-time error occurs. When used as a primary expression, the keyword this denotes a value that is a reference to the object for which the instance method was invoked (§15.12), or to the object being constructed. The type of this is the class C within which the keyword this occurs. At run time, the class of the actual object referred to may be the class C or any subclass of C.

[...]

The keyword this is also used in a special explicit constructor invocation statement, which can appear at the beginning of a constructor body (§8.8.7).

关键字 this 只能用在实例方法、实例初始值设定项或构造函数的主体中,或者在类的实例变量的初始值设定项中使用。如果它出现在其他任何地方,则会发生编译时错误。当用作主要表达式时,关键字 this 表示一个值,该值是对调用实例方法的对象(第 15.12 节)或正在构造的对象的引用。this 的类型是关键字 this 所在的类 C。在运行时,所引用的实际对象的类可能是类 C 或 C 的任何子类。

[...]

关键字 this 也用在特殊的显式构造函数调用语句中,它可以出现在构造函数体的开头(第 8.8.7 节)。

So yeah, a class can use the this keyword to refer to itself. The thiskeyword is also required when a local variable within a method has the same name as a class member variable, to distinguish them both.

所以是的,一个类可以使用 this 关键字来引用它自己。this当方法中的局部变量与类成员变量同名时,也需要使用关键字来区分它们。

回答by Jigar Joshi

thisrefers to current instance on which method has been invoked,

this指已调用方法的当前实例,

So it passes the reference to current instance invoking that member method

因此它将引用传递给调用该成员方法的当前实例

class MyRunnable implements Runnable {
    Thread t;
    public MyRunnable() {
        this.t = new Thread(this); //here it passes reference to current instance of `Runnable`
    }

    public void run() {

    }
}

回答by Sumukh

thiscan also be passed to pass the instance of the calling class to the called class. for example

this也可以通过将调用类的实例传递给被调用类。例如

public class Caller{
        public void callerClassMethod1(){
            new Called(this).
        }

        public void callerClassMethod2(){
        }
    }

class Called{
    private Caller caller;
    public Called(Caller caller){
       this.caller=caller;
    }
    public void calledClassMethod1(){
        //.... Do something before
        caller.callerClassMethod2();
        //..... Do something after        

    }
}

This way you can separate the responsibilities of the 2 classes and at the same time keep them coupled.

通过这种方式,您可以分离 2 个类的职责,同时保持它们耦合。

回答by me_digvijay

In java we have classes and create instances (objects) of those classes. So when the object needs to point or reference itself then the thiskeyword is used.

在 Java 中,我们有类并创建这些类的实例(对象)。因此,当对象需要指向或引用自身时,将使用this关键字。