目前我聲明,像這樣我的所有靜態字段:OOP設計:靜態字段
public static final int EXAMPLE_OF_STATIC_FIELD = 0;
說我有一個代表,可以在類中ocurre所有錯誤字段的基類:
public class Base {
public static final int ERROR_1 = 0;
public static final int ERROR_2 = 1;
public static final int ERROR_3 = 2;
}
如果我擴展這個基類,我想更多的Error types
添加到類,我會做到以下幾點:
public class OffSpring extends Base {
public static final int NEW_ERROR_1 = 3;
}
對於我宣佈新的Error types
,我需要知道基類的Error types
的價值,這在我看來不是很方便,因爲我可以不經意地在Base class
的後代類別中聲明Error Type
值爲Base class
的Error type
。例如:
public static final int NEW_ERROR_1 = 0;
這將是相同的
public static final int ERROR_1 = 0;
這會衝突...
我想,也許使用Enum Class
,然而事實證明,你不能擴展它。 Can enums be subclassed to add new elements?
另一種選擇是使用字符串值類型,而不是int值類型的所有靜態字段,但是這並不是一個非常有效的解決方案......
我如何添加更多領域的一個階級的後代,沒有他們與超級階級衝突?
如果您直接定義常量,總會有錯誤的可能性。怎麼樣使用GUID? – SJuan76