在java中实例化泛型类型 [英] Instantiate generic type in java
本文介绍了在java中实例化泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class A< T> {
private T t_;
public A(){
t_ = new T(); //错误
}
}
t想要构造函数看起来像这样:A(Class classT){...
理想情况下,我想要这样的c ++代码。
模板< class T>
class A {
private:
T t_;
public:
A(){}
};
感谢您的帮助。
你不能在Java中做这样的事情。阅读关于删除类型的信息: http://docs.oracle.com/javase/ tutorial / java / generics / erasure.html
I'm stuck. Coming from C++ I thought this would simply work, but it does not. Can you please give me an advice? I will try to not end up with kind of creation method in each class used for T.
public class A<T>{
private T t_;
public A(){
t_ = new T(); //error
}
}
Also i don't want to have constructor looking like: A(Class classT){ ... Ideally i would like to have something like this c++ code.
template<class T>
class A{
private:
T t_;
public:
A(){}
};
Thanks for help.
解决方案
You cannot do such things in Java. Read about type erasure: http://docs.oracle.com/javase/tutorial/java/generics/erasure.html
这篇关于在java中实例化泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文