我有
public enum BaseActions implements Actions{
STAND (0,0,0),
TURN (1,1,1);
//other stuff
}
public enum CoolActions implements Actions{
STAND (0,2,3),
TURN(1,6,9);
//other stuff
}
public enum LooserActions implements Actions{
STAND (0,-2,-3),
TURN(1,-6,-9);
//other stuff
}
public interface Actions {
//interface methods
}
class A {
Actions mCurrentAction;
protected void notifyNewAction(final Actions pAction, final Directions pDirection){
//body of the method
}
public void doStuff(final Actions pAction) {
if(pAction.getMyId() > 0)
notifyNewAction(BaseActions.STAND, myDirection);
else
notifyNewAction(BaseActions.TURN, myDirection);
}
}
class B extends A{
public void doMyStuff() {
doStuff(CoolActions.STAND);
}
}
class C extends A{
public void doMyStuff() {
doStuff(LooserActions.STAND);
}
}
我想在从C调用doStuff和从C调用LooserActions时使用CoolActions。 我认为我可以做到的一种方法是使用泛型,然后在B和C中使用
doStuff<CoolActions>(CoolActions.STAND)
并且在A
中public void doStuff<T extends EnumActions&Actions>(final Actions pAction) {
if(pAction.getMyId() > 0)
notifyNewAction(T.STAND, myDirection);
else
notifyNewAction(T.TURN, myDirection);
}
其中EnumActions是一个基本枚举,它只包含枚举元素的声明,仅此而已,类似于枚举的接口,但枚举不能扩展另一个类,因为它们已经扩展了Enum,并且接口不能提供我的意思。 另一种方法是使枚举实现具有
的EnumActions接口public interface EnumActions {
public <T> T getStand();
public <T> T getTurn();
}
并且
class A {
Actions mCurrentAction;
protected void notifyNewAction(final Actions pAction, final Directions pDirection){
//body of the method
}
public <T implements EnumActions> void doStuff(final Actions pAction) {
if(pAction.getMyId() > 0)
notifyNewAction(T.getStand(), myDirection);
else
notifyNewAction(T.getTrun(), myDirection);
}
}
和
public enum CoolActions implements Actions, EnumActions{
STAND (0,2,3),
TURN(1,6,9);
public CoolActions getStand();
public CoolActions getTurn();
//other stuff
}
class B extends A{
public void doMyStuff() {
doStuff<CoolActions>(CoolActions.STAND);
}
}
但是1)我不知道它是否会起作用2)我失去了使用枚举的优点3)这接缝是一种非常糟糕的方式来处理这个4)我将不得不写很多(每个X的枚举字段)不同的枚举)。我从静态最终字段改为枚举,以提高可读性和顺序,这种接缝使事情变得更加困难。
我是以错误的方式设计的吗?我怎么处理这个? 有没有一种解决这个问题的首选方法? 感谢
答案 0 :(得分:2)
似乎枚举没有添加任何东西,也不会做你想要的。也许您应该只使用普通的类层次结构 - 使BaseActions
,CoolActions
和LooserActions
只是在这些类中实现Actions
和STAND和TURN方法的类。
答案 1 :(得分:0)
它很难看,但它可能会做你想要的:
interface Actions {
int getMyId();
}
enum BaseActions implements Actions {
STAND(0, 0, 0), TURN(1, 1, 1);
BaseActions(int x, int y, int z) {}
@Override public int getMyId() {
return 0;
}
}
enum CoolActions implements Actions {
STAND(0, 2, 3), TURN(1, 6, 9);
CoolActions(int x, int y, int z) {}
@Override public int getMyId() {
return 0;
}
}
enum LooserActions implements Actions {
STAND(0, -2, -3), TURN(1, -6, -9);
LooserActions(int x, int y, int z) {}
@Override public int getMyId() {
return 0;
}
}
class Directions {}
class A {
Actions mCurrentAction;
protected void notifyNewAction(final Actions pAction, final Directions pDirection) {
System.out.println(pAction+" "+pAction.getClass());
}
public void doStuff(final Actions pAction) {
Directions myDirection = null;
Enum e=(Enum)pAction;
if(e instanceof CoolActions)
e=CoolActions.valueOf(e.name());
else if(e instanceof LooserActions)
e=LooserActions.valueOf(e.name());
if (pAction.getMyId() > 0) notifyNewAction((Actions)e, myDirection);
else
notifyNewAction((Actions)e, myDirection);
}
}
class B extends A {
public void doMyStuff() {
doStuff(CoolActions.STAND);
}
}
class C extends A {
public void doMyStuff() {
doStuff(LooserActions.STAND);
}
}
public class Main {
public static void main(String[] args) {
A a = new A();
a.doStuff(BaseActions.STAND);
B b = new B();
b.doMyStuff();
C c = new C();
c.doMyStuff();
}
}