前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Java 之 PropertyDescriptor[通俗易懂]

Java 之 PropertyDescriptor[通俗易懂]

作者头像
全栈程序员站长
发布2022-09-06 17:33:03
发布2022-09-06 17:33:03
4.5K00
代码可运行
举报
运行总次数:0
代码可运行

大家好,又见面了,我是你们的朋友全栈君。

PropertyDescriptor 描述了一个JavaBean 属性的一对访问方法即 getter和setter。

常用的构造方法是PropertyDescriptor(String propertyName,Class<?> beanClass);

propertyName就是属性的名称,beanClass就是这个属性对应属于哪个对象的Class.

代码语言:javascript
代码运行次数:0
运行
复制
/**
 *
 * @author zhangwei_david
 * @version $Id: PropertyDescriptorDemo.java, v 0.1 2015年5月25日 下午8:17:59 zhangwei_david Exp $
 */
public class PropertyDescriptorDemo {

    /**
     *
     * @param args
     * @throws IntrospectionException
     * @throws InvocationTargetException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public static void main(String[] args) throws IntrospectionException, IllegalAccessException,
    IllegalArgumentException, InvocationTargetException {
        // bean的实例
        Form form = new PropertyDescriptorDemo().new Form();
        // 创建属性name 的PropertyDescriptor
        PropertyDescriptor pd = new PropertyDescriptor("name", form.getClass());
        // 获取属性的setter方法
        Method writer = pd.getWriteMethod();
        // 反射调用setter方法设置值
        writer.invoke(form, "TEST");
        // 输入setter以后的结果
        System.out.println(form.getName());
        // 获取getter方法
        Method reader = pd.getReadMethod();
        // 获取属性值
        String value = (String) reader.invoke(form);
        // 获取属性
        String name = pd.getName();

        System.out.println(name + "=" + value);

    }

    /**
     *
     *  测试表单
     *
     * @author zhangwei_david
     * @version $Id: PropertyDescriptorDemo.java, v 0.1 2015年5月25日 下午8:40:29 zhangwei_david Exp $
     */
    class Form {
        /**属性name**/
        private String name;

        /**
         * Getter method for property <tt>name</tt>.
         *
         * @return property value of name
         */
        public String getName() {
            return name;
        }

        /**
         * Setter method for property <tt>name</tt>.
         *
         * @param name value to be assigned to property name
         */
        public void setName(String name) {
            this.name = name;
        }

    }
}

输出的结果是:

TEST name=TEST

可以发现,正确调用了setter和getter方法,如果将Form中的getter方法删除后运行的结果是什么呢?

代码语言:javascript
代码运行次数:0
运行
复制
Exception in thread "main" java.beans.IntrospectionException: Method not found: setName
	at java.beans.PropertyDescriptor.<init>(PropertyDescriptor.java:110)
	at java.beans.PropertyDescriptor.<init>(PropertyDescriptor.java:70)
	at com.cathy.demo.reflect.PropertyDescriptorDemo.main(PropertyDescriptorDemo.java:32)

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档