3
您將如何使用Java的WatchEvent<T>
類?Java7/WatchEvent - 爲什麼它不是枚舉?
我的意思是:
for (WatchEvent<?> event : key.pollEvents()) {
final WatchEvent.Kind<?> kind = event.kind();
/*
* This key is registered only for ENTRY_CREATE events,
* but an OVERFLOW event can occur regardless if events are
* lost or discarded.
*/
if (kind == OVERFLOW) {
continue;
}
// Cast is safe because we registered a path instance.
@SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>)event;
final Kind<Path> type = ev.kind();
if (type == ENTRY_CREATE) {
// Do something.
} else if (type == ENTRY_DELETE) {
// Do something.
} else if (type == ENTRY_MODIFY) {
// Do something.
}
}
似乎是有點醜(當然,二else if
語句可能還行,但是......)。
爲什麼他們不使用枚舉來表示事件類型?
我不知道,但我確定開發者有他們的理由。投票結束這個問題.. – mre
你可以打開枚舉,我沒有看到它的壞事。 –