ska*_*kaz 112 java android this
我正在进行Android编程并且正在学习Intents,当我看到一个构造函数时,对我的C#训练有素的思想,看起来很时髦.电话是:
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
Run Code Online (Sandbox Code Playgroud)
这两个参数对我来说都是新的.如何从班级名称中删除静态".this"?这是Java的东西还是Android的东西?我假设它只是说"this",因为我在上下文中CurrentActivity,但我不知道如何从类名本身调用"this".也.".class"看起来像是用于反射,我在C#中很熟悉,但对此的任何见解也会受到欢迎.
谢谢.
Cri*_*ian 134
通常,您只能使用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);
}
});
Run Code Online (Sandbox Code Playgroud)
DJC*_*rth 13
语法"Classname.this"适用于内部类.如果要引用类型为"Outerclass"的封闭实例,则将其设置为"Outerclass.this".
NextActivity.class只是描述类"NextActivity"的Class对象.
<ClassName>.this
Run Code Online (Sandbox Code Playgroud)
在嵌套类中用于指代封闭类的当前实例,因为“this”关键字指代嵌套类实例。
公共类 Siht {
类 NestedSiht {
无效演示这(){
System.err.println(" Siht.this' 是一个实例:" +this' is an instance of: " +
this.getClass().getName());
System.err.println("
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();
}
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
30346 次 |
| 最近记录: |