java - 让父类(super class)使用子类使用的枚举

标签 java class inheritance enums

我有

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);
     }
}

我想让A在从B调用doStuff时使用CoolActions,在从C调用doStuff时使用LooserActions。 我认为我可以做到这一点的方法之一是使用泛型,然后在 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 是一个基本枚举,仅包含枚举元素的声明,仅此而已,类似于枚举的接口(interface),但枚举不能扩展另一个类,因为它们已经扩展了枚举,并且接口(interface)不能扩展提供我的意思。 另一种方法是让枚举实现一个 EnumActions 接口(interface),该接口(interface)具有

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) 我必须写很多东西(每个 Y 的 X 枚举字段)不同的枚举)。我从静态最终字段更改为枚举,以提高可读性和顺序,这似乎让事情变得更加困难。

我的设计方式是错误的吗?我该如何处理这个问题? 有解决这个问题的首选方法吗? 谢谢

最佳答案

似乎枚举没有添加任何内容,并且不会执行您想要的操作。也许您应该只使用普通的类层次结构 - 使 BaseActionsCoolActionsLooserActions 只是实现 Actions 的类,并且这些类中的 STAND 和 TURN 方法。

关于java - 让父类(super class)使用子类使用的枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8918934/

相关文章:

javascript - 如何将购物车数组与产品数组链接起来?

java - 如何在Eclipse中查找所选成员的类型?

java - 如何动态地重复调用JSF bean方法?

java - 如何将正则表达式转换为 extglob 表达式?

objective-c - 初始化 'Method *' C 时不兼容的指针类型

c++ - 如何在两个实例中使用值进行计算?

java - 抽象类为 parcelable

java - 如何在将光标放在 HTML 元素上时获取其属性?

java - HashMap 的简单方法似乎是正确的,但它不起作用

c++ - 如何将模板实例传递给需要将模板参数传递给基类的函数?