更新时间:2023-09-27 GMT+08:00
分享

反转布尔值

通过此重构,您可以反转布尔变量的值或方法的返回值。

执行重构

  1. 在代码编辑器中,将光标放置在布尔变量或具有布尔返回值的方法的声明上。
  2. 在主菜单或编辑器上下文菜单中,选择Refactor>Invert Boolean
  3. 在打开的Invert Boolean对话框中,为反转变量或方法提供新名称。

  4. 单击Refactor以应用重构。

示例

例如,让我们反转condition变量和checkCondition方法的值。

重构前

class Invert {
    private static double a;
    public static void main(String[] args) {
        boolean condition = true;
        if (condition) {
            System.out.println("Hello World!");
        }
    }
    public static boolean checkCondition() {
        if (a > 15 && a < 100) {
            a = 5;
            return true;
        }
        return false;
   }
}

重构后

class Invert {
    private static double a;
    public static void main(String[] args) {
        boolean condition = false;
        if (!condition) {
            System.out.println("Hello World!");
        }
    }
    public static boolean checkCondition() {
        if (a > 15 && a < 100) {
            a = 5;
            return false;
        }
        return true;
   }
}
分享:

    相关文档

    相关产品