我有一个关于如何使用静态的问题,我看到了一个示例:
public class Exe {
static int i = 47;
public void call() {
for (i = 0; i < 3; i++) {
if (i == 2) {
System.out.println("\n");
}
}
}
public Exe() {
}
public static void main(String[] args) {
Exe t1 = new Exe();
Exe t2 = new Exe();
t2.i = 60;
System.out.println(t1.i);
t1.call();
System.out.println(t2.i);
t2.call();
}
}
当我尝试运行它时,它打印了 60 3,我想知道为什么 t2.i 这里是 3,我不知道 3 从哪里来,也不知道 t1.call() 和 t2 的结果。 call() 没有打印出来,请指教,谢谢!
最佳答案
for(i=0; i<3;i++){
if(i==2){
System.out.println("\n");
}
}
您的静态变量在此处分配/递增。
不是for循环中的i(您假设它是不同的)。
要清除云彩,只需使用另一个名为 j
的变量并执行循环即可。
for (int j = 0; j < 3; j++) {
if (j == 2) {
System.out.println("\n");
}
}
关于Java静态对象/方法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19031684/