父类属性值的copy

最近开发中遇到这样一个问题将父类的属性值copy到子类中,从而对子类添加一些其他属性。

父类:

package com.jalja.org.jms.test01;

import java.util.Date;

public class User {
    private Integer id;
    private String name;
    private Date time;
    public User() {
        super();
    }
    public User(String name, Date time) {
        super();
        this.name = name;
        this.time = time;
    }
    public User(Integer id, String name, Date time) {
        super();
        this.id = id;
        this.name = name;
        this.time = time;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getTime() {
        return time;
    }
    public void setTime(Date time) {
        this.time = time;
    }
}

子类:

package com.jalja.org.jms.test01;

public class UserVO extends User{
    private int sex;
    public int getSex() {
        return sex;
    }
    public void setSex(int sex) {
        this.sex = sex;
    }
}
package com.jalja.org.jms.test01;

import java.lang.reflect.Field;
import java.util.Date;

public class CopyObj {
    /**
     * 我们的业务是需要在现有的类上扩展一些属性供视图层使用
     * 1、在原有类上扩展一些属性也可以实现,但这样需要在别人的代码中直接更改,个人认为不是很好。
     * 2、写一个扩展类 UserVO 继承原类  User
     * @param args
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
        User user=new User("2131",new Date());
        //将父类强转为子类出现异常Exception in thread "main" java.lang.ClassCastException:
        //UserVO vo1=(UserVO) user;
        //vo1.setSex(0);

        UserVO vo2=(UserVO) copyObj(user,new UserVO());
        vo2.setSex(0);
        System.out.println(vo2.getName());

    }
    /** 子对象copy父对象的属性值
     *  copyObj 继承 targetObj
     * @param targetObj  被copy的目标对象
     * @param copyObj  copy后的对象
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     */
    public static <T> T copyObj(T targetObj,T copyObj ) throws IllegalArgumentException, IllegalAccessException{
        Field [] fields=targetObj.getClass().getDeclaredFields();
        Field [] fieldsvo=copyObj.getClass().getSuperclass().getDeclaredFields();
        for(Field f:fields){
            f.setAccessible(true);
            for(Field f2:fieldsvo){
                f2.setAccessible(true);
                if(f.get(targetObj)!=null&& f.getName().toString().equals(f2.getName().toString())){
                    f2.set(copyObj,f.get(targetObj)) ;
                }
             }
        }
        return copyObj;
    }
}
时间: 2024-07-30 23:40:00

父类属性值的copy的相关文章

通过反射获取某个对象下的属性值,或通过父类获取

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.*; /** * 通过反射获取某个对象下的属性值,或通过父类获取 * User: wangyongfei * Date

spring mvc 处理pojo传递对象时该对象继承父类的属性在网络接收端接收该属性值总是null,why?

//=========================== 情形一: ===============================//在网络上传递User1类对象时info属性值在网络的另一端能够接收到! public class User1 implements Serializable { public String info = null; public String userName = null; public String userPWD = null; } //=========

java 反射机制--根据属性名获取属性值

1.考虑安全访问范围内的属性,没有权限访问到的属性不读取 [java] view plain copy /** * 根据属性名获取属性值 * * @param fieldName * @param object * @return */ private String getFieldValueByFieldName(String fieldName, Object object) { try { Field field = object.getClass().getField(fieldName

为啥NSString的属性要用copy而不用retain

之前学习生活中,知道NSString的属性要用copy而不用retain,但是不知道为啥,这两天我研究了一下,然后终于明白了. 具体原因是因为用copy比用retain安全,当是NSString的时候,其实用copy和retain都行,当用NSMutableString,那么就要用copy,NSMutableString的值不会被修改,而用retain的时候,NSMutableString的值会被修改,具体情况,可以看下面的代码: #import <Foundation/Foundation.h

用反射获取类属性值并且赋值

/** * * @projectname 项目名称: cms-interface * @packageclass 包及类名: com.jy.modules.utils.CreditUtil.java * @description 功能描述: Object 可以是任意对象在不确定对象的时候获取值设置值 * @author 作 者: zhouzhiwei * @param 参 数: @param Requestobj * @param 参 数: @param obj * @param 参 数: @r

java 反射实现不同对象相同属性值复制

1.此方法会过滤final字段 2.此方法会过滤对象字段 3.此方法会兼容同对象之间.不同对象之间属性值复制 package com.bin.design.util; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.List; import com.bin.des

selenium_webdriver(python)获取元素属性值,浏览器窗口控制、网页前进后退,title/url打印

[python] view plain copy <span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;"># coding: UTF-8    #这句是为了声明编码格式,一定要有</span></span> [python] view plain copy <span style="fo

spring boot 读取配置文件(application.yml)中的属性值111

在spring boot中,简单几步,读取配置文件(application.yml)中各种不同类型的属性值: 1.引入依赖: [html] view plain copy <!-- 支持 @ConfigurationProperties 注解 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-proc

Java反射获取实体的所有可见属性值,返回Object数组

获取实体的所有可见属性值 以下代码提供了两种实现,一种是基于List,一种是基于Map; 基于List的实现更节省内存,更高效一些:如果你有其它特殊的需求,可以根据实际参考以下代码进行扩展,或许有需要用到Map的情况呢! 当然,使用BeanUtils.describe或PropertyUtils.describe一两句代码就搞定了,但还需要额外添加包引用:另外效率方面是不是更高呢?我没有做过测试. /** * 获取实体的所有可见属性值 * @param object 实体类的实例 * @retu