Java 如何在运行时检查子类是否是类的实例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2410304/
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
How to check if a subclass is an instance of a class at runtime?
提问by iamamused
In an android app test suite I have a class like this where B
is a view:
在一个 android 应用测试套件中,我有一个这样的类,其中B
有一个视图:
public class A extends B {
... etc...
}
now I have a list of view objects which may contain A
objects but in this case I only care if they're subclasses or "instances of" B
. I'd like to do something like:
现在我有一个可能包含A
对象的视图对象列表,但在这种情况下,我只关心它们是子类还是“实例” B
。我想做类似的事情:
ArrayList<View> viewList = getViews();
Iterator<View> iterator = viewList.iterator();
while (iterator.hasNext() && viewList != null) {
View view = iterator.next();
if (view.getClass().isInstance(B.class)) {
// this is an instance of B
}
}
The problem is that when the if
encounters an A
object it doesn't evaluate to an "instance of B
". Is there a way to do isSubclassOf
or something?
问题是当if
遇到一个A
对象时,它不会评估为“实例B
”。有什么办法isSubclassOf
吗?
采纳答案by Hardcoded
You have to read the API carefully for this methods. Sometimes you can get confused very easily.
您必须仔细阅读这些方法的 API。有时,您很容易感到困惑。
It is either:
它是:
if (B.class.isInstance(view))
API says: Determines if the specified Object(the parameter)is assignment-compatible with the object represented by this Class(The class object you are calling the method at)
API说:确定是否指定的Object (参数)是分配兼容由表示的对象此Class (类对象要调用的方法)
or:
或者:
if (B.class.isAssignableFrom(view.getClass()))
API says: Determines if the class or interface represented by this Class objectis either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter
API 说:确定此Class 对象表示的类或接口是否与指定的 Class 参数表示的类或接口相同,或者是其超类或超接口
or (without reflection and the recommended one):
或(没有反思和推荐的):
if (view instanceof B)
回答by Kris
if(view instanceof B)
This will return true if view is an instance of B or the subclass A (or any subclass of B for that matter).
如果视图是 B 或子类 A(或 B 的任何子类)的实例,则返回 true。
回答by Joachim Sauer
Maybe I'm missing something, but wouldn't this suffice:
也许我错过了一些东西,但这还不够:
if (view instanceof B) {
// this view is an instance of B
}
回答by nanda
It's the other way around: B.class.isInstance(view)
反过来说: B.class.isInstance(view)
回答by chama
I've never actually used this, but try view.getClass().getGenericSuperclass()
我从来没有真正使用过这个,但试试 view.getClass().getGenericSuperclass()
回答by Michael Borgwardt
Class.isAssignableFrom()
- works for interfaces as well. If you don't want that, you'll have to call getSuperclass()
and test until you reach Object
.
Class.isAssignableFrom()
- 也适用于接口。如果您不希望那样,则必须调用getSuperclass()
并进行测试,直到到达Object
.
回答by vientoho
If there is polymorphism such as checking SQLRecoverableException vs SQLException, it can be done like that.
如果存在多态性,例如检查 SQLRecoverableException 与 SQLException,则可以这样做。
try {
// sth may throw exception
....
} catch (Exception e) {
if(SQLException.class.isAssignableFrom(e.getCause().getClass()))
{
// do sth
System.out.println("SQLException occurs!");
}
}
Simply say,
简单地说,
ChildClass child= new ChildClass();
if(ParentClass.class.isAssignableFrom(child.getClass()))
{
// do sth
...
}