java - 如何为 Java 类传递参数 T
问题描述
如何为方法 reciveClassTest(Class clazz) 传递参数
- 当 fastxml.jackson.annotation 转换器用于一种类型(例如 MyType)时,无法转换,因为它不是类型
- 杰克逊属性全部为空
下面是一个类的示例,它们很好地代表了 withType 没有类型 Paper 的场景
public class TestPassClassType {
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
TestPassClassType call=new TestPassClassType();
Paper paper=new Paper();
System.out.println(paper);
MyType<Paper> type=new MyType<Paper>();
System.out.println(type);
// How to pass call.reciveClassTest(MyType<Paper>.class)
call.reciveClassTest(MyType.class);
}
public <T> void reciveClassTest(Class<T> clazz) throws InstantiationException, IllegalAccessException {
T t=clazz.newInstance();
MyType<Paper> withType=(MyType<Paper>) t;
//withType not has a type Paper
System.out.println(withType);
}
}
class MyType<T> {
public MyType() {}
T data;
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
class Paper implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
解决方案
推荐阅读
- javascript - Slick 不适用于动态创建的多个 div 元素
- python - 用 csv 文件中的列表中的列表计算字符
- javascript - 通过任意数量的参数设置对象的嵌套键/值
- flutter - Flutter Unicode 撇号在字符串中
- javascript - Typescript检查经典JS类时如何解决“'this'隐含类型'any'”
- node.js - 在 node.js 路由中读取 url 传递的数据
- django - Django 通过 F 批量更新
- java - 当我从 TextView 获取号码时,我的应用程序崩溃
- c# - 加密/令牌认证指南
- sql-server - SSIS 参数化映射