我只是按照本教程使用WatchService
API。我不知道为什么使用WatchEvent<?>
而不是WatchEvent<Path>
,如果我使用后者,不需要强制转换,或者还有其他情况WatchService
可以用来监视非路径事件?
@SuppressWarnings("unchecked")
static <T> WatchEvent<T> cast(WatchEvent<?> event) {
return (WatchEvent<T>)event;
}
void processEvents() {
for (; ; ) {
...
//why doesn't the poolEvents() return WatchEvent<Path>
for (WatchEvent<?> event: key.pollEvents()) {
WatchEvent.Kind kind = event.kind();
...
//here he use a static method cast() to SuppressWarnings the unchecked warning
WatchEvent<Path> ev = cast(event);
}
}
}