01..---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------
对JAVABEAN的简单内省操作
获取JAVABEAN类属性的两种方式:
1.
ReflectPoint pt1 =new ReflectPoint(3,5);
String PropretyName = "x"; //获取的变量名(私有的看不到)的值值
PropretyDescriptor pd = new PropretyDescriptor(PropretyName,pt1.class); //属性描述类
Method methodgetX = pd.getReadMethod();
Object retVal = methodgetX.invoke(pt1);
2.
BeanInfo beaninfo= IntroSpector.getBeanInfo(pt1.getClass());
PropretyDescriptor pd =beanInfo.getPropretyDescriptors();
Object retVal=null;
for(PropertyDescriptor pd : pd){
if(pd.getName().equals(propertyName)){
Method methodGetX = pd.getReadMethos();
retVal =methodGetX.invoke(pt1);
}
}
return retVal;
使用BeanUtils工具包操作JavaBean
1.导入BeanUtils包到工程下,在工程上创建文件lib复制到其目录下,右键AddbuildPath
2,导入commons -logging包
BeanUtils.setProperty(pt1,"birthday.time,"1111"); //以字符串的类型进行操作
PropertyUtils.setProperty(pt1,"x",6);//以属性本身的类型操作
--------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------