我想将方法保存在 Enum 中,但 Class.getDeclaredMethod 抛出 NoSuchMethodException,那么我该如何处理呢? 我的代码:
public enum Card {
OPENPRISON(false, Cards.class.getDeclaredMethod("", Player.class));
private boolean isInstant;
private Method method;
private Card(boolean isInstant, Method method){
this.method = method;
this.isInstant = isInstant;
}
public boolean isInstant() {
return isInstant;
}
public void run(Player p){
}
}
OPENPRISON 就是问题所在
最佳答案
一个直接的技术问题是您没有在调用 getDeclaredMethod()
时提供方法名称:
OPENPRISON(false, Cards.class.getDeclaredMethod("", Player.class));
一个更大的问题是为什么您需要使用反射。
枚举值是一个常量。使用反射可以做哪些使用静态方法无法轻松完成的事情?或者使用枚举之外的方法?
关于java - 尝试 - 捕获枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39807123/