简体   繁体   中英

Java Generic Method - Cannot instantiate Class with generic parameter

Following example is not the original code, I'm not looking for a workaround.

There is a Class For Generic Parsing/UnMarshalling like:

public class UnMarshaller<T extends AClass> {
...

This works fine, until I try to provide a Generic Method to access it.

public class UnMarshall{
...
// the T schema is every time a Subclass of AClass
public <T extends AClass> Queue<T> instantiateSomething(Input i, T schema) {
    UnMarshaller<schema> unmarshaller= new UnMarshaller<schema>(schema, i);
    return unmarshaller.getQueue();
}
...

UnMarshaller< schema > and new UnMarshaller< schema > are troubleing, but i doesn't get it. How can I instantiate this class?

使用泛型类型参数时,请提供类/类型名称,而不是变量名称:

UnMarshaller<T> unmarshaller = new UnMarshaller<T>(schema, i);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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