后台(32)——内省Introspector

探索Android软键盘的疑难杂症
深入探讨Android异步精髓Handler
详解Android主流框架不可或缺的基石
站在源码的肩膀上全解Scroller工作机制


Android多分辨率适配框架(1)— 核心基础
Android多分辨率适配框架(2)— 原理剖析
Android多分辨率适配框架(3)— 使用指南


自定义View系列教程00–推翻自己和过往,重学自定义View
自定义View系列教程01–常用工具介绍
自定义View系列教程02–onMeasure源码详尽分析
自定义View系列教程03–onLayout源码详尽分析
自定义View系列教程04–Draw源码分析及其实践
自定义View系列教程05–示例分析
自定义View系列教程06–详解View的Touch事件处理
自定义View系列教程07–详解ViewGroup分发Touch事件
自定义View系列教程08–滑动冲突的产生及其处理


版权声明


内省(Introspector)简介

内省Introspector是一种特殊的反射技术,常用于操作JavaBean。


字段和属性以及JavaBean

先来看我们经常写的一种类:

/**
 * 本文作者:谷哥的小弟
 * 博客地址:http://blog.csdn.net/lfdfhl
 */
package cn.com;

public class Girl {

    private String name;
    private int age;
    private int bust;
    private int waist;
    private int hip;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getBust() {
        return bust;
    }
    public void setBust(int bust) {
        this.bust = bust;
    }
    public int getWaist() {
        return waist;
    }
    public void setWaist(int waist) {
        this.waist = waist;
    }
    public int getHip() {
        return hip;
    }
    public void setHip(int hip) {
        this.hip = hip;
    }

}

在该类中,我们定义了几个字段并且为它们生成了对应的getXXX()和setXXX()。在很多时候我们喜欢把name,age,waist这些东西叫属性。其实,更加规范的的说法应该是:把有getXXX()或者setXXX()的变量叫做属性,把没有getXXX()或者setXXX()的变量称作字段。

很多时候,我们会看到这样的类:里面只有属性及其对应的get和set方法,这样的类也被称为JavaBean,比如此处的Girl类。


内省Introspector的使用方式

在此介绍与内省Introspector相关的操作

    /**
     * 本文作者:谷哥的小弟
     * 博客地址:http://blog.csdn.net/lfdfhl
     */
    private static void testPropertyDescriptor1() throws IntrospectionException{
        BeanInfo beanInfo = Introspector.getBeanInfo(Girl.class);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            System.out.println(propertyDescriptor.getName());
        }
    }

在此,我们利用属性描述器PropertyDescriptor获取Bean的属性,结果如下:

age
bust
class
hip
name
waist

嗯哼,发现了么?我们不但获取到了Gril类的五个属性还获取到了它的父类Object的属性class。

继续来看下面的例子:

    /**
     * 本文作者:谷哥的小弟
     * 博客地址:http://blog.csdn.net/lfdfhl
     */
    private static void testPropertyDescriptor2() throws Exception{
        Girl girl = new Girl();
        PropertyDescriptor propertyDescriptor = new PropertyDescriptor("age", Girl.class);
        Method setter = propertyDescriptor.getWriteMethod();
        setter.invoke(girl, 18);
        Method getter = propertyDescriptor.getReadMethod();
        Object age = getter.invoke(girl, null);
        System.out.println("age="+age);
    }

输出结果:

age=18

代码解析如下:

  • 得到与属性对应的属性描述器PropertyDescriptor,请参见代码第7行
  • 得到该属性的set方法后为对象赋值,请参见代码第8—9行
  • 得到该属性的get方法后取出属性值,请参见代码第10—11行

Beanutils

BeanUtils是Apache提供的操作JavaBean的工具,它使用简单,操作方便。如有需要,可至其官方网站详细了解;在此不再赘述。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷哥的小弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值