当前位置: 动力学知识库 > 问答 > 编程问答 >

java - return a Vector of an generic class type

问题描述:

I am having a Java function, that I want to return a Vector of a generic class type:

private static <T extends IReader> Vector<T> Read()

{

Vector<T> List = new Vector<T>(); // Create new Vector

// Create new instance of the type T

T Dataset = new T();

Dataset.myFunction();

// Add to the Vector

List.add(Dataset);

// return Vector

return List;

}

The mentioned IReader is a very simple abstract class:

public abstract class IReader

{

abstract public void myFunction();

}

The error I am getting while compiling is that T is a type parameter and that a class is required instead:

error: unexpected type

T Dataset = new T();

^

required: class

found: type parameter T

where T is a type-variable:

T extends IReader declared in method <T>Read()

1 error

Now I don't know what I did wrong. I can summarize my thoughts: T needs to be derived from IReader, otherwise Java wouldn't be able to find the method "myFunction", which I am calling. Also I need to work with generics here, because the method Read should be able to return a Vector of different kind of classes.

Concerning the compiler error message: When I replace

<T>

by

Class<T>

it doesn't work either. So I think I am stuck here.

My question: How can I make the mentioned function "Read" to return a Vector of a generic type, that is derived from IReader?

Thanks in advance!

网友答案:

Consider a class which implements IReader but has a private no-arg default constructor.Then obviously you cant create Objects using a default constructor.Consider this :-

class Example extends IReader {

private Example{

}
@Override
 public void myFunction(){
//your code goes here 
}
}

Obviously you cant create instances of class Example outside the class ,in this case

You can do two things:-

1)Use reflection to create objects

private static <T extends IReader> Vector<T> Read(Class<T> cl)
{
    Vector<T> List = new Vector<T>(); // Create new Vector

    // Create new instance of the type T
    T Dataset = cl.newInstance();
    Dataset.myFunction();

    // Add to the Vector
    List.add(Dataset);

    // return Vector
    return List;
}

2)You can pass an Object of type T and simpy add to the list

private static <T extends IReader> Vector<T> Read(T obj)
{
    Vector<T> List = new Vector<T>(); // Create new Vector

    // Create new instance of the type T

    obj.myFunction();

    // Add to the Vector
    List.add(obj);

    // return Vector
    return List;
}
网友答案:

Unfortunately Java Generics (unlike C++ templates) don't allow you to use constructions like new T(). This happens because Generics are considered as syntax sugar, inside JVM these entities are simply references of type Object (unlike Javac a C++ compiler derives a separate instance of code for each instantiation of a template parameter.

Thus you may move creation of an object of a particular type out of the function and pass a reference to a pre-created object or instantiate an object using reflection and explicitly specified object class as described on java generic constructors

分享给朋友:
您可能感兴趣的文章:
随机阅读: