黑马程序员_JavaBean

来源:互联网 发布:大数据薪资待遇 编辑:程序博客网 时间:2024/06/06 13:13

------- android培训、java培训、期待与您交流!---------

类加载器

public class ReflectTest2 {public static void main(String[] args) throws Exception{InputStream ips = ReflectTest2.class.getClassLoader().getResourceAsStream("cn/itcast/day1/config.properties");Properties props = new Properties();props.load(ips);ips.close();String className = props.getProperty("className");

JavaBean

使用getXX,setXX方法来获取设置xx字段的类。方法get/set+字段名中,一般字段首字母变大写。

内省

PropertyDescriptor pd=new PropertyDescriptor(str_propertyName,pt1.getClass());//通过属性名字和对象的类获得属性描述  Method methodGetX = pd.getReadMethod();//通过属性描述获得读取属性的方法  Object retVal = methodGetX.invoke(pt1);//通过这个方法读取对象的属性

BeanInfo beanInfo =  Introspector.getBeanInfo(pt1.getClass());PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); //比上面的方法多了一个beanInfo.getPropertyDescriptors();Object retVal = null;for(PropertyDescriptor pd : pds){if(pd.getName().equals(propertyName)){Method methodGetX = pd.getReadMethod();retVal = methodGetX.invoke(pt1);break;}}

 BeanUtils与PropertyUtils

PropertyUtils.setProperty(pt1, "x", 9);//按照原本的属性类型传递参数BeanUtils.setProperty(pt1, "x", "9");//按照字符串的形式传递参数




------- android培训、java培训、期待与您交流!---------



0 0
原创粉丝点击