java 如何检查java模板方法中的类类型(instanceof)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5367890/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:46:36  来源:igfitidea点击:

how to check the Class Type(instanceof) in java template method?

javagenericspolymorphism

提问by Koerr

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 :)

感谢帮助 :)

回答by Heisenbug

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:

像 instanceof 这样的结构用于在运行时检查对象的动态类型,所以如果你正在使用它们,你可以简单地避免使用泛型。(您可以使用泛型对象作为函数的参数,然后使用 instanceof 检查其类型)例如:

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
     }
}

回答by Bozho

if mes instanceof Stringshould work.

如果mes instanceof String应该工作。

回答by rsp

Instead of checking INPUT, how about checking the actual parameter?

不是检查INPUT,而是检查实际参数如何?

if(mes instanceof String) { 

回答by Kozio?ek

You will need instance of INPUT:

您将需要 INPUT 的实例:

class A<INPUT>{

    void typeOf(INPUT input){
        if(input.getClass() == "".getClass()){
            System.out.println("Input is String");
        }       
    }
}

all objects extends Object co they have getClassmethod. You could use it to check class.

所有对象都扩展了 Object co 他们有getClass方法。你可以用它来检查类。