本帖最后由 杨兴庭 于 2013-8-5 18:51 编辑
ReflectPoint代码如下:- package com.itheima.day1;
- import java.util.Date;
- public class ReflectPoint {
- private Date birthday = new Date();
- public Date getBrithday() {
- return birthday;
- }
- public void setBrithday(Date brithday) {
- this.birthday = brithday;
- }
- private int x;
- public int y;
-
- public String str1 = "ball";
- public String str2 = "basketball";
- public String str3 = "itcast";
-
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
- public ReflectPoint(int x, int y) {
- super();
- this.x = x;
- this.y = y;
- }
- @Override
- 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;
- }
- @Override
- public String toString() {
- return "ReflectPoint [str1=" + str1 + ", str2=" + str2 + ", str3="
- + str3 + "]";
- }
-
- }
复制代码 IntroSpectorTest1代码如下:- package com.itheima.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 IntroSpectorTest1 {
- public static void main(String[] args)throws Exception {
- // TODO Auto-generated method stub
- ReflectPoint pt1 = new ReflectPoint(3,5);
-
- String propertyName = "x";
-
- Object retVal = getProperty(pt1, propertyName);
- System.out.println(retVal);
-
- Object value = 7;
-
- //通过BeanUtils工具包可以直接对相应对象进行getProperty()和setProperty()操作
- setProperty(pt1, propertyName, value);
- System.out.println(BeanUtils.getArrayProperty(pt1, "x").getClass().getName());
- BeanUtils.setProperty(pt1,"x","9");
- System.out.println(pt1.getX());//setProperty设置某个对象的哪个属性,并将其设置成某个值
-
- BeanUtils.setProperty(pt1, "birthday.time", "111");
- System.out.println(BeanUtils.getProperty(pt1, "birthday.time"));
- }
- //设置value的值
- private static void setProperty(Object pt1, String propertyName,
- Object value) throws IntrospectionException,
- IllegalAccessException, InvocationTargetException {
- PropertyDescriptor pd2 = new PropertyDescriptor(propertyName,pt1.getClass());
- Method methodSetX = pd2.getWriteMethod();
- methodSetX.invoke(pt1, value);
- }
- //获取methodGetX的值
- private static Object getProperty(Object pt1, String propertyName)
- throws IntrospectionException, IllegalAccessException,
- InvocationTargetException {
- PropertyDescriptor pd = new PropertyDescriptor(propertyName,pt1.getClass());
- Method methodGetX = pd.getReadMethod();
- Object retVal = methodGetX.invoke(pt1);
- return retVal;
- }
- }
复制代码 然而就是在运行的时候,总是提示报错,报错详情总是提示System.out.println(BeanUtils.getProperty(pt1, "birthday.time"));这行有问题,
想让大牛帮忙看看到底是问题错在哪儿了?谢谢!
|
|