如何在Java中参数化一个或多个if (toastMessage!= null) {
toastMessage.cancel();
}
toastMessage= Toast.makeText(context, "The message you want to display", duration);
toastMessage.show();
值?
我有3个非常相似的enum
:
enum
,
ActiveStateGreenRed
和
ActiveStateGreenOrange
。
如何制作“通用”ActiveStateGreenYellow
(我们称之为enum
)和“继承”ActiveState
:
ActiveStateGreenRed = ActiveState(STATE_COLOR.RED)
ActiveStateGreenOrange = ActiveState(STATE_COLOR.ORANGE)
enum
答案 0 :(得分:1)
enum
s无法继承。你可以做类似的事情:
public enum ActiveState {
INACTIVE_RED(false, STATE_COLOR.RED), //the only difference between enums
ACTIVE_RED(true, STATE_COLOR.RED),
INACTIVE_GREEN(false, STATE_COLOR.GREEN),
ACTIVE_GREEN(true, STATE_COLOR.GREEN);
...
}
或者,采用“经典”的前缀方式:
public final class ActiveState {
public final static ActiveState ACTIVE_RED = new ActiveState( true, STATE_COLOR.RED );
public final static ActiveState ACTIVE_GREEN = new ActiveState( true, STATE_COLOR.GREEN );
...
private final Boolean value;
private final STATE_COLOR color;
private ActiveState(Boolean value, STATE_COLOR color) {
this.value = value;
this.color = color;
}
}
通常,具有(包)私有构造函数的类(其中仅提供预定义的常量实例)可以与enum
非常相似。并且您可以使用这些类继承层次结构。