【发布时间】:2021-10-29 16:21:12
【问题描述】:
假设我有以下界面:
public interface NUMS {
public static final int ZERO = 0;
public static final int ONE = 1;
public static final int TWO = 2;
}
然后我有一个函数返回 0-2 之间的 int。 有没有办法获取接口中数字所代表的成员名?
例如:
函数返回 1,我想将其映射回 ONE,而不使用 Map/Array。
谢谢!
【问题讨论】:
-
为什么不使用枚举呢?
-
不,除了使用反射或手动映射所有选项之外没有可能。
-
为什么不使用实际的枚举?