我刚刚按照这个教程来使用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);
}
}
}