java 设置javabean未知属性
设置javabean中未知属性的值,请帮忙看看这段代码问题在哪,或者给出您的代码,谢谢!classJavaBeanTest{privatebooleanflag;priv...
设置javabean中未知属性的值,请帮忙看看这段代码问题在哪,或者给出您的代码,谢谢!
class JavaBeanTest
{
private boolean flag;
private int i;
private String str;
private double d;
public boolean isFlag()
{
return flag;
}
public void setFlag(boolean flag)
{
this.flag = flag;
}
public int getI()
{
return i;
}
public void setI(int 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;
}
}
public class Test7
{
public static void main(String[] args) throws Exception
{
Class clazz = Class.forName("test.JavaBeanTest");
Object bean = clazz.newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(clazz.getClass());
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor pd : pds)
{
Object name = pd.getName();
Object type = pd.getPropertyType();
Method getMethod = pd.getReadMethod();
Method setMethod = pd.getWriteMethod();
if(type==String.class)
setMethod.invoke(bean, "abcde");
else if(type==int.class)
setMethod.invoke(bean, 100);
else if(type==boolean.class)
setMethod.invoke(bean, true);
else if(type==double.class)
setMethod.invoke(bean, 0.01D);
System.out.println("修改后:"+getMethod.invoke(bean));
}
}
} 展开
class JavaBeanTest
{
private boolean flag;
private int i;
private String str;
private double d;
public boolean isFlag()
{
return flag;
}
public void setFlag(boolean flag)
{
this.flag = flag;
}
public int getI()
{
return i;
}
public void setI(int 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;
}
}
public class Test7
{
public static void main(String[] args) throws Exception
{
Class clazz = Class.forName("test.JavaBeanTest");
Object bean = clazz.newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(clazz.getClass());
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor pd : pds)
{
Object name = pd.getName();
Object type = pd.getPropertyType();
Method getMethod = pd.getReadMethod();
Method setMethod = pd.getWriteMethod();
if(type==String.class)
setMethod.invoke(bean, "abcde");
else if(type==int.class)
setMethod.invoke(bean, 100);
else if(type==boolean.class)
setMethod.invoke(bean, true);
else if(type==double.class)
setMethod.invoke(bean, 0.01D);
System.out.println("修改后:"+getMethod.invoke(bean));
}
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询