问题描述
我做机器人编程,并学习意图,当我看到一个构造函数,我的C#训练有素的头脑,显得时髦。该电话是:
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:
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
这两个参数是新的我。如何有一个静态的。这个关的类名?这是一个Java的东西或Android的事吗?我猜想,这是一样的只是说这个,因为我在 CurrentActivity
的背景下,但我不明白怎么这个可取消了类名本身。也。 .class的看起来是用来思考,而我熟悉C#,但是任何洞察到这将受到欢迎也是如此。
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.
感谢。
推荐答案
通常情况下,你只能使用这
。但是,有时这
引用了一个内部类......所以,例如:
Usually, you can use only this
. But, sometimes this
makes reference to an inner class... so, for example:
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);
}
});
这篇关于使用"这"与类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!