Java 使用带有类名的“this”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4080868/
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-08-14 11:23:28  来源:igfitidea点击:

Using "this" with class name

javaandroidthis

提问by skaz

I am doing Android programming and was learning about Intents, when I saw a constructor that, to my C# trained mind, seemed funky. The call was:

我正在做 Android 编程并且正在学习 Intent,当我看到一个构造函数时,在我受过 C# 训练的头脑中,它看起来很时髦。电话是:

Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);

Both of the parameters are new to me. How is there a static ".this" off of a Class Name? Is this a Java thing or an Android thing? I am assuming that it is the same as just saying "this", since I am in the context of CurrentActivity, but I don't get how the "this" can be called off of the Class name itself. Also. The ".class" looks like it is used for reflection, which I am familiar with in C#, but any insight into this would be welcomed as well.

这两个参数对我来说都是新的。类名中如何有一个静态的“.this”?这是 Java 的东西还是 Android 的东西?我假设它与仅说“this”相同,因为我处于 的上下文中CurrentActivity,但我不明白如何从类名本身中调用“this”。还。“.class”看起来像是用于反射,我在 C# 中很熟悉,但任何对此的见解也将受到欢迎。

Thanks.

谢谢。

采纳答案by Cristian

Usually, you can use only this. But, sometimes thismakes reference to an inner class... so, for example:

通常,您只能使用this. 但是,有时this会引用内部类......所以,例如:

Button button = (Button)findViewById(R.id.ticket_details_sell_ticket);
button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // it will be wrong to use only "this", because it would
        // reference the just created OnClickListener object
        Intent login = new Intent(ClassName.this, Login.class);
        startActivityForResult(login, LOGIN_REQUEST);
    }
});

回答by akf

ClassName.thisis used to reference the current instance of an outerclass from an inner class.

ClassName.this用于从内部类引用外部类的当前实例。

回答by DJClayworth

The syntax "Classname.this" is for inner classes. If you want to refer to the enclosing instance of type "Outerclass" then you do it as "Outerclass.this".

语法“Classname.this”用于内部类。如果你想引用“Outerclass”类型的封闭实例,那么你可以将它作为“Outerclass.this”。

NextActivity.class is simply the Class object that describes class "NextActivity".

NextActivity.class 只是描述类“NextActivity”的 Class 对象。

回答by codymanix

NextActivity.classin java means typeof(NextActivity)in C#

NextActivity.class在 Java 中意味着typeof(NextActivity)在 C# 中

回答by Yishai

One at a time:

一次一个:

The first construct is called a qualified this. The purpose of the syntax is in the case where you are in an inner class (typically an anonymous inner class) and you want to reference the thisof the outer class rather than the thisof the (anonymous) inner class. The "qualified this" can only be used in a context where thiswould be ambiguous. The quote the JLS "It is a compile-time error if the expression occurs in a class or interface which is not an inner class of class T or T itself".

第一个构造称为限定的 this。语法的目的是在您处于内部类(通常是匿名内部类)中并且您想要引用this外部类的 而不是this(匿名)内部类的 的情况下。“限定此”只能用于this不明确的上下文中。JLS 引用“如果表达式出现在不是类 T 或 T 本身的内部类的类或接口中,则为编译时错误”。

The second construct is called a class literalis the way to reference the Class object that represents that type. It can be used in any context.

第二个构造称为 aclass literal是引用表示该类型的 Class 对象的方法。它可以在任何上下文中使用。

回答by khachik

<ClassName>.this

is used in nested classes to refer to the current instance of the enclosing class, since the `this' keyword refers to the nest class instance.

在嵌套类中用于指代封闭类的当前实例,因为“this”关键字指代嵌套类实例。

public class Siht {
    class NestedSiht {
        void demoThis() {
            System.err.println("this' is an instance of: " + 
                            this.getClass().getName());
            System.err.println("Siht.this' is an instance of: " +
                            Siht.this.getClass().getName());
        }
    }

void demoThis() {
    new java.lang.Object() {
        void demoThis() {
            System.err.println("`this' is an instance of: " + 
                            this.getClass().getName());
            System.err.println("`Siht.this' is an instance of: " +
                            Siht.this.getClass().getName());
        }
    }.demoThis();
    new NestedSiht().demoThis();
}

public static void main(String [] args) {
    new Siht().demoThis();
}

}

回答by Gaetano

It's confusing only because when you use "MainActivity.this", it seems that you are referring to the class and not the object. In reality when you use "this" you are always referring to the current object, as the java se documentation states:

之所以令人困惑,只是因为当您使用“ MainActivity.this”时,您似乎指的是类而不是对象。实际上,当您使用“this”时,您总是指的是当前对象,如 java se 文档所述:

https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

Within an instance method or a constructor, thisis a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

在实例方法或构造函数中,this对当前对象的引用——正在调用其方法或构造函数的对象。您可以使用 this 从实例方法或构造函数中引用当前对象的任何成员。

It's just syntactically peculiar.

它只是在语法上很奇特。