zoukankan      html  css  js  c++  java
  • Java 之 PropertyDescriptor

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

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

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

    /**
     *
     * @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方法删除后运行的结果是什么呢?

    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)
    
  • 相关阅读:
    解决pydev无法增加jython271 interpreter的问题
    使用pygal 做chart图的经验分享
    python4delphi 使用
    Some Delphi tips
    http request method and response codes
    flask 项目的开发经验总结
    解决pydev报unsolved import的问题
    python __future__ package的几个特性
    用一个简单的例子来理解python高阶函数
    正确地组织python项目的结构
  • 原文地址:https://www.cnblogs.com/wei-zw/p/8797778.html
Copyright © 2011-2022 走看看