• 技术文章 >java >java基础

    java使用ParameterizedType实现泛型

    小妮浅浅小妮浅浅2021-05-11 09:20:07原创2726

    本教程操作环境:windows7系统、java10版,DELL G3电脑。

    1、过程

    (1)测试属性类型

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

    (3)测试参数类型

    (4)测试返回值类型

    2、实例

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    public class Client {

      

        private Map<String, Object> objectMap;

      

        public void test(Map<String, User> map, String string) {

        }

      

        public Map<User, Bean> 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学习指路:Java基础

    专题推荐:java parameterizedtype
    上一篇:java如何访问成员变量 下一篇:java对象创建过程是什么

    相关文章推荐

    • java反射如何读写字段?• java构造器如何创建实例?• java中valueOf方法如何使用?• java中ordinal有什么用?• java反射生成对象的方法• java如何访问成员变量

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网