黑马程序员技术交流社区
标题:
代码
[打印本页]
作者:
刘德坤
时间:
2015-10-22 11:43
标题:
代码
package com.itheima;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
public class Test24 {
//存在一个javabean,设置string,Boolean,double,integer的默认初值为www.itheima.com,true,0.01D,100
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
Class<?> clazz = Class.forName("com.itheima.testBean");
Object bean = clazz.newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor pd : propertyDescriptors ){
Object name = pd.getName();
Object type = pd.getPropertyType();
Method getMethod = pd.getReadMethod();
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 String str;
private Integer i;
private double d;
public boolean isB() {
return b;
}
public void setB(boolean b) {
this.b = b;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public Integer getI() {
return i;
}
public void setI(Integer i) {
this.i = i;
}
public double getD() {
return d;
}
public void setD(double d) {
this.d = d;
}
}
作者:
695783362
时间:
2016-11-1 22:30
乱乱的,虽然看不懂但是感觉好厉害
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2