Java isInstance 与 instanceOf 运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4140065/
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 isInstance vs instanceOf operator
提问by rybit
The whole generics thing is kinda throwing me for a loop, and more so the RTT.
整个泛型的事情有点让我陷入循环,RTT 更是如此。
Specificis? Ah well here's the gist:
具体?嗯,这里是要点:
enum QueryHelper {
query1,
query2;
static <T> QueryHelper getQueryHelper (Class<T> expectedReturn) {
if (expectedReturn.isInstance (SomeRelatedClass.class))
return query1;
else
return query2;
}
}
and then I would call it like so:
然后我会这样称呼它:
...
QueryHelper helper = QueryHelper.getQueryHelper(SomeRelatedClass.class);
...
This is so that I can really flexibly assign the query return type in the actual helper. It does some casting and object creation. What I am seeing is that there is no match, should I be doing this some other way? Or is the whole idea just bad?
这样我就可以在实际的帮助器中真正灵活地分配查询返回类型。它执行一些转换和对象创建。我看到的是没有匹配项,我应该以其他方式这样做吗?还是整个想法很糟糕?
And the real heart of this is that I don't understand the difference between class.isInstance and the instanceOf operator? Should I be using the latter?
真正的核心是我不明白 class.isInstance 和 instanceOf 运算符之间的区别?我应该使用后者吗?
采纳答案by Dónal
This is so that I can really flexibly assign the query return type in the actual helper.
这样我就可以在实际的帮助器中真正灵活地分配查询返回类型。
There is nothing flexible about the return type of this method
此方法的返回类型没有任何灵活性
static <T> QueryHelper getQueryHelper (Class<T> expectedReturn) {
if (expectedReturn.isInstance (SomeRelatedClass.class))
return query1;
else
return query2;
}
It will always return an instance of QueryHelper
. If you want the return type to be flexible you would need to define it as something like:
它将始终返回 的实例QueryHelper
。如果您希望返回类型灵活,则需要将其定义为:
static <T> T getQueryHelper (Class<T> expectedReturn) {
}
Now the return type is flexible, because it will depend on the type of the argument
现在返回类型是灵活的,因为它将取决于参数的类型
And the real heart of this is that I don't understand the difference between class.isInstance and the instanceOf operator?
真正的核心是我不明白 class.isInstance 和 instanceOf 运算符之间的区别?
The difference is that instanceof does a type-check that is fixed at compile-time, for example:
不同之处在于 instanceof 会在编译时进行类型检查,例如:
static boolean isInstance(Object myVar) {
return (myVar instanceof Foo);
}
will always check that myVar is an instance of Foo, whereas
将始终检查 myVar 是否是 Foo 的实例,而
static <T> boolean isInstance(Object myVar, Class<T> expectedType) {
return expectedType.isInstance(myVar);
}
will check that myVar is an instance of expectedType, but expectedType can be a different type each time the method is called
将检查 myVar 是否是 expectedType 的实例,但每次调用该方法时 expectedType 可以是不同的类型
回答by Affe
The expected argument of isInstance is an object that may be an Instance of the class that your class object represents. What you're comparing it to is an instance of the class... java.lang.Class
! So it's not going to match.
isInstance 的预期参数是一个对象,该对象可能是您的类对象所代表的类的实例。你将它与类的一个实例进行比较...... java.lang.Class
!所以它不会匹配。
e.g., would be true:
例如,将是真的:
Class.class.isInstance(SomeRelatedClass.class);
Also would be true (without architectural commentary on the sanity of actually building your query helper this way)
也是如此(没有关于以这种方式实际构建查询助手的理智的架构评论)
expectedReturn.isInstance(new SomeRelatedClass());
回答by vanza
Class.isInstance() doesn't work like your code expects. It tests whether the object you pass to it is an instance of the class. In you code:
Class.isInstance() 不像您的代码所期望的那样工作。它测试您传递给它的对象是否是类的实例。在你的代码中:
expectedReturn.isInstance(SomeRelatedClass.class)
The object you're passing is a Class object. Try this instead, which returns true:
您传递的对象是一个 Class 对象。试试这个,它返回 true:
Class.class.isInstance(SomeRelatedClass.class);
What you're probably looking for is Class.isAssignableFrom(), e.g.:
您可能正在寻找的是Class.isAssignableFrom(),例如:
Object.class.isAssignableFrom(Class.class);
Means you can do this:
意味着你可以这样做:
Class klass = ...;
Object o = klass;