简体   繁体   English

获取泛型类型的 class 的类型?

[英]Get the the type of the class of a generic type?

A class which has a method declared as this:一个 class 有一个声明如下的方法:

public class A{
    public <T> T test(java.lang.Class<T> classOfT)
}

Normally it can be called against an object like this:通常可以像这样针对 object 调用它:

A a = new A()
String s = "test";
a.test(String.class); //I wrote this as s.class at first,which is wrong.Thanks Nick.

Now I would like to generalize the object passed to it, so I declare a class like this:现在我想概括传递给它的 object,所以我声明一个 class 如下:

public class B <T>{
    private A a   = new A();
    private T obj = null;
    T test(){return a.test(obj.getClass()); } 
}

But the code wont' compile.I am wondering is it possible to achieve my goal?但是代码不会'编译。我想知道是否有可能实现我的目标?

Thank you in advance Java gurus;)提前感谢您 Java 大师;)

T obj = ...;
obj.getClass()

The last line returns Class<? extends T>最后一行返回Class<? extends T> Class<? extends T> -- but T is unbounded, so it is basically bounded by Object , which means it returns Class<? extends Object> Class<? extends T> -- 但是T是无界的,所以它基本上以Object为界,这意味着它返回Class<? extends Object> Class<? extends Object> , which is the same as Class<?> . Class<? extends Object> ,与Class<?>相同。

So doing this:所以这样做:

T test () { return a.test(obj.getClass()); }

Will actually invoke a.test with a parameter of type Class<?> , which returns an Object , and not a T .实际上会使用Class<?>类型的参数调用a.test ,它返回Object ,而不是T

Just cast the parameter to Class<T> or the return type to T and it should work -- although I am yet to understand why you need something like this.只需将参数转换为Class<T>或将返回类型转换为T ,它应该可以工作——尽管我还不明白你为什么需要这样的东西。 Also, there is this strange error in the original post:此外,原始帖子中存在这个奇怪的错误:

 String s = "test";
 a.test(s.class);

Doing "test".class is wrong -- it should be String.class ."test".class是错误的——它应该是String.class

Class<T> clazz  = (Class<T>) obj.getClass();
return a.test(clazz);

Try尝试

@SuppressWarnings("unchecked")
T test() {
    return (T) a.test(obj.getClass());
}

In complement of the other answers, when defining your function as作为其他答案的补充,将您的 function 定义为

public class A{
    public <T> T test(java.lang.Class<T> classOfT) { ... }
}

if you are using the classOfT parameter just to determine the type of T , you might be better off defining your method with no parameter at all.如果您使用classOfT参数只是为了确定T的类型,那么您最好在完全没有参数的情况下定义您的方法。 For instance, do this instead of the above declaration:例如,这样做而不是上面的声明:

public class A{
    public <T> T test() { ... }
}

That way you can just call it using this qualified method-call syntax.这样你就可以使用这种合格的方法调用语法来调用它。 This is how I would rewrite your code:这就是我将如何重写您的代码:

public class A{
    public <T> T test() {
          // ...
    }        

    public static void main(String[] args) {
          A a = new A();
          String result = a.<String>test(); 
    }
}

public class B <T> {
    private A a = new A();

    T test() {
        return this.a.<T>test();
    }
}

This way you do not need to pass the generic A#test() function a class parameter, which would possibly have been unused at runtime.这样,您不需要传递通用A#test() function 参数 class 参数,该参数可能在运行时未使用。 Of course if you do use the classOfT at runtime, this solution may not be that much interesting for you...当然,如果你确实在运行时使用了classOfT ,那么这个解决方案对你来说可能不是那么有趣......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM