public class Test2 {
public static void main(String[] args) throws Exception {
A a = new A();
Field[] fa = a.getClass().getFields();
Field[] fc = Class.forName("c").getFields();
for (Field f : fa) {
for (Field ff : fc) {
if (ff.getName().equals(f.getName())) {
ff.setAccessible(true);
ff.set(a.c, f.get(a));
}
}
}
}
}
class A{
C c;
}
class C{
public static List<Bean> list = new ArrayList<Bean>();
static{
Bean b1 = new Bean("id", "id");
Bean b2 = new Bean("name", "name");
Bean b3 = new Bean("newAge", "age");
Bean b4 = new Bean("email", "newEmail");
list.add(b1);
list.add(b2);
list.add(b3);
list.add(b4);
}
public static void main(String[] args) throws Exception{
A a = new A();
a.setId(1l);
a.setName("json");
a.setNewAge(11);
a.setEmail("aa@163.com");
B b = new B();
cloneObj(list, a, b);
System.out.println(b.getId() + "--" + b.getName() + "--" + b.getAge() + "--" + b.getNewEmail());
}
}
class A{
private Long id;
private String name;
private Integer newAge;
private String email;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getNewAge() {
return newAge;
}
public void setNewAge(Integer newAge) {
this.newAge = newAge;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
class B{
private Long id;
private String name;
private Integer age;
private String newEmail;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getNewEmail() {
return newEmail;
}
public void setNewEmail(String newEmail) {
this.newEmail = newEmail;
}
}
class Bean{
private String srcField;
private String distField;
public Bean(String srcField, String distField){
this.srcField = srcField;
this.distField = distField;
}
public String getSrcField() {
return srcField;
}
public void setSrcField(String srcField) {
this.srcField = srcField;
}
public String getDistField() {
return distField;
}
public void setDistField(String distField) {
this.distField = distField;
}
}
--------------------编程问答--------------------
Class<?> srcType = srcField.getType();
Class<?> distType = distField.getType();
if(srcType == distType){
String srcGetMethodName = "get" + srcFieldName.substring(0, 1).toUpperCase() + srcFieldName.substring(1);
String distSetMethodName = "set" + distFieldName.substring(0, 1).toUpperCase() + distFieldName.substring(1);
Method getSrcMethod = classA.getMethod(srcGetMethodName, new Class[]{});
Method setDistMethod = classB.getMethod(distSetMethodName, new Class[]{distType});
setDistMethod.invoke(b, getSrcMethod.invoke(a, new Object[]{}));
}
这个只能复制普通的属性,A类中的对象属性无法复制
--------------------编程问答--------------------
你这个只是把A的值给C与问题描述不符
--------------------编程问答--------------------
BeanUtils 只能解决
A
C
B
C
这种情况
--------------------编程问答--------------------
public class Client {
public static List<Bean> list = new ArrayList<Bean>();
static{
Bean b1 = new Bean("id", "id");
Bean b2 = new Bean("name", "name");
Bean b3 = new Bean("newAge", "age");
Bean b4 = new Bean("email", "newEmail");
Bean b5 = new Bean("c", "d");
list.add(b1);
list.add(b2);
list.add(b3);
list.add(b4);
list.add(b5);
}
public static void main(String[] args) throws Exception{
A a = new A();
a.setId(1l);
a.setName("json");
a.setNewAge(11);
a.setEmail("aa@163.com");
C c = new C();
c.setId(1000l);
a.setC(c);
B b = new B();
cloneObj(list, a, b);
System.out.println(b.getId() + "--" + b.getName() + "--" + b.getAge() + "--" + b.getNewEmail() + "[c:" + b.getD().getId() + "]");
}