【发布时间】:2013-12-17 19:39:45
【问题描述】:
class Param implements Runnable {
int c;
public Param(int a, int b){
c = a+b;
}
public void run(){
// System.out.println(a); // <<<<----What I'm going to do. it's impossible.
}
}
public class ParamTest {
public static void main(String args[]){
Runnable r = new ThreadParam(10,20);
new Thread(r).start();
}
}
在上面的代码中,如何打印“a”的值?我的目标是,当我在 main 方法中调用 Runnable r = new ThreadParam(10,20) 时,我想打印 "a" --> 10 的值。这是不可能的吗?如果我在 run() 方法中声明int a,“a”的结果是“0”。我想要结果“10”。我该怎么做?
【问题讨论】:
-
更改您的设计并添加一个实例变量来存储
a的值。 -
@RahulTripathi 那是我的问题。但我遇到了另一个问题:这个问题。请考虑一下,谢谢。
标签: java