[英]Copy all values from fields in one class to another class
I have 2 classes:我有 2 节课:
public class A {
Integer a;
X object;
List<Y> list;
}
public class B {
Integer a;
X object;
List<Y> list;
}
Cloud you tell me how to copy fields from class A to B Is it a good idea to use Gson? Cloud 你告诉我如何将字段从 class A 复制到 B 使用 Gson 是个好主意吗? are there better solutions?
有更好的解决方案吗?
Gson gson= new Gson();
String tmp = gson.toJson(a);
B b = gson.fromJson(tmp,B.class);
The most oop method is to add a constructor to each of the classes that gets the other class and copies the fields needed.最 oop 的方法是在每个类中添加一个构造函数,获取另一个 class 并复制需要的字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.