java中javaBean有属性 属性名未知,现在要给这些属性设置默认值..这个怎么写
请高手指点.下边代码写了一半不会了
public class TestJavaBean {
public static void main(String[] args) throws Exception {
JavaBean j = new JavaBean();
Check(j);
PropertyDescriptor pd1 = new PropertyDescriptor("int", "int".getClass());//从这怎么写,也就是怎么给int设默认值?
Method methodSetX = pd1.getWriteMethod();
methodSetX.invoke("int", 100);
}
public static void Check(JavaBean j) throws IntrospectionException {
BeanInfo beaninfo = Introspector.getBeanInfo(j.getClass());
PropertyDescriptor[] pds = beaninfo.getPropertyDescriptors();// 获取javabean的所有属性
for (PropertyDescriptor pd : pds) {
System.out.println(pd.getPropertyType() + "=" + pd.getName());
}
}
}
class JavaBean {
// 属性
private boolean b;
private int i;
private String s;
private double d;
public boolean isB() {
return b;
}
public void setB(boolean b) {
this.b = b;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
public double getD() {
return d;
}
public void setD(double d) {
this.d = d;
}
public void show() {
System.out.println("boolean:" + isB() + ", " + "int:" + getI() + ", "
+ "String:" + getS() + ", " + "double:" + getD());
}
}