嘿:)我遇到了变量问题。我想知道 run() 方法如何看到“l”和“a”。
public static void startDraw(String[] players) {
final DrawGame a = new DrawGame(players);
for ( Player b : a.getPlayers()){
final Player l = b;
Runnable q = new go();
}
}
protected static class go implements Runnable{
public go(){
}
public void run(){
while (a.finished()!= true){
l.drawLine();
}
}
Thread t = new Thread(new go()).start();
}
最佳答案
I wanna know how the methode run() can see 'l' and 'a'?
因为你的内部类go
是方法本地内部类,而a
和l
是同一方法中的方法本地最终变量去()
如果将变量声明为final,内部类知道它们不会改变,因此他们可以复制它供私有(private)使用,该副本将在原始值从堆栈中删除后存在。
关于java - 在内部类运行中使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23492493/