如何在JAVA中使用其他类名称克隆“类树”?

时间:2018-05-06 06:55:06

标签: java java-8 clone

假设我有一个包含很多字段的2个继承类,

我想创建另一个“继承族”,其类C,D具有相同的值 - 内容(在RUN TIME中),如下例所示。

package server.api.analytics;

public class A {

    protected String param1;
    protected String param2;
    protected String param3;

    public String getParam1() {
        return param1;
    }

    public void setParam1(String param1) {
        this.param1 = param1;
    }

    public String getParam2() {
        return param2;
    }

    public void setParam2(String param2) {
        this.param2 = param2;
    }

    public String getParam3() {
        return param3;
    }

    public void setParam3(String param3) {
        this.param3 = param3;
    }

    public static void main(String[] args) {
        B b = new B();
        b.setParam1("out_param1");
        b.setParam2("out_param2");
        b.setParam3("out_param3");
        b.setParam4("out_param4");
        b.setParam5("out_param5");
        System.out.println(b.toString());

    // OUTPUT:
     B{param1='out_param1', param2='out_param2', param3='out_param3', 
         param4='out_param4', param5='out_param5'}
  //  EXPECTED : 
     D{param1='out_param1', param2='out_param2', param3='out_param3', 
         param4='out_param4', param5='out_param5'}
    }
}

class B extends A {
    protected String param4;
    protected String param5;

    public String getParam4() {
        return param4;
    }

    public void setParam4(String param4) {
        this.param4 = param4;
    }

    public String getParam5() {
        return param5;
    }

    public void setParam5(String param5) {
        this.param5 = param5;
    }

    @Override
    public String toString() {
        return "B{" +
                "param1='" + param1 + '\'' +
                ", param2='" + param2 + '\'' +
                ", param3='" + param3 + '\'' +
                ", param4='" + param4 + '\'' +
                ", param5='" + param5 + '\'' +
                '}';
    }
}

B{param1='out_param1', param2='out_param2', param3='out_param3', param4='out_param4', param5='out_param5'}

我不想像“复制承包商”那样过去所有的属性因为有很多,如果我用克隆做“深层复制”,那么班级名称将是相同的

预期的例子:

D{param1='out_param1', param2='out_param2', param3='out_param3', param4='out_param4', param5='out_param5'}

1 个答案:

答案 0 :(得分:1)

您可以使用BeanUtils.copyProperties((Object dest, Object orig)apache common library

  

将属性值从原始bean复制到目标bean   所有属性名称相同的情况。

例如:

  • B延伸A
  • D进入C

以下代码可以将所有文件从B复制到D

import org.apache.commons.beanutils.BeanUtils;

public class Main {

    public static void main(String[] args) throws Exception {
        B source = new B();
        source.setParam("param");
        source.setParameter("parameter");
        D target = new D();
        try {
            BeanUtils.copyProperties(target, source);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(source.getParam());  // param
        System.out.println(source.getParameter());  // parameter
        System.out.println(target.getParam()); // param
        System.out.println(target.getParameter()); // parameter
    }

    public static class A {
        String param;

        public String getParam() {
            return param;
        }

        public void setParam(String param) {
            this.param = param;
        }
    }

    public static class B extends A {
        String parameter;

        public String getParameter() {
            return parameter;
        }

        public void setParameter(String parameter) {
            this.parameter = parameter;
        }
    }

    public static class C {
        String param;

        public String getParam() {
            return param;
        }

        public void setParam(String param) {
            this.param = param;
        }

    }

    public static class D extends C {
        String parameter;

        public String getParameter() {
            return parameter;
        }

        public void setParameter(String parameter) {
            this.parameter = parameter;
        }
    }
}