day02-注解&JavaBean&内省机制

2020-03-20  本文已影响0人  建国同学

----------------------写代码过程中的一些新的体会.
1.如果没有get/set. 就算你有字段,也不是属性.
2.字段是boolean的,读方法不是get开头,是is开头.
3.final修饰类不能有子类,修饰方法可以继承,不可覆盖
4.查api子类找不到找他爹

------------------------用自己的话总结一下当天的重点.

一、javabean

三大成员: 事件、方法、 属性(最重要的一个概念).

1.javabean规范

2.属性

属性不是字段,属性是通过get/set方法推导出来的
推导:如getName()方法
get 去掉 N变小写 ----> name 属性 那这样name就是属性

注意:
    不一定非得是通过工具自动生成的规范的写法
    并不存在的字段,但也是规范的get/set方法也能通过推导出属性。
    规范的IsMan方法也能推导出Man属性   

二、内省的基本操作

什么是内省

内省的作用

操作步骤

//获取属性描述器  Person类中  有属性  有方法
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
@Test
    public void testInvokeMethod() throws Exception {
        // 获取BeanInfo对象                                             
        BeanInfo beanInfo = Introspector.getBeanInfo(Person.class,Object.class);
        Object object = Person.class.newInstance();
        System.out.println(object);//Person [name=null, age=0, isMan=false]
        // 获取属性描述器  Person类中  有属性  有方法
        PropertyDescriptor[]  propertydescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor propertyDescriptor : propertydescriptors) {
            // PropertyDescriptor  这个类包装了一个属性的信息
            if (propertyDescriptor.getName().equals("name")) {
                // 获取属性的set方法
                Method writeMethod = propertyDescriptor.getWriteMethod();
                // 执行该方法 
                writeMethod.invoke(object, "小马哥");
            }else if(propertyDescriptor.getName().equals("age")){
                // 获取属性的set方法
                Method writeMethod = propertyDescriptor.getWriteMethod();
                // 执行该方法 
                writeMethod.invoke(object, 12);
            }
        }
        System.out.println(object);Person [name=小马哥, age=12, isMan=false]
    }

javaBean 与 Map 互转

    //javaBean 转 map
    public static Map<String, Object> beanToMap(Object obj) throws Exception {
        // 创建一个空的map 返回
        Map<String, Object> map = new HashMap<String, Object>();
        // 获取JavaBean中的属性名和属性值
        // 1.获取BeanInfo对象
        BeanInfo beaninfo = Introspector.getBeanInfo(obj.getClass(), Object.class);
        // 2.从BeanInfo中获取到属性描述器
        PropertyDescriptor[] descriptor = beaninfo.getPropertyDescriptors();
        // 3.遍历数组,获取到属性的名称和属性的值
        for (PropertyDescriptor propertyDescriptor : descriptor) {
            // 获取属性的名称
            String name = propertyDescriptor.getName();
            // 获取属性的methode的方法对象
            Method readMethod = propertyDescriptor.getReadMethod();
            // 获取属性的值
            Object value = readMethod.invoke(obj);
            // 把获取的属性名和属性值 存入map
            map.put(name, value);
        }
        return map;

    }
    //map 转 javaBean
    public static Object mapToBean(Map<String, Object> map, Class clz) throws Exception {
        // 根据反射创建对象
        Object instance = clz.newInstance();
        // 先通过内省获取到属性的名称key,然后从map中获取到value
        // 获取JavaBean中的属性名和属性的值
        // 1.获取BeanInfo对象
        BeanInfo beanInfo = Introspector.getBeanInfo(clz, Object.class);
        // 2.从BeanInfo中获取到属性描述器
        PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
        // 3.遍历数组,获取到属性的名称和属性的值
        for (PropertyDescriptor propertyDescriptor : descriptors) {
            // 获取属性的名称
            String name = propertyDescriptor.getName();
            // 这个name和map中的key是对应的
            Object value = map.get(name);
            // 把获取到的数据存入对象中
            Method writeMethod = propertyDescriptor.getWriteMethod();
            // 执行方法把map中的value存入到bean中相当于真实对象。setXxx (value)
            writeMethod.invoke(instance, value);
        }
        return instance;
    }

三、注解

什么是注解

注解贴在程序元素上,想要拥有某一些功能,.必须有三个角色去参与.

注解接口:Annotation

@Override                      限定覆写父类方法
@Deprecated                 标记已过时,不推荐使用.在JDK1.5之前,使用文档注释来标记过时
@SuppressWarings       抑制编译器发出的警告,@SuppressWarings(value="all")
@SafeVarargs                抑制堆污染警告(Java7开始出现的)          
@Functionallnterface     标记该接口是一个函数接口(Java8开始出现的)

元注解

元注解:在定义注解的时候用来贴在注解上的注解,用来限定注解的用法

@Target: 表示注解可以贴在哪些位置(类,方法上,构造器上等等).位置的常量封装在ElementType枚举类中:
    ElementType.ANNOTATION_TYPE只能修饰Annotation
    ElementType.CONSTRUCTOR只能修饰构造方法
    ElementType.FIELD只能修饰字段(属性),包括枚举常量
    ElementType.LOCAL_VARIABLE只能修饰局部变量
    ElementType.METHOD只能修饰方法
    ElementType.PACKAGE只能修饰包(极少使用)
    ElementType.PARAMETER只能修饰参数
    ElementType.TYPE只能修饰类,接口,枚举

@Retention:  表示注解可以保存在哪一个时期. 表示时期的值,封装在RetentionPolicy枚举类中:
        RetentionPolicy.SOURCE-------------源码时期:编译之后不存在了  >>辅助编译
        RetentionPolicy.CLASS------------字节码时期:运行时期不存在了
        RetentionPolicy.RUNTIME----------运行时期:-直存在  >>程序运行过程中都存在:白定义的注解,都要使用RUNTIME时期
上一篇 下一篇

猜你喜欢

热点阅读