【发布时间】:2013-01-06 20:10:17
【问题描述】:
我正在测试下面的sn-p,我需要知道如何访问t.x 或t.hello?它的范围是什么? 开发者会这样定义变量吗?
public class Test{
public Test(){
System.out.print("constructor\n");
}
public static void main(String[] args) {
Test t = new Test(){
int x = 0;
//System.out.print("" + x);
void hello(){
System.out.print("inside hello\n");
}
};
}
编辑
但是为什么这个 sn-p 有效
Thread tr = new Thread() {
int loops = 1;
@Override
public void run() {
loops += 1;
}
};
tr.start();
【问题讨论】:
-
有趣的是,我在回答中给出的示例与您的编辑非常接近;-)
-
回复:您的编辑。
start()是Thread的一个方法。变量tr也是Thread类型,所以你可以调用它的方法。只是不是您在 AIC 中添加的新方法。