黑马程序员技术交流社区

标题: 存在一个JavaBean,它包含以下几种可能的属性: 1:boolean/Boo... [打印本页]

作者: code2014    时间: 2014-5-9 08:00
标题: 存在一个JavaBean,它包含以下几种可能的属性: 1:boolean/Boo...
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;  }

作者: 相濡、彼岸    时间: 2014-8-10 20:33
怎么说呢,谢谢你的分享,给了我提示,让我能做出那道题。
作者: xiayoutianxia    时间: 2014-9-28 10:49
int 不用设定get set方法么
作者: 马嘉    时间: 2014-10-14 10:34
看到后让我豁然开朗,写的很好
作者: 猴子请d逗比    时间: 2014-12-26 01:12
写的很不错
作者: chenshiji    时间: 2014-12-29 13:27
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:29
大神楼主:

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

我知道异常原因了,还是要感谢楼主!!
作者: 胡谭龙    时间: 2015-4-1 11:19
不错不错

作者: smartisan    时间: 2015-4-25 16:59
学习了~~
作者: 猪猪fly侠    时间: 2015-5-6 08:05
学习了!!
作者: 你们敬爱的人    时间: 2015-6-7 15:12
什么意思...卡诺哪个呢.
作者: 八戒    时间: 2015-7-3 11:58
学习了...........................不错
作者: 文尐    时间: 2015-9-18 14:05
chenshiji 发表于 2014-12-29 13:41
我知道异常原因了,还是要感谢楼主!!

这是一个什么异常   我没看懂

作者: CM520Z    时间: 2015-9-18 18:00
这道题 我真不会
作者: jiaweizhang    时间: 2015-9-25 19:08
谢谢分享
作者: SuperApollo    时间: 2015-9-29 20:35
感谢分享,学习了
作者: 770365954    时间: 2015-11-15 18:00
chenshiji 发表于 2014-12-29 13:27
Exception in thread "main" java.lang.ClassNotFoundException: cn.heima.test.testBean

为什么会报这个 ...

我就不知道你二十七个技术分怎么得到的   你有cn.heima.test.testBean吗
作者: 770365954    时间: 2015-11-15 18:01
写详细点好不好楼主,我怕考培错答案啊
作者: only-hou    时间: 2016-2-18 00:15
谢谢分享
作者: 就是我    时间: 2016-3-23 09:42
题目都不是太理解,还是得好好捋一捋




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2