我想在第一次调用sleep()之后刷新方法状态
这是我尝试过的
public class MyClass {
private static boolean eatCalled = false;
private static boolean sleepCalled = false;
public static void main(String args[]) {
eat();
sleep();
sleep();
}
private static void eat(){
System.out.println("Om nom nom");
eatCalled = true;
}
private static void sleep(){
sleepCalled = true;
System.out.println("Z-z-z...");
if(eatCalled){
System.out.println("Good");
sleepCalled = false;
}
}
}
我希望看到:Om nom nom,Z-z-z好,Z-z-z。但是我得到了Om nom nom,Z-z-z Good,Z-z-z Good。我怎样才能得到想要的?
最佳答案
更改
if(eatCalled){
System.out.println("Good");
sleepCalled = false;
}
至
if(eatCalled){
System.out.println("Good");
eatCalled= false;
}
关于java - 如何刷新方法状态?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57231455/