【发布时间】:2016-02-17 09:34:58
【问题描述】:
我想存储在一个枚举类上,以便稍后我可以实例化这个类。但我不知道我该怎么做。
这是一个例子:
public enum STATE {
MENU(Menu.class, Color bgColor);
PLAY(Play.class, Color bgColor);
...
public STATE() {...
}
}
并且有一种在状态之间改变的方法。 STATE 上的所有类都继承自 AState,例如 (public class Menu extends AState{...})
Astate currentState;
public void changeState(STATE s){
if(currentState != null) currentState.dispose();
currentState = ...some code to instantiate the class and assign to this value
currentState.init();
}
我的想法是有一个枚举来保存每个状态的类,以及一些参数来用不同的值实例化这个类,比如他的 bgColor,但我不知道如何在 Java 中做到这一点。
【问题讨论】:
标签: java class inheritance