Java instanceof 实现/扩展
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13762566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java instanceof implements/extends
提问by Jaanus
How does instanceof work exactly? When I have a hierarchy of objects that extend and implement eachother, does being instance of something work through these both lines?
instanceof 究竟是如何工作的?当我有一个相互扩展和实现的对象层次结构时,作为某物的实例是否可以通过这两行工作?
For example I want to know if my object is instance of List
or ArrayList
or Collection
?
例如,我想知道如果我的对象是实例List
或ArrayList
或Collection
?
I examined this tree, http://docs.oracle.com/javase/6/docs/api/java/util/package-tree.html
我检查了这棵树,http://docs.oracle.com/javase/6/docs/api/java/util/package-tree.html
And they seem to fall all under Object
ofcourse, but what I need, I think is AbstractCollection
or even normal Collection
, because that seems to be the highest in hierarchy.
他们似乎都属于当然Object
,但我需要的是,我认为是AbstractCollection
或者甚至是正常的Collection
,因为这似乎是层次结构中最高的。
Will I be fine, when I check an object against only Collection
to cover all those 3 classes?
当我检查一个对象只Collection
覆盖所有这 3 个类时,我会好吗?
回答by Thilo
Will I be fine, when I check an object against only Collection to cover all those 3 classes?
当我仅针对 Collection 检查对象以涵盖所有这 3 个类时,我会没事吗?
Yes, instanceof Collection
will return true for all implementation (direct or indirect) of the Collection interface.
是的,instanceof Collection
将为 Collection 接口的所有实现(直接或间接)返回 true。
In the rare case that you do not want this, you'd have to use reflection. For example, Class#getDeclaredClasseswill give you a list of all classes and interfaces that are directly extended/implemented by the class.
在极少数情况下您不希望这样做,您必须使用反射。例如,Class#getDeclaredClasses将为您提供一个由该类直接扩展/实现的所有类和接口的列表。
Once you know that something is a Collection, you can cast it to get access to its methods (like iterator
):
一旦你知道某个东西是一个集合,你就可以转换它来访问它的方法(比如iterator
):
if (myObject instanceof Collection){
Collection<?> c = (Collection<?>) myObject;
for (Object o: c){
// do something with every element in the collection
}
}
回答by cowls
if(obj instanceof Collection)
Will return true if the object is any kind of collection. So it will return true for your 3 cases, however it will also return true for other cases such as HashMap
如果对象是任何类型的集合,则返回 true。所以它会为你的 3 种情况返回 true,但是它也会为其他情况返回 true,例如 HashMap
回答by Averroes
instanceof
operator will return true
if the inspected object is a class or subclass of a given class or if it (or one of its ancestors) implements a given interface.
instanceof
true
如果被检查的对象是给定类的类或子类,或者它(或其祖先之一)实现了给定的接口,则运算符将返回。
回答by Azodious
Will I be fine, when I check an object against only Collection to cover all those 3 classes?
当我仅针对 Collection 检查对象以涵盖所有这 3 个类时,我会没事吗?
Not always.
不总是。
It is possible that an object is-a
a Collection
but ArrayList
.
对象is-a
a Collection
but是可能的ArrayList
。
For example:
例如:
ArrayList anArrList = new ArrayList();
HashMap ahashMap = new HashMap();
anArrList instanceof Collection // true
ahashMap instanceof Collection // true
but both are in different hierarchy.
但两者处于不同的层次结构。