A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 秦兰之 中级黑马   /  2013-6-22 21:36  /  2780 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 秦兰之 于 2013-6-22 21:39 编辑

编译通过,运行异常。
package cn.itcast.day1;

public class ReflectPoint {
    private int x;   
    public int y;                                                //自定义两个成员变量。
    public String str1 ="ball";
    public String str2 ="basketball";
    public String str3 ="itcast";
   
    public int getX() {                                            //抽取getter和setter方法
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
   
    @Override    // 生成哈希值代码,和equals()方法。
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + x;
        result = prime * result + y;
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ReflectPoint other = (ReflectPoint) obj;
        if (x != other.x)
            return false;
        if (y != other.y)
            return false;
        return true;
    }   
   
    public ReflectPoint(int x, int y) {                                //生成构造方法的源代码
        super();
        this.x = x;
        this.y = y;
    }
    @Override
    public String toString() {
        return "ReflectPoint [x=" + x + ", y=" + y + "]";
    }
   
}

package cn.itcast.day1;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.commons.beanutils.BeanUtils;
public class IntrspectorTest {
    public static void main(String[] args)throws Exception {// TODO Auto-generated method stub        
        /*    对JavaBean 的简单内省操作        */
        ReflectPoint pt1=new ReflectPoint(3,5);            //自定义一个反射方法对象。   
        String propertyName="x";
        //"x" --> "X"--> "getX()" --> Methodget() -->//PropertyDescriptor 描述 Java Bean 通过一对存储器方法导出的一个属性。Property:物业 Descriptor:描述        
        Object retVal = getProperty(pt1, propertyName);//自定义一个返回值对象,存储带参数的获取x反射方法对象。
        System.out.println(retVal);
        
        Object value=9;
        //设置(对象,属性名,值)    n.房地产(property的名词复数);财产;所有权;特性
        setProperties(pt1, propertyName, value);
        
        System.out.println(BeanUtils.getProperty(pt1,"x"));
        BeanUtils.setProperty(pt1, "x","9");
        
        System.out.println(pt1.getX());//打印设置后x的值。
        
    }


    private static void setProperties(Object pt1, String propertyName,
            Object value) throws IntrospectionException,
            IllegalAccessException, InvocationTargetException {
        PropertyDescriptor pd2=new PropertyDescriptor(propertyName,pt1.getClass());//通过属性名和获取反射类方法,得到一个属性对象。
        Method methodSetX=pd2.getReadMethod();//自定义一个 设置X的反射方法对象,存储 属性对象的调用获取读取方法的方法。
        methodSetX.invoke(pt1,value);//调用x反射方法对象,设置一个X对象的值。
    }
   
   
    //对某个对象上的属性进行获取。(重构     抽取方法)
    private static Object getProperty(Object pt1, String propertyName)
            throws IntrospectionException, IllegalAccessException,
            InvocationTargetException {
        /*PropertyDescriptor pd=new PropertyDescriptor(propertyName,pt1.getClass());//通过属性名和获取反射类方法,得到一个属性对象。
        Method methodGetX=pd.getReadMethod();//自定义一个获取X的反射方法对象,(导入反射方法包)存储 属性对象的调用获取读取方法的方法。
        
        Object retVal=methodGetX.invoke(pt1);*/
        
        /*    对JavaBean 的复杂内省操作        */
        BeanInfo beaninfo=Introspector.getBeanInfo(pt1.getClass());//
        PropertyDescriptor[] pds=beaninfo.getPropertyDescriptors();//用一个属性对象数组存储所有的属性对象。
        Object retVal=null;
        for(PropertyDescriptor pd:pds){
            if(pd.getName().equals(propertyName))
            {
                Method methodGetX=pd.getReadMethod();//自定义一个获取X的反射方法对象,(导入反射方法包)存储 属性对象的调用获取读取方法的方法。               
                retVal = methodGetX.invoke(pt1);//自定义一个获取X的反射方法对象,(导入反射方法包)存储 属性对象的调用获取读取方法的方法。
               
                break;
            }
        }        
        
        return retVal;
    }   
}

捕获yichang.PNG (23.8 KB, 下载次数: 0)

捕获yichang.PNG

评分

参与人数 1技术分 +1 收起 理由
张勇1993 + 1 很给力!

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马