Java 是否有“是一种类”的测试方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/928531/
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
Does Java have an "is kind of class" test method
提问by Heat Miser
I have a baseclass, Statement
, which several other classes inherit from, named IfStatement
, WhereStatement
, etc... What is the best way to perform a test in an if
statement to determine which sort of Statement
class an instance is derived from?
我有一个基类,Statement
其他几个类继承自,命名IfStatement
,WhereStatement
等等......在if
语句中执行测试以确定Statement
实例派生自哪种类的最佳方法是什么?
采纳答案by benjismith
if (obj.getClass().isInstance(Statement.class)) {
doStuffWithStatements((Statement) obj));
}
The nice thing about this technique (as opposed to the "instanceof" keyword) is that you can pass the test-class around as an object. But, yeah, other than that, it's identical to "instanceof".
这种技术的好处(与“instanceof”关键字相反)是您可以将测试类作为对象传递。但是,是的,除此之外,它与“instanceof”相同。
NOTE: I've deliberately avoided editorializing about whether or not type-instance-checking is the rightthing to do. Yeah, in most cases, it's better to use polymorphism. But that's not what the OP asked, and I'm just answering his question.
注意:我故意避免对类型实例检查是否正确进行社论。是的,在大多数情况下,最好使用多态。但这不是 OP 所问的,我只是在回答他的问题。
回答by Dave Ray
if(object instanceof WhereStatement) {
WhereStatement where = (WhereStatement) object;
doSomething(where);
}
Note that code like this usually means that your base class is missing a polymorphic method. i.e. doSomething()
should be a method of Statement
, possibly abstract, that is overridden by sub-classes.
请注意,像这样的代码通常意味着您的基类缺少多态方法。iedoSomething()
应该是 的方法Statement
,可能是抽象的,被子类覆盖。
回答by Igor Krivokon
The answer to your question is instanceof.
你的问题的答案是 instanceof。
However, keep in mind that if your code needs instanceof, it's a sign that something is not right with your design. There are some cases when instanceof is justified, but they are rather exceptions. Usually, if your subclasses need to behave differently, you have to use polymorphism instead of if()s.
但是,请记住,如果您的代码需要 instanceof,则表明您的设计存在问题。在某些情况下 instanceof 是合理的,但它们是相当例外的。通常,如果您的子类需要不同的行为,您必须使用多态而不是 if()s。
回答by paxdiablo
This is notthe way to do things in an object-oriented way, it's a throwback to the old code/data dichotomy. Now that's not necessarily a bad thing (if you know what you're doing) but it should be left to the non-object-oriented languages like C.
这不是以面向对象的方式做事的方式,它是对旧代码/数据二分法的回归。现在这不一定是件坏事(如果您知道自己在做什么),但应该留给像 C 这样的非面向对象语言。
With proper design, you don't need that sort of behavior. Instead of the construct:
通过适当的设计,您不需要那种行为。而不是构造:
if (obj.getClass().isInstance(Statement.class)) {
doStuffWithStatements((Statement) obj));
}
(apologies to benjismith for 'stealing' his code), you should really be making the object itself responsible for its own activities thus:
(向本吉史密斯“窃取”他的代码表示歉意),您确实应该让对象本身对自己的活动负责,因此:
obj.doStuff();
Then each different obj
class will have its own definition for doStuff
. That is the right way to do it.
然后每个不同的obj
类都会有自己的定义doStuff
。这是正确的做法。
回答by Akshay
isAssignableFrom(java.lang.Class) from class is your answer. http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#isAssignableFrom(java.lang.Class)
isAssignableFrom(java.lang.Class) from class 是你的答案。 http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#isAssignableFrom(java.lang.Class)
回答by tayfun ?ztemel
Try this:
尝试这个:
if (Statement.class.isInstance(obj)) {
doStuffWithStatements((Statement) obj));
}
since Class.isInstance()method takes an object instance as a parameter.
因为Class.isInstance()方法将对象实例作为参数。