在写代码的时候,经常会遇到switch语句,根据状态码进行不同的操作,该方法简单,快捷,但维护起来较麻烦,也不易扩展。 例如,如下代码
public class Employee {
private int mType;
static final int salary=500;
static final int commission=500;
static final int bonus=1000;
static final int ENGINEER=0;
static final int SALESMAN=1;
static final int MANAGER=2;
public Employee(int type) {
this.mType=type;
}
int payAmout(){
switch (mType) {
case ENGINEER:
return salary;
case SALESMAN:
return salary+commission;
case MANAGER:
return salary+bonus;
default:
throw new RuntimeException();
}
}
}
该类有Employee类,并且区分了三种状态:ENGINEER、SALESMAN、MANAGER。运行代码如下所示
public class Main {
public static void main(String[] args) {
Employee enginer=new Employee(Emplo