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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李挺 中级黑马   /  2013-2-17 13:01  /  1427 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 李挺 于 2013-2-17 14:51 编辑
  1. import org.apache.commons.beanutils.BeanUtils;

  2. public class PropertyDescriptorDemo {

  3.         /**
  4.          * @param args
  5.          */
  6.         public static void main(String[] args) throws Exception{
  7.                 // TODO Auto-generated method stub
  8.                 ReflectPoint rf=new ReflectPoint(3, 5);
  9.                
  10.                 System.out.println(BeanUtils.getProperty(rf, "x"));
  11.         }

  12. }

  13. class ReflectPoint          //这是个javabean
  14. {
  15.         private int x;
  16.         private int y;        
  17.         public ReflectPoint(int x, int y) {
  18.                 super();
  19.                 this.x = x;
  20.                 this.y = y;
  21.         }
  22.         public int getX() {
  23.                 return x;
  24.         }
  25.         public void setX(int x) {
  26.                 this.x = x;
  27.         }
  28.         public int getY() {
  29.                 return y;
  30.         }
  31.         public void setY(int y) {
  32.                 this.y = y;
  33.         }
  34.         @Override
  35.         public String toString() {
  36.                 return "ReflectPoint [x=" + x + ", y=" + y + "]";
  37.         }
  38.         
  39. }
复制代码
这里会报错
Exception in thread "main" java.lang.NoSuchMethodException: Property 'x' has no getter method in class 'class day2.ReflectPoint'
可是我有getX()方法啊

2 个回复

正序浏览
谢波 发表于 2013-2-17 14:44
你把class ReflectPoint写到一个新的java文件中并且改成public就行了
内省操作的类都必须是public的 ...

OK了,谢谢提醒
回复 使用道具 举报
你把class ReflectPoint写到一个新的java文件中并且改成public就行了
内省操作的类都必须是public的
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马