简体   繁体   中英

Copy all values from fields in one class to another class

I have 2 classes:

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? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM