如何在 JAVA 中使用其他 类 名称克隆 "class tree"?
How to clone "class tree" with other classes name in JAVA?
假设我有 2 个继承的 classes 有很多字段,
我想用 classes C,D 创建另一个 "inheritance family",它具有相同的值 - 内容(在 运行 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'}
我不想像 "copy contractor" 一样遍历所有属性,因为有很多属性,如果我使用克隆 "deep copy",class 名称将是一样
预期示例:
D{param1='out_param1', param2='out_param2', param3='out_param3', param4='out_param4', param5='out_param5'}
您可以使用 BeanUtils.copyProperties((Object dest, Object orig)
of the apache common library:
Copy property values from the origin bean to the destination bean for
all cases where the property names are the same.
例如:
- 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;
}
}
}
假设我有 2 个继承的 classes 有很多字段,
我想用 classes C,D 创建另一个 "inheritance family",它具有相同的值 - 内容(在 运行 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'}
我不想像 "copy contractor" 一样遍历所有属性,因为有很多属性,如果我使用克隆 "deep copy",class 名称将是一样
预期示例:
D{param1='out_param1', param2='out_param2', param3='out_param3', param4='out_param4', param5='out_param5'}
您可以使用 BeanUtils.copyProperties((Object dest, Object orig)
of the apache common library:
Copy property values from the origin bean to the destination bean for all cases where the property names are the same.
例如:
- 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;
}
}
}