如何用Java编写简短的逻辑表达式?

如何用Java编写简短的逻辑表达式?,java,logical-operators,Java,Logical Operators,有没有办法缩短对同一变量的连续逻辑操作 例如: 在if子句中,我检查了animation.getStatus()两次,一次是暂停,一次是停止。 是否有一种方法可以使其类似于animation.getStatus()==animation.Status.PAUSED | | animation.Status.STOPPED 我确信这个问题已经被问过了,但我真的不知道该搜索什么,所以如果这是重复的,我很抱歉。否;Java语法是不可变的 有几种选择,但最简单的是重构,作为奖励,重构使其清晰易读,例如

有没有办法缩短对同一变量的连续逻辑操作

例如:

在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)) {
        ...
    }
    
    下面是第二种方法的示例:

    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;
    }
    

    如果你的代码是多线程的,如果运行动画并希望能够停止/启动/暂停,那么代码应该是多线程的,那么你应该考虑同步。

    因此,要获取Dave Newton代码片段并使其线程安全:

    synchronized(this){
        if (animation.shouldReplay()) {
            animation.playFromStart();
        } else if (animation.shouldStop() {
           animation.stop();
        }
    }
    

    因此,不存在第一个条件返回false的危险,然后当前线程不再运行,另一个线程修改动画的状态,然后当前线程再次变为可运行并尝试停止动画。

    不是真的,不可以。如果调用特别昂贵,最好将其重构为一个变量。如果Animation.Status为enum,则这可能是有用的:EnumSet notRunning=EnumSet.of(Animation.Status.PAUSED,Animation.Status.STOPPED);然后执行if(notRunning.contains(animation.getStatus())使其更好,您的枚举(animation.Status)可以使用静态方法返回该枚举集