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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 乞文超 中级黑马   /  2012-4-20 19:20  /  1678 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.beans.PropertyDescriptor;


import java.lang.reflect.Method;


import org.apache.commons.beanutils.BeanUtils;


public class IntroSpectorTest {


     

        public static void main(String[] args) throws Exception {

           ReflectPoint pt=new ReflectPoint(3,5);

           

           String propertyName="x";

           PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt.getClass());

           Method methodGetX1 = pd.getReadMethod();

           System.out.println(methodGetX1.invoke(pt));
           

           System.out.println(BeanUtils.getProperty(pt, propertyName));

           

   

          System.out.println( pt.getX());

        }      

}


class ReflectPoint {

        private int x;

        private int y;

        
        public ReflectPoint(int x,int y) {

        
                this.x = x;

                this.y = y;

        }

        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;

        }

}
运行里,提示
Exception in thread "main" java.lang.NoSuchMethodException: Property 'x' has no getter method in class 'class ReflectPoint'
         at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1327)
         at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:770)
         at org.apache.commons.beanutils.BeanUtilsBean.getNestedProperty(BeanUtilsBean.java:715)
         at org.apache.commons.beanutils.BeanUtilsBean.getProperty(BeanUtilsBean.java:741)
         at org.apache.commons.beanutils.BeanUtils.getProperty(BeanUtils.java:382)
         at IntroSpectorTest.main(IntroSpectorTest.java:18)

3 个回复

正序浏览
我这里你的代码页可以打印出来
你这个情况应该是导commons-beanutils.jar和commons-logging.jar这2个jar包导错了吧,报了apache错误,建议你看
张老师的高新技术32讲再调一下
回复 使用道具 举报
楼主你再看看,代码在我这里运行没一点问题,下面是运行结果,
file:///D:/20120420201249.png

QQ截图20120420201249.png (8.23 KB, 下载次数: 34)

QQ截图20120420201249.png
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马