黑马程序员技术交流社区
标题:
关于内省的知识
[打印本页]
作者:
桉树
时间:
2013-6-5 09:10
标题:
关于内省的知识
本帖最后由 桉树 于 2013-6-5 09:11 编辑
先上代码:
<div>public class IntroSepctorDemo {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>public static void main(String[] args) throws Exception {</div><div>//<span class="Apple-tab-span" style="white-space:pre"> </span>用内省方式获取JavaBean中的属性。</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Person person = new Person("李四","男",14) ; </div><div>
</div><div>//<span class="Apple-tab-span" style="white-space:pre"> </span>获取person 对象中name 属性的值</div><div>//<span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println( getProperty(person,"name"));</div><div>//<span class="Apple-tab-span" style="white-space:pre"> </span>设置person 对象中name 属性的值</div><div>//<span class="Apple-tab-span" style="white-space:pre"> </span>setProperty(person, "name", "张三") ;</div><div>//<span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println(person.getName());</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div>//<span class="Apple-tab-span" style="white-space:pre"> </span>通过BeanUtils 工具包</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>BeanUtils.setProperty(person, "name", "张三") ;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println(BeanUtils.getProperty(person,"name"));</div><div>//<span class="Apple-tab-span" style="white-space:pre"> </span>通过BeanUtils 设置brithday 属性。birthday 是一个复合属性。</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>BeanUtils.setProperty(person, "birthday.time", "111") ; </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println(BeanUtils.getProperty(person, "birthday.time"));</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>private static Object getProperty(Object obj , String propertyName) throws Exception{</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass()) ;</div><div>//<span class="Apple-tab-span" style="white-space:pre"> </span>获取Obj中的get方法</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Method methodGet = pd.getReadMethod() ; </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return methodGet.invoke(obj) ; </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>private static void setProperty(Object obj , String propertyName,Object value) throws Exception {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass()) ;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Method methodSet = pd.getWriteMethod() ; </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>methodSet.invoke(obj, value) ; </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>} </div><div>}</div>
复制代码
BeanUtils.setProperty(person, "birthday.time", "111") ;
System.out.println(BeanUtils.getProperty(person, "birthday.time"));
为什么 这里运行会报错?,
还有就是JavaBean 中的既然有get和set方法,为什么我们还要通过内省的方式来get和set JavaBean的对象呢?{:soso_e176:}
作者:
桉树
时间:
2013-6-5 09:12
public class IntroSepctorDemo {
public static void main(String[] args) throws Exception {
// 用内省方式获取JavaBean中的属性。
Person person = new Person("李四","男",14) ;
// 获取person 对象中name 属性的值
// System.out.println( getProperty(person,"name"));
// 设置person 对象中name 属性的值
// setProperty(person, "name", "张三") ;
// System.out.println(person.getName());
// 通过BeanUtils 工具包
BeanUtils.setProperty(person, "name", "张三") ;
System.out.println(BeanUtils.getProperty(person,"name"));
// 通过BeanUtils 设置brithday 属性。birthday 是一个复合属性。
BeanUtils.setProperty(person, "birthday.time", "111") ;
System.out.println(BeanUtils.getProperty(person, "birthday.time"));
}
private static Object getProperty(Object obj , String propertyName) throws Exception{
PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass()) ;
// 获取Obj中的get方法
Method methodGet = pd.getReadMethod() ;
return methodGet.invoke(obj) ;
}
private static void setProperty(Object obj , String propertyName,Object value) throws Exception {
PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass()) ;
Method methodSet = pd.getWriteMethod() ;
methodSet.invoke(obj, value) ;
}
}
为什么我用代码文字,会出现HTML的代码?
作者:
袁梦希
时间:
2013-6-12 18:42
咱们这里都是java基础的问题
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2