当前位置: 首页 > news >正文

抽象父类获取子类的泛型 或接口泛型

jie通过getClass().getGenericSuperclass()或者子类的泛型

getClass().getGenericInterfaces();获取多个接口的泛型

GenericTypeResolver.resolveTypeArgument(GenericityService.class, GenericitySuper.class)

抽象父类

public abstract class GenericitySuper<T> {protected Class<T> getGenericity(){return (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];}protected List<Class<T>> getInterfaceGenericity(){List<Class<T>> list = new ArrayList<Class<T>>();Type[] types = getClass().getGenericInterfaces();for(Type type:types) {list.add((Class<T>)((ParameterizedType)type).getActualTypeArguments()[0]);}return list;}
}

接口

public interface GenericityInterface<T> {}
public interface Genericity2Interface<T> {}

子类

public class GenericityService extends GenericitySuper<Data> implements GenericityInterface<CardInfo>,Genericity2Interface<Page>{public static void main(String[] args) {GenericityService service = new GenericityService();System.out.println(service.getGenericity().getSimpleName());for(Class cls:service.getInterfaceGenericity()) {System.out.println(cls.getSimpleName());}}}

http://www.lryc.cn/news/116267.html

相关文章:

  • 题目:2341.数组能形成多少数对
  • NB-IOT 和蜂窝通信(2/3/4/5G)的区别和特点是什么?
  • vue3 动态导入src/page目录下的所有子文件,并自动注册所有页面组件
  • python优雅地爬虫
  • mysql8查看执行sql历史日志、慢sql历史日志,配置开启sql历史日志general_log、慢sql历史日志slow_query_log
  • vscode关闭绑定元素“xxx”隐式具有“any”类型这类错误
  • View绘制流程-Window创建
  • Jenkins build包时虽然单元测试失败了,但是仍然可以成功build包(最终结束时build success)
  • 【vue3】基础知识点-setup语法糖
  • idol!! 2023牛客暑期多校训练营6 C
  • 深入理解Jdk5引入的Java泛型:类型安全与灵活性并存
  • idea在控制台中输出文字显示乱码
  • hacksudo3 通关详解
  • CentOS 虚拟机磁盘扩容(非常实用)
  • docker案例复现
  • 淘宝资源采集(从零开始学习淘宝数据爬取)
  • 【C语言】预处理详解
  • 2023中国(合肥)场景创新峰会成功举办,全息网御被纳入《合肥市第二批场景能力清单》
  • QT QLCDNumber 使用详解
  • 明年,HarmonyOS不再兼容Android应用!
  • 华为OD机试 - 人气最高的店铺(Java JS Python)
  • mysql sql 语句sum求和嵌套数学表达式
  • Java课题笔记~ Servlet编程
  • 修改IDEA的idea.vmoptions参数导致IDEA无法打开(ReservedCodeCacheSize)
  • P1321 单词覆盖还原
  • GODOT游戏引擎简介,包含与unity性能对比测试,以及选型建议
  • Add-in Express for Microsoft Office and Delphi Crack
  • opencv基础-34 图像平滑处理-2D 卷积 cv2.filter2D()
  • webSocket 与传统的 http 有什么优势
  • 如何测试Linux磁盘的读写速度