爲什麼我不能訪問我的變量p
在mull
類的iterate
方法?如何使公共靜態方法可以訪問實例變量?
public class mull {
public static void main(String[] args) throws InterruptedException {
final JPanel p = createAndShowGUI();
Timer timer = new Timer(1000, new MyTimerActionListener());
timer.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
timer.stop();
public static void iterate(){
for (int i = 0; i < 55; i++){
// "p cannot be resolved"
p.moveSquare(i*10, i*10);
p.setParamsRing(i*5, i*7, 200, 200);
}
}
}
class MyPanel extends JPanel {
....
}
爲什麼Eclipse的強迫我用這個:
((MyPanel) p).setParamsRing(i*5, i*7, 200, 200);
代替:
p.setParamsRing(i*5, i*7, 200, 200);
?
因爲p是JPanel類型,它沒有任何方法setParamsRing()。 – Tudor
回答標題中的問題,因爲這是一個矛盾的詞彙。 – EJP