应用BeanUtils工具包操作JavaBean-2
使用BeanUtils工具包操作JavaBean-2
在操作之前就先下Apache官网上下载相就的工具,这里我们要下载commons-beanutils 和commons-logging,我下载的版本是commons-beanutils-1.8.3和commons-logging-1.1.1,然后我选择的是commons-beanutils-1.8.3.jar和commons-logging-1.1.1.jar这两个jar包
演示用Eclipse如何加入jar包,先只是引入beanutils包,等程序运行出错后再引入logging包
在前面的内省例子的基础上,用BeanUtils类先get原来设置好的属性,再将其set为一个新值。
get属性时返回的结果为该属性本来的类型,set属性时只接受该属性本来的类型。、
BeanUtils.setProperty(pt1, "x","9"); //这里的9是String类型
PropertyUtils.setProperty(pt1, "x", 9); //这里的是int类型
//这两个类BeanUtils和PropertyUtils,前者能自动将int类型转化,后者不能