问题描述
public class Test {
private static Object createInstance(String classPath){
try {
Class<?> tClass = Class.forName(classPath);
if(tClass!= null){
return tClass.newInstance();
}
} catch(Exception e){
e.printStackTrace();
}
返回null;
}
@SuppressWarnings(unchecked)
public final static< INPUT,OUTPUT>过滤< INPUT,OUTPUT> getFilter(String path){
return(Filter< INPUT,OUTPUT>)createInstance(path);
}
public final static< INPUT,OUTPUT> OUTPUT过滤器(字符串路径,INPUT消息){
过滤< INPUT,OUTPUT> filter = getFilter(path);
//如何检查INPUT和OUTPUT类型?
// if(INPUT instanceof String){...}不起作用
return filter.filter(mes);
code
$ b $ p
$ b 参考我以前的问题
感谢您的帮助:)
解决方案其他答案肯定是正确的。无论如何,我认为你做的事很不寻常。
我会试着解释一下:
泛型用于静态多态。泛型类型的实例在编译时确定。
类似于instanceof的构造用于在运行时检查对象的动态类型,所以如果您使用它们,则可以避免使用泛型。
(您可以使用一个通用的Object作为函数的参数,然后使用instanceof来检查它的类型)
例如:
public void method(Object o){
if(o instanceof String){} //做你想做的事
else ....
}
通常,如果您使用泛型,您只是试图避免该构造。通常情况下,泛型类型可以实现一个众所周知的接口,通过这种方式,可以对执行该接口的不同类型的对象执行任何对该对象执行的操作,以执行该过滤器方法,而不必知道涉及的特定类型的对象。
我不知道你需要什么样的多态功能,无论如何你也可以尝试类似的东西:
public interface polymorphicObj {
public method();
public class Filter {GENERIC implements polymorphicObj> {
public filter(GENERIC obj){
obj.method(); //你不需要知道具体的类型是多态的对象
}
}
public class Test {
private static Object createInstance(String classPath) {
try {
Class<?> tClass = Class.forName(classPath);
if (tClass != null) {
return tClass.newInstance();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("unchecked")
public final static <INPUT, OUTPUT> Filter<INPUT, OUTPUT> getFilter(String path) {
return (Filter<INPUT, OUTPUT>) createInstance(path);
}
public final static <INPUT, OUTPUT> OUTPUT filter(String path, INPUT mes) {
Filter<INPUT, OUTPUT> filter = getFilter(path);
//How to check the INPUT and OUTPUT type here?
//if(INPUT instanceof String){ ... } not work
return filter.filter(mes);
}
}
refer to my earlier question here
thanks for help :)
解决方案 Other answer are certainly correct. Anyway i think you are doing something quite unusual.I'll try to explain:
Generics are use for static polymorphism. An instance of a generic type is determined at compile time.
Constructs like instanceof are used to check dynamic type of an object at runtime, so if you are using them, you can simply avoid the use of generics.(You can use a generic Object as parameter for your function and then use instanceof to check its type)For example:
public void method(Object o){
if (o instanceof String){} //do what you want
else ....
}
Typically, if you use generics, you are just trying to avoid that constructs. Typically a generic type can implements a well know interface, in a way that any operation performed upon that object into your filter method, could be performed for different types of objects implementing that interface and without knowing the specific type of objects involved.
I don't know exactly what are the polymorphic feature that you need, anyway you could try also something like that:
public interface polymorphicObj{
public method();
}
public class Filter<GENERIC implements polymorphicObj>{
public filter(GENERIC obj){
obj.method(); //you don't need to know of which specific type is polymorphicObj
}
}
这篇关于如何检查java模板方法中的类类型(instanceof)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!