JAVA中parameterized,java使用ParameterizedType实现泛型

JAVA中parameterized,java使用ParameterizedType实现泛型1 过程 1 测试属性类型 2 打印 type 与 generictype 的区别 3 测试参数类型 4 测试返回值类型 2 实例 publicclassC privateMapob publicvoidte Mapmap Stringstring publicMaptes returnnull 测试属性类型 throws

1f99d017e047d4c102d59c5f9b8fb28e.png

1、过程

(1)测试属性类型

(2)打印type与generic type的区别

(3)测试参数类型

(4)测试返回值类型

2、实例public class Client {

private Map objectMap;

public void test(Map map, String string) {

}

public Map test() {

return null;

}

/

* 测试属性类型

*

* @throws NoSuchFieldException

*/

@Test

public void testFieldType() throws NoSuchFieldException {

Field field = Client.class.getDeclaredField(“objectMap”);

Type gType = field.getGenericType();

// 打印type与generic type的区别

System.out.println(field.getType());

System.out.println(gType);

System.out.println(“”);

if (gType instanceof ParameterizedType) {

ParameterizedType pType = (ParameterizedType) gType;

Type[] types = pType.getActualTypeArguments();

for (Type type : types) {

System.out.println(type.toString());

}

}

}

/

* 测试参数类型

*

* @throws NoSuchMethodException

*/

@Test

public void testParamType() throws NoSuchMethodException {

Method testMethod = Client.class.getMethod(“test”, Map.class, String.class);

Type[] parameterTypes = testMethod.getGenericParameterTypes();

for (Type type : parameterTypes) {

System.out.println(“type -> ” + type);

if (type instanceof ParameterizedType) {

Type[] actualTypes = ((ParameterizedType) type).getActualTypeArguments();

for (Type actualType : actualTypes) {

System.out.println(“\tactual type -> ” + actualType);

}

}

}

}

/

* 测试返回值类型

*

* @throws NoSuchMethodException

*/

@Test

public void testReturnType() throws NoSuchMethodException {

Method testMethod = Client.class.getMethod(“test”);

Type returnType = testMethod.getGenericReturnType();

System.out.println(“return type -> ” + returnType);

if (returnType instanceof ParameterizedType) {

Type[] actualTypes = ((ParameterizedType) returnType).getActualTypeArguments();

for (Type actualType : actualTypes) {

System.out.println(“\tactual type -> ” + actualType);

}

}

}

}

以上就是java使用ParameterizedType实现泛型的方法,希望能对大家有所帮助。更多Java学习指路:

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233078.html原文链接:https://javaforall.net

(0)
上一篇 2025年8月19日 下午1:01
下一篇 2025年8月19日 下午1:22


相关推荐

  • idea202112激活码永久【在线注册码/序列号/破解码】

    idea202112激活码永久【在线注册码/序列号/破解码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月19日
    42
  • 图像边缘检测经典算子及MATLAB实现

    图像边缘检测经典算子及MATLAB实现一 边缘检测边缘是图象最基本的特征 边缘检测在计算机视觉 图象分析等应用中起着重要的作用 是图象分析与识别的重要环节 这是因为子图象的边缘包含了用于识别的有用信息 所以边缘检测是图像分析和模式识别的主要特征提取手段 所谓边缘是指其周围像素灰度后阶变化或屋顶状变化的那些像素的集合 它存在于目标与背景 目标与目标 区域与区域 基元与基元之间 因此它是图象分割所依赖的重要的特征 也是纹理特征的重要信

    2026年3月17日
    2
  • createJs继承

    createJs继承下面是一个继承的例子 varcls nbsp nbsp nbsp nbsp nbsp nbsp function nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp functionMap nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp varthan this nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp this Container constructor 相当于 as3this super nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp this name

    2026年3月26日
    3
  • jquery实现向服务器下载文件的方法_从服务器下载文件命令

    jquery实现向服务器下载文件的方法_从服务器下载文件命令html页面,创建个button <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>上传文件</title><linkrel="stylesheet&quo

    2025年6月9日
    4
  • Java项目的创建以及注意事项「建议收藏」

    Java项目的创建以及注意事项「建议收藏」Java项目的创建以及注意事项一,创建JavaProject方法1,在左边空白处右键,然后选择new,再点击JavaProject。方法2,点击左上角的File,然后选择new,再点击JavaProject二,给project项目命名,然后点击finish三,打开Test,然后右…

    2022年7月8日
    25
  • java.lang.String cannot be cast to java.lang.Integer错误解决

    java.lang.String cannot be cast to java.lang.Integer错误解决java.lang.Stringcannotbecasttojava.lang.Integer错误解决当遇到t.service()forservlet[springmvc]incontextwithpath[]threwexception…java.lang.Stringcannotbecasttojava.lang.Integer上面是原来的代码,但是这里出现错与不能将object类型的数据库转成integer类型,可以使用Integer.valueOf(S

    2022年7月16日
    33

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号