Set是一个接口,所以不,你不能直接实例化它。但是,如果您不能拥有一个接口的实例,那么接口将毫无用处!返回的实例是接口tree.keySet()的一些具体实现Set。
让我们变得超级具体,TreeMap#keySet()看看源代码:
public Set<K> keySet() {
return navigableKeySet();
}
好吧,这并不能告诉我们太多。我们需要深入研究:
public NavigableSet<K> navigableKeySet() {
KeySet<K> nks = navigableKeySet;
return (nks != null) ? nks : (navigableKeySet = new KeySet(this));
}
所以返回的具体类型是KeySet!这是你的Set接口实现。http://www.docjar.com/html/api/java/util/TreeMap.java.html#1021
这解释了这一点:
System.out.println(set instanceof Set); // prints true
System.out.println(set instanceof HashSet); // prints false
Set是一个接口;HashSet是该接口的实现。foo instanceof Set将适用于任何实现true的每个实例。我们已经确定了返回的对象的具体类型是 a ,而不是 a ,所以这就解释了为什么是- 因为是 a ,所以它不可能是 a !foo SetTreeMap#keySet()KeySetHashSetset instanceof HashSetfalsesetKeySetHashSet
如果这对您仍然没有意义,请继续阅读instanceof:
运算符将instanceof对象与指定类型进行比较。您可以使用它来测试对象是类的实例、子类的实例还是实现特定接口的类的实例。