本帖最后由 ☆星痕泪 于 2013-12-21 19:07 编辑
- 示例:构建连级属性,在NewBean中引入变量DemoBean实例
- package cn.heima.Day2;
- public class DemoBean{
- private String name;
- public DemoBean(String name) {
- super();
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- package cn.heima.Day2;
- public class NewBean {
- private DemoBean db = new DemoBean();
- public DemoBean getDb() {
- return db;
- }
- public void setDb(DemoBean db) {
- this.db = db;
- }
- }
- 测试类:
- package cn.heima.Day2;
- import java.lang.reflect.InvocationTargetException;
- import org.apache.commons.beanutils.BeanUtils;
- public class ReflectBean {
- public static void main(String[] args)
- throws IllegalAccessException,
- InvocationTargetException, NoSuchMethodException {
-
- NewBean nb = new NewBean();
- BeanUtils.setProperty(nb, "db.name", "练习");
- System.out.println(BeanUtils.getProperty(nb, "db.name"));
- }
- }
复制代码 |
|