当前位置:网站首页>理解泛型之得到泛型类型
理解泛型之得到泛型类型
2022-08-09 14:55:00 【按劳分配】
在网络框架中,通过得到泛型的类型,使用Gson把返回结果转为对象
- **误区:首先想到的是如果得到类中的泛型类型,然后就去百度,会得到如下代码
Class cls = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).
getActualTypeArguments()[0];//获取泛型class
大概的代码意识,获取直接父类,得到实际类型参数数组 得到第一个类型参数
首先我的疑惑是为什么要通过父类来获取泛型的类型,既然我自己不是有个class Student<T> 泛型T,
直接通过new Student<User> 这样new一个对象,不就吧User类作为泛型参数传递进去了吗?
那为什么在Student类中拿不到泛型,看下面一段话
public Type getGenericSuperclass()
用来返回表示当前Class 所表示的实体(类、接口、基本类型或 void)的直接超类的Type。
如果这个直接超类是参数化类型的,则返回的Type对象必须明确反映在源代码中声明时使用的类型(抄袭的)
就是说在源码声明中,可以限定T 是什么。
那么先来个实例- 于是自己写代码就是这样
public class User {
public int age;
}
public class HttpCallBackBean<T> {
private int code;
private T data;
public int getCode() {
return code;
}
public T getData() {
return data;
}
}
public abstract class HttpBack<T> {
protected Type genericityType;
public HttpBack() {
Type genericSuperclass = getClass().getGenericSuperclass();
if (genericSuperclass instanceof ParameterizedType) {
this.genericityType = ((ParameterizedType) genericSuperclass).getActualTypeArguments()[0];
} else {
this.genericityType = Object.class;
}
}
abstract T doGson(String json);
}
public class HttpBackImp<T> extends HttpBack<T>{
@Override
public T doGson(String json){
T t = (new Gson()).fromJson(json, genericityType);
return t;
}
}
好了,来一个测试代码
String src = "{\"code\":200,\"data\":{\"age\":123}}";
HttpCallBackBean<User> huser = new HttpBackImp<HttpCallBackBean<User>>().doGson(src);
心里想着就是这样的原理,我在new的时候传入类型,然后通过获取T的类型,用gson来转对象,结果。。。
报错:
Caused by: java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.example.lenovo.myapplication.HttpCallBackBean
类型转换异常,
通过打断点可以看出问题,
Type genericSuperclass = getClass().getGenericSuperclass();
这里返回的类型为HttpBack<T>
所有这里返回this.genericityType = ((ParameterizedType) genericSuperclass).getActualTypeArguments()[0];
的结果是T 这下不就悲剧了,搞了半天拿了一个T,搞个锤子。
想了一下用网络框架的时候都是这样的,
HttpCallBackBean<User> huser = new HttpBackImp<HttpCallBackBean<User>>(){
}.doGson(src);//这里使用了一个匿名内部类
Toast.makeText(this,huser.getData().age+"",Toast.LENGTH_SHORT).show();
结果通过匿名内部类,一切都成功了
因为匿名内部类,比如是 A,也就是
A extends HttpBackImp<HttpCallBackBean<User>>(){
}
那么A的父类也就是HttpBackImp<HttpCallBackBean<User>>
所以就拿到了具体的类型,gson转换也就成功了。
其实也就是在源码阶段,我们明确了T的类型 就能转成特定的对象。边栏推荐
- 单向链表几个比较重要的函数(包括插入、删除、反转等)
- 如何在实际操作中进行亚马逊测评
- 【C语言初阶】求最小公倍数的三种方法
- What is an index in MySql?What kinds of indexes are commonly used?When does an index fail?
- focal loss原理及简单代码实现
- LNK1123: Failed during transition to COFF: invalid or corrupt file
- 浏览器中的302你真的知道吗
- Servlet life cycle
- (12)Cookie和Session
- More than pytorch from zero to build neural network to realize classification (training data sets)
猜你喜欢
随机推荐
Example of file operations - downloading and merging streaming video files
单向链表几个比较重要的函数(包括插入、删除、反转等)
What are the implications of programmatic trading rules for the entire trading system?
Matlab修改Consolas字体
走得通,看得见!你的交通“好帮手”
爱因斯坦的光子理论
bin文档读写
正则化原理的简单分析(L1/L2正则化)
为什么要学编译原理
OpenCV简介与搭建使用环境
实现一个支持请求失败后重试的JS方法
名词概念总结(不定期更新~~)
Analysis of the common methods and scopes of the three servlet containers
NetCore 5.0连接MySql
Inverted order at the beginning of the C language 】 【 string (type I like Beijing. Output Beijing. Like I)
(13)Filter过滤器
(12)Cookie和Session
跨平台桌面应用 Electron 尝试(VS2019)
简单记录下offsetof和container_of
.Net Core动态注入









