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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

public static void main(String[] args) throws Exception {   
Class clazz = Class.forName("cn.heima.test.testBean");  
Object bean = clazz.newInstance();
  BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
// System.out.println(beanInfo);
  PropertyDescriptor[] propertyDescriptors = beanInfo     .getPropertyDescriptors();
  for (PropertyDescriptor pd : propertyDescriptors) {    // System.out.println(pd);    // 获取属性名
   Object name = pd.getName();    // 获取属性类型
   Object type = pd.getPropertyType();    // 获取get方法
   Method getMethod = pd.getReadMethod();    // 获取set方法
   Method setMethod = pd.getWriteMethod();  
   if (!"class".equals(name)) {     if (setMethod != null) {      if (type == boolean.class || type == Boolean.class) {
      setMethod.invoke(bean, true);      }
     if (type == String.class) {
      setMethod.invoke(bean, "www.itheima.com");      }
     if (type == int.class || type == Integer.class) {       setMethod.invoke(bean, 100);      }
     if (type == double.class || type == Double.class) {  
      setMethod.invoke(bean, 0.01D);      }     }
    if (getMethod != null) {
     System.out.println(type + " " + name + "="        + getMethod.invoke(bean, null));     }    }   }  } }  
class testBean {
private boolean b;  private Integer i;
private String str;  private Double d;  
public Boolean getB() {   return b;  }  
public void setB(Boolean b) {   this.b = b;  }  
public Integer getI() {   return i;  }  
public void setI(Integer i) {   this.i = i;  }  
public String getStr() {   return str;  }  
public void setStr(String str) {   this.str = str;  }  
public Double getD() {   return d;  }  
public void setD(Double d) {   this.d = d;  }

20 个回复

倒序浏览
怎么说呢,谢谢你的分享,给了我提示,让我能做出那道题。
回复 使用道具 举报
int 不用设定get set方法么
回复 使用道具 举报
看到后让我豁然开朗,写的很好
回复 使用道具 举报
写的很不错
回复 使用道具 举报
Exception in thread "main" java.lang.ClassNotFoundException: cn.heima.test.testBean

为什么会报这个异常呢?
回复 使用道具 举报
大神楼主:

Exception in thread "main" java.lang.ClassNotFoundException: cn.heima.test.testBean;

为什么会报这个异常呢?
回复 使用道具 举报
chenshiji 发表于 2014-12-29 13:29
大神楼主:

Exception in thread "main" java.lang.ClassNotFoundException: cn.heima.test.testBean;

我知道异常原因了,还是要感谢楼主!!
回复 使用道具 举报
不错不错
回复 使用道具 举报
学习了~~
回复 使用道具 举报
学习了!!
回复 使用道具 举报
什么意思...卡诺哪个呢.
回复 使用道具 举报
学习了...........................不错
回复 使用道具 举报
chenshiji 发表于 2014-12-29 13:41
我知道异常原因了,还是要感谢楼主!!

这是一个什么异常   我没看懂
回复 使用道具 举报
这道题 我真不会
回复 使用道具 举报
谢谢分享
回复 使用道具 举报
感谢分享,学习了
回复 使用道具 举报
chenshiji 发表于 2014-12-29 13:27
Exception in thread "main" java.lang.ClassNotFoundException: cn.heima.test.testBean

为什么会报这个 ...

我就不知道你二十七个技术分怎么得到的   你有cn.heima.test.testBean吗
回复 使用道具 举报
写详细点好不好楼主,我怕考培错答案啊
回复 使用道具 举报
谢谢分享
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马