今天再看一个Android LunchList的tutorial
发现有两种写法:
都是在 LunchList 这个类里,不同的方法
new Intent(this, EditPreferences.class);
和
new Intent(LunchList.this, DetailForm.class);
LunchList.this和this有什么区别?
如果你的程序里面包含了内部匿名类之类,比如线程
class A {
public void callSomething(){
new Thread(){
public void run(){
// 这里的this 代表 匿名类的上下文, A.this 代表A实例化后对象的上下文
}
}.start();
}
}
这应该是在内部类里面调用的吧
如果在内部类里面的this就是这个内部类的实例,而OuterClassName.this就是它外面的那个类的实例
看起来是Android编程吧,应该是写了一个listener的匿名类?
如果是的话,那么this就是指这个listener实例,而LunchList.this就是外面的类,可能是Activity吧
http://stackoverflow.com/questions/4080868/java-classname-this
正文完