• 反射(承接上面的泛型注解与反射)


    1.

    User.java

    package com.inspur.demo3;
    
    public class User {
        private String name;
        private String sex;
        private int age;
        public String getName() {
            return name;
        }
        public User() {
            // TODO Auto-generated constructor stub
        }
        public User(String name){
            this.name=name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        @Override
        public String toString(){
            return this.name+","+this.sex+","+this.age;
            
        }
    
    }

    2.获取私有属性

    3.

    MyClass.java

    package com.inspur.demo4;
    
    public class MyClass<A extends Integer,B,C>  {
    
    }

    DemoMain4.java

    package com.inspur.demo4;
    
    import java.lang.reflect.Field;
    import java.util.Arrays;
    
    import com.inspur.demo3.User;
    
    public class DemoMain4 {
    
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            
            
            try {
                /*Class c = User.class;
                Field f = c.getDeclaredField("name");
                System.out.println(f.getName());
                System.out.println(f.get(new User("xiaohei")));*/
                Class c1 = MyClass.class;
                System.out.println(c1.getTypeParameters().length);
                System.out.println(c1.getTypeParameters()[0].getName());
                System.out.println(c1.getTypeParameters()[0].getBounds()[0]);
                
                
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } /*catch (NoSuchFieldException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } */catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } /*catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }*/
    
        }
    
    }

    运行结果:

  • 相关阅读:
    AE旋转
    AE2
    AE1
    面试
    TS 基础数据类型
    vue-cli结构介绍
    js异步加载的5种方式
    slot 插槽的使用
    使用组件的细节点
    Vue 条件渲染
  • 原文地址:https://www.cnblogs.com/sunxiaoyan/p/9210997.html
Copyright © 2020-2023  润新知