清理java代码,多个if语句

Tri*_*u86 0 java code-cleanup

我应该清理一个java代码,摆脱了很多东西,但应该还有什么要清理,也许以某种方式摆脱多个if语句,而不完全重写这段代码?似乎无法弄清楚它们是如此不同,以便将它们堆叠在一个'if'中.有任何想法吗?

 public class Calc {
           // employee types
           public static final int SELLER;
           public static final int COOK;
           public static final int CHIEF;



       public static void main(final String[] args) {
              Calc c = new Calc();
              System.err.println(c.pay(CHIEF) + " should be 66");
       }

       private int pay(final int type, final int h) {
              int Sum = 0;
              if (type == SELLER) {
                     if (h > 8) { 
                           Sum = 20 * (h - 8);  
                           Sum += 80;
                     } else {
                           Sum += 10 * h;
                     }
              }
              if (type == COOK) {
                     if (h > 8) { 
                           Sum = 30 * (h - 8); 
                           Sum += 15 * 8;
                     } else {
                           Sum += 15 * h;
                     }
              }
              if (type == CHIEF) {
                     if (h > 8) { 
                           Sum = 66 * (h - 8); 
                           Sum += 22 * 8;
                     } else {
                           Sum += 22 * h;
                     }
              }
              if (h > 20) {
                     if (type == SELLER) {
                           Sum += 10;
                     }
                     if (type == COOK) {
                           Sum += 20;
                     }
                     if (type == CHIEF) {
                           Sum += 30;
                     }
              }
              return Sum;
       }
}
Run Code Online (Sandbox Code Playgroud)

And*_*huk 6

您编写的代码纯粹是程序性的,在大多数情况下,在使用Java等面向对象语言编写时,这被认为是一种不好的做法.您应该了解多态性的强大功能,不应手动执行类型检查:

if (type == COOK) { //Avoid doing this in OO languages!
Run Code Online (Sandbox Code Playgroud)

您应该将您的域实体(员工)视为对象,并且每种特定类型的员工都可以定义自己的计算薪酬的规则.

让我们用一个抽象方法创建一个抽象类Employee int calculatePay(int h):

public abstract class Employee {
    abstract int calculatePay(int h);
}
Run Code Online (Sandbox Code Playgroud)

Word 摘要表示此方法没有实际实现,但所有计算薪酬的逻辑都将放在子类Seller,Cook和Chief中:

public class Cook extends Employee {

    public Cook() {}

    int calculatePay(int h) {
        int sum = (h > 20) ? 20 : 0;
        if (h > 8) {
            sum = 30 * (h - 8);
            sum += 15 * 8;
        } else {
            sum += 15 * h;
        }
        return sum;
    }
}
Run Code Online (Sandbox Code Playgroud)

注意这一行:

    int sum = (h > 20) ? 20 : 0;
Run Code Online (Sandbox Code Playgroud)

这是三元运算符.有时它对表达式中的条件赋值很有用.因此sum,当h大于20时,我们用20 初始化变量,否则为0.现在我们if在方法的最后不使用额外的语句.

现在每个员工都负责计算自己的薪酬,而不需要在PayCalculator类中执行类型检查 - 它在运行时动态解析哪些代码基于参数类型执行:

public class PayCalculator {

    int pay(Employee e, int hours) {
        return e.calculatePay(hours);
    }

    public static void main(String[] args) {
        Seller seller = new Seller();
        Cook cook = new Cook();
        Chief chief = new Chief();

        PayCalculator calc = new PayCalculator();

        System.out.println("Seller is payed " + calc.pay(seller, 15));
        System.out.println("Cook is payed " + calc.pay(cook, 10));
        System.out.println("Chief is payed " + calc.pay(chief, 22));
    }
}
Run Code Online (Sandbox Code Playgroud)

这称为多态性.如果这个术语对您来说是新的,您可以阅读OOP基础知识中的Oracle教程:https://docs.oracle.com/javase/tutorial/java/concepts/index.html

Bruce Eckel 在Java书中思考也对基本的OOP概念有了很好的解释.