我用的是類java.util.concurrent.TimeUnit
,我發現有關我不知道一些有趣的約定,我不明白的是關於枚舉我有做我自己我一個例子希望有人能向我解釋..java.util.concurrent.TimeUnit中的類設計模式
一個簡單的接口與方法。
interface MyInterface{public String concate(String... a);}
後者我創建了一個enumeration
public enum Enumerations implements MyInterface
{
STRINGUTILITIES{},
BEANENUMERATION{},
NANOSECONDS
{//sample methods
public long toNanos(long d) { return d; }
public long toMicros(long d) { return d; }
public long toMillis(long d) { return d; }
public long toSeconds(long d) { return d; }
public long toMinutes(long d) { return d; }
public long toHours(long d) { return d; }
public long toDays(long d) { return d;}
public long convert(long d) { return d;}
int excessNanos(long d, long m){return 13;}
};
@Override public String concate(String... a){throw new AbstractMethodError();}
}
我需要實現concate
方法對我來說是
我的第一個問題是OK。
STRINGUTILITIES{},
BEANENUMERATION{},
NANOSECONDS{}
是枚舉嗎?我想其實是嵌套枚舉..
我主要關注的是,如果一個comment
在外枚舉Enumerations
的
STRINGUTILITIES{},
BEANENUMERATION{},
NANOSECONDS{}
抱怨他們沒有實現的方法。該concate方法看來3內部枚舉在某種程度上擴展了外部枚舉,並且如果外部枚舉正在實現該方法,那麼它們就可以使用。
這樣
public enum Enumerations implements MyInterface
{
STRINGUTILITIES{},
BeanEnumeration{},
NANOSECONDS
{
public long toNanos(long d) { return d; }
public long toMicros(long d) { return d; }
public long toMillis(long d) { return d; }
public long toSeconds(long d) { return d; }
public long toMinutes(long d) { return d; }
public long toHours(long d) { return d; }
public long toDays(long d) { return d;}
public long convert(long d) { return d;}
int excessNanos(long d, long m){return 13;}
};
}
如果外枚舉不實現所述方法和我實現在每個嵌套枚舉的方法是OK
。
其他問題,如果comment
這樣
interface MyInterface{public String concate(String... a);}
public enum Enumerations implements MyInterface
{
@Override public String concate(String... a){return "java";}
}
嵌套枚舉顯示錯誤..
請人指導過解釋這種方法是如何使用的,並命名爲..
非常感謝..
這看起來*很奇怪。你爲什麼會有枚舉枚舉*?任何人都可以用'Enumerations'類型的值做什麼?這並沒有幫助你沒有提供任何'concate'方法意味着什麼的指示。 –
你有沒有真正閱讀[enum文檔](http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html)? –
concate方法只是一種不關心的手段,只是我有興趣瞭解的設計.....這段代碼是基於Java API的TimeUtil.class,這只是一個更易讀的例子。 – chiperortiz