假设我有一个 Enum 定义如下:
public enum Sample{
// suppose AClass.getValue() returns an int
A(AClass.getValue()),
B(AClass.getValue()),
C(AClass.getValue());
private int _value;
private Sample(int _val){
this._value = _val;
}
public int getVal(){
return _value;
}
我可以使用Sample.A
或Sample.A.getAVal()
不使用问题来提取值。
现在假设它AClass.getValue()
可以接受一个参数来返回一个可能不同的特定值,例如AClass.getValue(42)
.
可以将参数传递给公共 Enum 方法并检索 Enum 值吗?换句话说,我可以有一个像这样的枚举定义吗
public enum Sample{
// suppose AClass.getValue() returns an int
A(AClass.getAValue()),
B(AClass.getBValue()),
C(AClass.getCValue());
private int _value;
private Sample(int _val){
this._value = _val;
}
public int getVal(){
return _value;
}
public int getVal(int a){
// somehow pull out AClass.getAValue(a)
}
使用Sample.A.getValue(42)
?