2012-01-18 38 views
0

让超类使用由子类

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

我想提出时doStuff从B和LooserActions称为A使用CoolActions从C 叫的方式之一时使用的枚举我认为我可以做到这一点是使用泛型,然后在B和C使用

doStuff<CoolActions>(CoolActions.STAND) 

,并已在

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元素声明的基本枚举,仅此而已,类似于枚举接口,但枚举不能扩展另一个类,因为它们已经扩展Enum,并且接口不能提供什么我的意思是。 另一种方法是使枚举实现了具有

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)我不知道是否会一EnumActions接口工作2)我失去了使用枚举的优点3)这接近了一个非常糟糕的方式来处理这个4)我将不得不写很多(X枚举字段每Y不同的枚举)。我从静态最终字段改为枚举以提高可读性和顺序,并且这种接合使事情变得更加困难。

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

回答

2

看起来像枚举没有增加什么,也不会做你想做的。也许你应该只使用普通的类层次结构 - 使BaseActionsCoolActionsLooserActions只是实现Actions和STAND和TURN方法的类。

+0

那么,什么是如果他们可以通过类来代替,不能做什么,他们是专为(限的选择有枚举的原因只有一组变量)?我从来没有使用过它们,当我第一次认为它们会有用时,它们不能满足我的需求。而在网上冲浪时,我发现了很多其他的情况,比如我.. – 2012-01-18 23:48:26

+2

@Makers_F:枚举完全按照他们的设计做。但是它们不能被扩展,所以不是多态的。你的设计需要多态性,所以不要使用枚举。 – 2012-01-18 23:56:31

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(); 
    } 
} 
+0

是的,这是丑陋的;)顺便说一句,我不会这种黑客,因为我会有很多行动,然后代码将成为一个巨大的,如果其他,并将难以维护(添加/删除操作等。 )但在不同的情况下,这可能是一个很好的解决方法 – 2012-01-20 14:45:56

+0

看起来像丑陋可以消失:public void doStuff(final Actions pAction){/ *这是否仍然真的做你想要的? */ \t \t Directions myDirection = null; (pAction.getMyId()> 0)notifyNewAction(pAction,myDirection);如果(pAction.getMyId()> 0)notifyNewAction(pAction,myDirection); \t \t else \t \t \t notifyNewAction(pAction,myDirection); \t} – 2012-01-20 22:52:31