我正在尝试在Java枚举中编写一个方法:isBigger(Valoration val)
,如果当前评级大于作为参数传递的参数,则返回true,如果估值则抛出异常ValorationNoValued
比较(或两者)是NO_VALUED。
public enum Valoracion {
NO_VALUED(0),
SO_BAD(0),
BAD(2),
OK(4),
GOOD(6),
EXCELENT(8),
WOW(10);
....
public boolean isBigger(Valoration val) throws ValorationNoValued {
if (val == NO_VALUED) {
throw new ValorationNoValued("message");
}
else {
return val.getValor() > this.valor;
}
}
}
我想知道目前的评级是否为NO_VALUED,但我不知道如何。
答案 0 :(得分:2)
您始终可以通过this
引用“当前对象”,无论是enum
还是enum
。但...
看来,您的getValor()
有一个方法public enum Valoracion {
NO_VALUED (0) {
@Override int getValor() {
throw new ValorationNoValued("message");
}
},
SO_BAD (0),
BAD(2),
OK (4),
GOOD (6),
EXCELENT (8),
WOW (10);
....
int getValor() {
return valorValue;
}
public boolean isBigger(Valoration val) throws ValorationNoValued {
return val.getValor()>this.getValor();
}
来返回数值。然后拦截异常状态那里而不是比较方法会更清晰:
a.isBigger(b)
这样,就不需要条件,b.isBigger(a)
和getValor()
之间的对称性是免费的。
如果你更喜欢条件而不是覆盖方法,你仍然可以实现int getValor() {
if(this==NO_VALUED) throw new ValorationNoValued("message");
return valorValue;
}
方法
isBigger
无需在let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self
contactPicker.predicateForEnablingContact = NSPredicate(format: "emailAddresses.@count > 0")
presentViewController(contactPicker, animated: true, completion: nil)
方法中关注双方。
答案 1 :(得分:0)
比较this
:
If (this == NO_VALUED) {