我知道Java的泛型有点不如.Net.
我有一个泛型类Foo<T>的,我真的需要一个实例T在Foo使用参数的构造函数.如何解决Java的局限?
是否可以获得通用参数的类型?
一个例子:
public final class Voodoo {
public static void chill(List<?> aListWithTypeSpiderMan) {
// Here I'd like to get the Class-Object 'SpiderMan'
Class typeOfTheList = ???;
}
public static void main(String... args) {
chill(new ArrayList<SpiderMan>());
}
}
Run Code Online (Sandbox Code Playgroud) 在GSON中获取您执行的对象列表
Gson gson = new Gson();
Type token = new TypeToken<List<MyType>>(){}.getType();
return gson.fromJson(json, token);
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但我想更进一步,让MyType参数化,这样我就可以有一个通用的函数来解析这个代码的对象列表
// the common function
public <T> List<T> fromJSonList(String json, Class<T> type) {
Gson gson = new Gson();
Type collectionType = new TypeToken<List<T>>(){}.getType();
return gson.fromJson(json, collectionType);
}
// the call
List<MyType> myTypes = parser.fromJSonList(jsonString, MyType.class);
Run Code Online (Sandbox Code Playgroud)
遗憾地返回一个StringMaps数组,而不是类型.T被解释为另一种泛型类型,而不是我的类型.任何解决方法?
我有一个没有默认构造函数的类.我需要一种方法来获得这个类的"空白"实例.'blank'表示在实例化之后,所有类字段都应该具有默认值,如null,0等.
我问,因为我需要能够序列化/绝望化大对象树.并且我无法访问此对象类的源,并且类既没有默认构造函数也没有实现可序列化.尝试序列化这样的结构可能不是一个好主意,但另一种方法是将其转换为更容易序列化的东西.
java reflection serialization constructor default-constructor
如果我有这样的抽象类:
public abstract class Item
{
private Integer value;
public Item()
{
value=new Integer(0);
}
public Item(Integer value)
{
this.value=new Integer();
}
}
Run Code Online (Sandbox Code Playgroud)
有些类派生自Item,如下所示:
public class Pencil extends Item
{
public Pencil()
{
super();
}
public Pencil(Integer value)
{
super(value);
}
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我不能使用泛型调用构造函数:
public class Box <T extends Item>
{
T item;
public Box()
{
item=new T(); // here I get the error
}
}
Run Code Online (Sandbox Code Playgroud)
我知道有可能有一个没有构造函数的类型,但这种情况是不可能的,因为Pencil有没有参数的构造函数,而Item是抽象的.但我从eclipse得到这个错误:
不能实现类型T
我不明白为什么,以及如何避免这种情况?
我有以下方法:
public <T extends Result> T execute(Command<T> command)
{
return new LoginResult();
}
Run Code Online (Sandbox Code Playgroud)
这Result是一个接口,该类LoginResult确实实现了这个接口.但是,我收到错误:
不兼容的类型,必需:T,found:com.foo.LoginResult
然而,如果我将方法签名更改为:
public Result execute(Command<T> command)
Run Code Online (Sandbox Code Playgroud)
然后相同的返回线工作正常,没有任何错误.
这是什么问题?我怎么LoginResult能从这个方法回来?
编辑:我想使用泛型的原因是,我可以做如下的事情:
Command<LoginResult> login = new Command<>();
LoginResult result = execute( login );
Run Code Online (Sandbox Code Playgroud) 我正在编写如下的服务器
public class Server<T extends RequestHandler> {
public void start() {
try{
this.serverSocket = new ServerSocket(this.port, this.backLog);
} catch (IOException e) {
LOGGER.error("Could not listen on port " + this.port, e);
System.exit(-1);
}
while (!stopTheServer) {
socket = null;
try {
socket = serverSocket.accept();
handleNewConnectionRequest(socket);
} catch (IOException e) {
LOGGER.warn("Accept failed at: " + this.port, e);
e.printStackTrace();
}
}
}
protected void handleNewConnectionRequest(Socket socket) {
try {
executorService.submit(new T(socket));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是在 …
我有以下Java类
public class MyClass <T extends MyInterface> implements RowMapper<MyInterface>
Run Code Online (Sandbox Code Playgroud)
我正在实现以下方法:
@Override
public T mapRow(ResultSet rs, int arg1) throws SQLException {
Run Code Online (Sandbox Code Playgroud)
如何在该类上创建"T"类型的实例?
我试图定义T类类并使用"myInstance.newInstance()",但我无法定义它.将Class作为参数发送到方法并使用"newInstance()"方法可以工作,但我希望能够这样做,而不必将其作为参数传递.谢谢!
可能重复:
在Java中创建泛型类型的实例?
我有一些代码:
public class foo<K> {
public void bar() {
K cheese = new K();
// stuff
}
}
Run Code Online (Sandbox Code Playgroud)
这不编译,Intellij的linter告诉我Type parameter 'K' cannot be instantiated directly.
我将如何实例化新副本K.
java ×10
generics ×9
reflection ×2
constructor ×1
gson ×1
instance ×1
interface ×1
raw-types ×1
types ×1