如何在Java中写出简短的逻辑表达式?
How to write short logical expressions in Java?
有没有办法缩短对同一个变量的连续逻辑运算?
示例:
if (animation.getStatus() == Animation.Status.PAUSED || animation.getStatus() == Animation.Status.STOPPED) {
animation.playFromStart();
} else if (animation.getStatus() == Animation.Status.RUNNING) {
animation.stop();
}
您在 if 子句中看到我检查了 animation.getStatus()
两次,一次是暂停,一次是停止。
有没有办法让它像 animation.getStatus() == Animation.Status.PAUSED || Animation.Status.STOPPED
?
我确定这个问题已经有人问过了,但我真的不知道要搜索什么,所以如果这是重复的,我很抱歉。
没有; Java 语法是不可变的。
有多种选择,但最简单的是重构,作为奖励使其清晰易读,例如
if (animation.shouldReplay()) {
animation.playFromStart();
} else if (animation.shouldStop() {
animation.stop();
}
或更深一层,例如
animation.controlFromCurrentStatus();
如果您不愿意封装,只需导入状态即可:
Animation.Status currentStatus = animation.getStatus();
if (currentStatus == PAUSED || currentStatus == STOPPED) {
animation.playFromStart();
} else if (currentStatus == RUNNING) {
animation.stop();
}
或者让它们成为 enum
s,无论如何它们都应该是:
switch (currentStatus) {
case PAUSED:
case STOPPED:
animation.playFromStart();
break;
case RUNNING:
animation.stop();
}
您可以通过以下几种方式完成:
- 通过引入一个临时变量 - 创建一个变量,为其分配状态,并在表达式中使用变量的值而不是多次调用
- 通过编写辅助方法 - 编写一个获取当前状态和状态变量参数列表的方法,并在条件中调用此方法。
这是第一种方法的说明:
Animation.Status status = animation.getStatus();
if (status == Animation.Status.PAUSED || status == Animation.Status.STOPPED) {
animation.playFromStart();
} else if (status == Animation.Status.RUNNING) {
animation.stop();
}
这是第二种方法的说明:
private static boolean checkStatus(Animation.Status status, Animation.Status... expected) {
for (Animation.Status e : expected) {
if (e == status) {
return true;
}
}
return false;
}
...
if (checkStatus(animation.getStatus(), Animation.Status.PAUSED, Animation.Status.STOPPED)) {
...
}
在这种情况下 switch
语句看起来不错
switch (animation.getStatus()) {
case Animation.Status.PAUSED:
case Animation.Status.STOPPED:
animation.playFromStart();
break;
case Animation.Status.RUNNING:
animation.stop();
break;
}
如果您的代码是多线程的,如果您是 运行 动画并且希望能够 stop/start/pause 它们,那么您应该考虑同步。
因此,采用 Dave Newton 的代码片段并使其成为线程安全的:
synchronized(this){
if (animation.shouldReplay()) {
animation.playFromStart();
} else if (animation.shouldStop() {
animation.stop();
}
}
因此,没有危险第一个条件会return假,然后当前线程不再运行,另一个线程修改动画的状态,然后当前线程再次变为可运行并尝试停止动画。
有没有办法缩短对同一个变量的连续逻辑运算?
示例:
if (animation.getStatus() == Animation.Status.PAUSED || animation.getStatus() == Animation.Status.STOPPED) {
animation.playFromStart();
} else if (animation.getStatus() == Animation.Status.RUNNING) {
animation.stop();
}
您在 if 子句中看到我检查了 animation.getStatus()
两次,一次是暂停,一次是停止。
有没有办法让它像 animation.getStatus() == Animation.Status.PAUSED || Animation.Status.STOPPED
?
我确定这个问题已经有人问过了,但我真的不知道要搜索什么,所以如果这是重复的,我很抱歉。
没有; Java 语法是不可变的。
有多种选择,但最简单的是重构,作为奖励使其清晰易读,例如
if (animation.shouldReplay()) {
animation.playFromStart();
} else if (animation.shouldStop() {
animation.stop();
}
或更深一层,例如
animation.controlFromCurrentStatus();
如果您不愿意封装,只需导入状态即可:
Animation.Status currentStatus = animation.getStatus();
if (currentStatus == PAUSED || currentStatus == STOPPED) {
animation.playFromStart();
} else if (currentStatus == RUNNING) {
animation.stop();
}
或者让它们成为 enum
s,无论如何它们都应该是:
switch (currentStatus) {
case PAUSED:
case STOPPED:
animation.playFromStart();
break;
case RUNNING:
animation.stop();
}
您可以通过以下几种方式完成:
- 通过引入一个临时变量 - 创建一个变量,为其分配状态,并在表达式中使用变量的值而不是多次调用
- 通过编写辅助方法 - 编写一个获取当前状态和状态变量参数列表的方法,并在条件中调用此方法。
这是第一种方法的说明:
Animation.Status status = animation.getStatus();
if (status == Animation.Status.PAUSED || status == Animation.Status.STOPPED) {
animation.playFromStart();
} else if (status == Animation.Status.RUNNING) {
animation.stop();
}
这是第二种方法的说明:
private static boolean checkStatus(Animation.Status status, Animation.Status... expected) {
for (Animation.Status e : expected) {
if (e == status) {
return true;
}
}
return false;
}
...
if (checkStatus(animation.getStatus(), Animation.Status.PAUSED, Animation.Status.STOPPED)) {
...
}
在这种情况下 switch
语句看起来不错
switch (animation.getStatus()) {
case Animation.Status.PAUSED:
case Animation.Status.STOPPED:
animation.playFromStart();
break;
case Animation.Status.RUNNING:
animation.stop();
break;
}
如果您的代码是多线程的,如果您是 运行 动画并且希望能够 stop/start/pause 它们,那么您应该考虑同步。
因此,采用 Dave Newton 的代码片段并使其成为线程安全的:
synchronized(this){
if (animation.shouldReplay()) {
animation.playFromStart();
} else if (animation.shouldStop() {
animation.stop();
}
}
因此,没有危险第一个条件会return假,然后当前线程不再运行,另一个线程修改动画的状态,然后当前线程再次变为可运行并尝试停止动画。