清理java代码、多个if语句
我应该清理一个java代码,去掉很多东西,但是是否应该清理其他东西,也许以某种方式去掉多个if语句,而不完全重写此代码?似乎无法理解,因为它们是如此不同,以至于可以将它们叠加在一个“如果”中。有什么想法吗清理java代码、多个if语句,java,code-cleanup,Java,Code Cleanup,我应该清理一个java代码,去掉很多东西,但是是否应该清理其他东西,也许以某种方式去掉多个if语句,而不完全重写此代码?似乎无法理解,因为它们是如此不同,以至于可以将它们叠加在一个“如果”中。有什么想法吗 public class Calc { // employee types public static final int SELLER; public static final int COOK; pu
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;
}
}
您编写的代码纯粹是过程性的,在大多数情况下,在使用面向对象语言(如Java)编写代码时被认为是一种不好的做法。您应该了解多态性的威力,不应该手动执行类型检查:
if (type == COOK) { //Avoid doing this in OO languages!
您应该将域实体(员工)视为对象,每个特定类型的员工都可以定义自己的薪酬计算规则
让我们使用单个抽象方法创建一个抽象类EmployeeintCalculatePay(inth)
:
单词abstract表示该方法没有实际实现,但计算工资的所有逻辑将放在子类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;
}
}
请注意这一行:
int sum = (h > 20) ? 20 : 0;
这是三元运算符。有时,它适用于表达式中的条件赋值。因此,当h
大于20时,我们用20初始化sum
变量,否则用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));
}
}
这被称为多病态。如果这个术语对您来说是新术语,您可以阅读OOP基础中的Oracle教程:
Bruce Eckel的《Java中的思考》一书也很好地解释了OOP的基本概念。您编写的代码纯粹是过程性的,在大多数情况下,在使用Java等面向对象语言编写时被认为是一种不好的做法。您应该了解多态性的威力,不应该手动执行类型检查:
if (type == COOK) { //Avoid doing this in OO languages!
您应该将域实体(员工)视为对象,每个特定类型的员工都可以定义自己的薪酬计算规则
让我们使用单个抽象方法创建一个抽象类EmployeeintCalculatePay(inth)
:
单词abstract表示该方法没有实际实现,但计算工资的所有逻辑将放在子类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;
}
}
请注意这一行:
int sum = (h > 20) ? 20 : 0;
这是三元运算符。有时,它适用于表达式中的条件赋值。因此,当h
大于20时,我们用20初始化sum
变量,否则用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));
}
}
这被称为多病态。如果这个术语对您来说是新术语,您可以阅读OOP基础中的Oracle教程:
Bruce Eckel在Java书中的Thinking对OOP的基本概念也有很好的解释。
Java.util.Map
节省大量的if/else,并使用Enum
作为用户的选择
public class X {
public enum Type {
SELLER, COOK, CHIEF
}
private Map<Type, Integer> constantValue1;
private Map<Type, Integer> constantValue2;
private Map<Type, Integer> additionalValue;
public X() {
initialConstantValue1();
initialConstantValue2();
initialAdditionalValue();
}
private void initialConstantValue1() {
constantValue1 = new HashMap<>();
constantValue1.put(Type.SELLER, 20);
constantValue1.put(Type.COOK, 30);
constantValue1.put(Type.CHIEF, 66);
}
private void initialConstantValue2() {
constantValue2 = new HashMap<>();
constantValue2.put(Type.SELLER, 10);
constantValue2.put(Type.COOK, 15);
constantValue2.put(Type.CHIEF, 22);
}
private void initialAdditionalValue() {
additionalValue = new HashMap<>();
additionalValue.put(Type.SELLER, 10);
additionalValue.put(Type.COOK, 20);
additionalValue.put(Type.CHIEF, 30);
}
int pay(final Type type, final int h) {
int sum = 0;
if (h > 8) {
sum = constantValue1.get(type) * (h - 8);
sum += constantValue2.get(type) * 8;
}
else {
sum += constantValue2.get(type) * h;
}
if (h > 20) {
sum += additionalValue.get(type);
}
return sum;
}
}
公共类X{
公共枚举类型{
卖主、厨师、主任
}
私有地图1;
私有地图2;
私有地图附加值;
公共X(){
initialConstantValue1();
initialConstantValue2();
初始附加值();
}
私有void initialConstantValue1(){
constantValue1=新HashMap();
constantValue1.put(类型卖方,20);
康斯坦特价值1.put(库克类型,30);
康斯坦特价值1.put(Type.CHIEF,66);
}
私有void initialConstantValue2(){
constantValue2=新HashMap();
constantValue2.put(卖方类型,10);
康斯坦特价值2.put(库克类型,15);
constantValue2.put(首席类型,22);
}
私有void initialAdditionalValue(){
additionalValue=新HashMap();
附加价值。卖出价(卖方类型,10);
附加值.put(Type.COOK,20);
额外价值。投入(类型。主要,30);
}
整数支付(最终类型,最终整数h){
整数和=0;
如果(h>8){
sum=constantValue1.get(type)*(h-8);
sum+=constantValue2.get(type)*8;
}
否则{
sum+=constantValue2.get(type)*h;
}
如果(h>20){
sum+=additionalValue.get(类型);
}
回报金额;
}
}
java.util.Map
保存大量if/else,并使用Enum
作为用户的选择
public class X {
public enum Type {
SELLER, COOK, CHIEF
}
private Map<Type, Integer> constantValue1;
private Map<Type, Integer> constantValue2;
private Map<Type, Integer> additionalValue;
public X() {
initialConstantValue1();
initialConstantValue2();
initialAdditionalValue();
}
private void initialConstantValue1() {
constantValue1 = new HashMap<>();
constantValue1.put(Type.SELLER, 20);
constantValue1.put(Type.COOK, 30);
constantValue1.put(Type.CHIEF, 66);
}
private void initialConstantValue2() {
constantValue2 = new HashMap<>();
constantValue2.put(Type.SELLER, 10);
constantValue2.put(Type.COOK, 15);
constantValue2.put(Type.CHIEF, 22);
}
private void initialAdditionalValue() {
additionalValue = new HashMap<>();
additionalValue.put(Type.SELLER, 10);
additionalValue.put(Type.COOK, 20);
additionalValue.put(Type.CHIEF, 30);
}
int pay(final Type type, final int h) {
int sum = 0;
if (h > 8) {
sum = constantValue1.get(type) * (h - 8);
sum += constantValue2.get(type) * 8;
}
else {
sum += constantValue2.get(type) * h;
}
if (h > 20) {
sum += additionalValue.get(type);
}
return sum;
}
}
公共类X{
公共枚举类型{
卖主、厨师、主任
}
私有地图1;
私有地图2;
私有地图附加值;
公共X(){
initialConstantValue1();
initialConstantValue2();
初始附加值();
}
私有void initialConstantValue1(){
constantValue1=新HashMap();
constantValue1.put(卖方类型,20);
康斯坦特价值1.put(库克类型,30);
康斯坦特价值1.put(Type.CHIEF,66);
}
私有void initialConstantValue2(){
constantValue2=新HashMap();
constantValue2.put(卖方类型,10);
康斯坦特价值2.put(库克类型,15);
constantValue2.put(首席类型,22);
}
私有void initialAdditionalValue(){
additionalValue=新HashMap();
附加价值。卖出价(卖方类型,10);
附加值.put(Type.COOK,20);
额外价值。投入(类型。主要,30);
}
整数支付(最终类型,最终整数h){
整数和=0;
如果(h>8){
sum=constantValue1.get(type)*(h-8);
sum+=constantValue2.get(type)*8;
}
否则{
sum+=constantValue2.get(type)*h;
}