将父类的属性赋值给子类(利用反射)
方法1
org.springframework.beans.BeanUtils.copyProperties(父类对象,子类对象);
package com.***.test11111;
//Arbor 2022/6/7
public class Dog {
String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
Father类
package com.***.test11111;
//Arbor 2022/6/7
public class Father {
String name;
int age;
Dog dog;
public void setAge(int age) {
this.age = age;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Dog getDog() {
return dog;
}
@Override
public String toString() {
return "Father{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", dog=" + dog +
'}';
}
}
package com.***.test11111;
//Arbor 2022/6/7
public class Son extends Father {
int height;
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
@Override
public String toString() {
return "Son{" +
"name='" + name + '\'' +
", age=" + age +
", dog=" + dog +
", height=" + height +
'}';
}
}
package com.***.test11111;//Arbor 2022/6/7
import org.springframework.beans.BeanUtils;
public class Test {
public static void main(String[] args) {
Dog dog = new Dog();
dog.setName("小黄");
dog.setAge(1);
Father father = new Father();
father.setAge(30);
father.setName("bb");
father.setDog(dog);
Son son = new Son();
son.setHeight(180);
BeanUtils.copyProperties(father,son);
System.out.println(son);
System.out.println(son.getDog());
System.out.println(son.getDog()==father.getDog());
}
}
控制台打印:
C:\Users\Arbor\.m2\repository\commons-net\commons-net\3.8.0\commons-net-3.8.0.jar" com.***.test11111.Test
Son{name='bb', age=30, dog=com.elemis.test11111.Dog@380fb434, height=180}
com.elemis.test11111.Dog@380fb434
true
Process finished with exit code 0
方法2
public class test{
public static void main(String[] args) throws Exception {
A a=new A();
a.setA("a");
a.setB("b");
B b=new B();
fatherClassPropertiesToChild(a,b);
System.out.println(b.getA());
}
public static <T>void fatherClassPropertiesToChild(T father,T child) throws Exception {
if (child.getClass().getSuperclass()!=father.getClass()){
throw new Exception("child 不是 father 的子类");
}
Class<?> fatherClass = father.getClass();
Field[] declaredFields = fatherClass.getDeclaredFields();
for (int i = 0; i < declaredFields.length; i++) {
Field field=declaredFields[i];
Method method=fatherClass.getDeclaredMethod("get"+upperHeadChar(field.getName()));
Object obj = method.invoke(father);
field.setAccessible(true);
field.set(child,obj);
}
}
/**
* 首字母大写,hello->Hello
*/
public static String upperHeadChar(String in) {
String head = in.substring(0, 1);
String out = head.toUpperCase() + in.substring(1, in.length());
return out;
}
}
方法3
一个一个set
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)