enum BankOperation { PLUS, MINUS }
public BankOperation getBankOperation(int oldAmount, int newAmount) {
BankOperation operation = newAmount >= oldAmount ? BankOperation.PLUS : BankOperation.MINUS;
int delta = Math.abs(newAmount - oldAmount);
switch (operation) {
case PLUS:
System.out.println(String.format("Adding %s dollars", delta));
break;
case MINUS:
System.out.println(String.format("Withdrawing %s dollars", delta));
break;
}
return operation;
}
注意:该示例故意简单,不能从使用enum
方法中受益。
我有两个BankOperation
PLUS
和MINUS
和一个switch
。我通过BankOperation
得到了getBankOperation(5, 20);
,因此我可以继续进行。
我想知道是否可以在enum
而不是switch
上使用方法,以便我可以通过{p>
BankOperation
其中
BankOperation bankOperation = BankOperation.forOldAndNewAmount(5, 20).startProcessing();
初始化枚举并使forOldAndNewAmount(5, 20)
delta
成为枚举15
的{{1}}字段执行
private int delta
是BankOperation
的抽象startProcessing()
,它根据method
BankOperation
我发现最困难的是-在System.out.println(String.format("Adding/Withdrawing %s dollars", delta));
正文中打印BankOperation
delta
而不将其作为15
的参数-返回enum
和startProcessing()
中的PLUS
MINUS
本身。
答案 0 :(得分:2)
在您的实际示例中,getBankOperation()
是BankOperation
枚举应负责的处理。
您不需要Bank
实例来处理它。
因此,只需将此方法作为枚举的静态方法移动即可:
enum BankOperation { PLUS, MINUS;
public static BankOperation getBankOperation(int oldAmount, int newAmount) {
BankOperation operation = newAmount >= oldAmount ? BankOperation.PLUS : BankOperation.MINUS;
int delta = Math.abs(newAmount - oldAmount);
switch (operation) {
case PLUS:
System.out.println(String.format("Adding %s dollars", delta));
break;
case MINUS:
System.out.println(String.format("Withdrawing %s dollars", delta));
break;
}
return operation;
}
};
现在您可以:
BankOperation bankOperation = BankOperation.getBankOperation(5, 20);
此外,这部分可以通过枚举方法移动:
switch (operation) {
case PLUS:
System.out.println(String.format("Adding %s dollars", delta));
break;
case MINUS:
System.out.println(String.format("Withdrawing %s dollars", delta));
break;
}
它将给出:
enum BankOperation {
PLUS {
public String getOutput(int delta) {
return String.format("Adding %s dollars", delta);
}
},
MINUS {
@Override
public String getOutput(int delta) {
return String.format("Withdrawing %s dollars", delta);
}
};
public abstract String getOutput(int delta);
public static BankOperation getBankOperation(int oldAmount, int newAmount) {
BankOperation operation = newAmount >= oldAmount ? BankOperation.PLUS : BankOperation.MINUS;
int delta = Math.abs(newAmount - oldAmount);
System.out.println(operation.getOutput(delta));
return operation;
}
}
关于您的评论:
有没有可能让我知道如何实现“创造三角洲” 初始化时,枚举
private int delta
的{{1}}字段 用BankOperation
然后我可以在 链而不将其设为参数”?
您不应这样做,因为枚举表示常量值。在处理过程中,枚举值的覆盖值尚不清楚,如果您的应用程序是多线程的或变为多线程的,可能会产生副作用。
作为替代,您可以使用包装forOldAndNewAmount(5, 20)
和增量(startProcessing()
)并返回BankOperation
的类。
BankOperationComputed
最好命名为forOldAndNewAmount()
:
BankOperation getBankOperation()
BankOperationComputed
BankOperationComputed computeBankOperation()
现在您可以做到:
public static BankOperationComputed computeBankOperation(int oldAmount, int newAmount) {
BankOperation operation = newAmount >= oldAmount ? BankOperation.PLUS : BankOperation.MINUS;
int delta = Math.abs(newAmount - oldAmount);
return new BankOperationComputed(operation, delta);
}
答案 1 :(得分:1)
我认为您正在寻找类似的东西:
public class Bank {
enum BankOperation {
PLUS("Adding %s dollars"),
MINUS("Withdrawing %s dollars");
private final String msg;
BankOperation(final String msg) {
this.msg = msg;
}
public String getMsg() {
return msg;
}
}
public BankOperation getBankOperation(int oldAmount, int newAmount) {
BankOperation operation = newAmount >= oldAmount ? BankOperation.PLUS : BankOperation.MINUS;
int delta = abs(newAmount - oldAmount);
System.out.println(String.format(operation.getMsg(), delta));
return operation;
}
}
答案 2 :(得分:1)
枚举的常量是(抽象的)枚举类本身的子类。然后,您可以执行类似的操作
SharedModule
由于public enum BankOperation {
DEPOSIT {
@Override
public void execute(Bank bank, Amount amount) {
// do something
}
},
WITHDRAW {
@Override
public void execute(Bank bank, Amount amount) {
// do something else
}
};
public abstract void execute(Bank bank, Amount amount);
}
// called like
BankOperation.DEPOSIT.execute(bank, amount);
// or
BankOperation operation;
...
operation.execute(bank, amount
类是其每个常量()的(抽象)父级。可以将字段和构造函数添加到enum
:
enum
还添加静态方法会有所帮助:
public enum BankOperation {
DEPOSIT("Deposit") {
@Override
public void execute(Bank bank, Amount amount) {
// do something
}
},
WITHDRAW("Withdraw") {
@Override
public void execute(Bank bank, Amount amount) {
// do something else
}
};
private String name;
private BankOperation(String theName) {
this.name = theName;
}
public abstract void execute(Bank bank, Amount amount);
public String getName() {
return name;
}
}