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());
}
}
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());
}
}
...全文
相关推荐
java 基础之JavaBean属性命名规范问题
主要介绍了java 基础之JavaBean属性命名规范问题的相关资料,需要的朋友可以参考下
java 基础之JavaBean属性命名规范问题.docx
java 基础之JavaBean属性命名规范问题.docx
Java面向对象(基础)- 属性赋值过程、JavaBean和UML类图
Java面向对象(基础)-- 属性赋值过程、JavaBean和UML类图
jsp中使用javabean实例介绍
主要介绍了jsp中使用javabean的实例,同时介绍了相关属性,需要的朋友可以参考下
Java试题-6:JavaBean的应用
问题描述:存在一个JavaBean,它包含以下几种可能的属性: 1.boolean/Boolean 2.int/Integer 3.String 4.double/Double 属性名未知, 现在要给这些属性设置默认值, 以下是要求的默认值:String类型的默认值为字符串 www.itheima.com , int/Integer类型的默认值为100, boolean/Boolean类型的默认值为true, double/Double的默认值为0.01D. 只需要设置带有getXxx/isXxx/setXxx方法的属性, 非JavaBean属性不设置, 请用代码实现.


Eclipse
