2012-01-18 39 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