测试对象是否是instanceof参数类型 [英] Test if object is instanceof a parameter type
本文介绍了测试对象是否是instanceof参数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以确定一个对象是否是一个泛型类型的实例?
public< T> test(Object obj){
if(obj instanceof T){
...
}
}
这显然不起作用。有其他选择吗?就像我想用Java反射来实例化一个类,然后检查以确保它是类型通用 T
。
Class 对象:
Class< T>类型; //如果(type.isInstance(obj)){
$ p可能传入方法
// ...
}
$ p>Is there a way to determine if an object is an instance of a generic type?
public <T> test(Object obj) { if (obj instanceof T) { ... } }
That clearly doesn't work. Is there an alternative? Like I want to use Java reflection to instantiate a class and then check to make sure it is of type generic
T
.解决方案The only way you can do this check is if you have the
Class
object representing the type:Class<T> type; //maybe passed into the method if ( type.isInstance(obj) ) { //... }
这篇关于测试对象是否是instanceof参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文