Java反射小案例(一)
如题:一个实体类,实例化两个对象A和B,将A对象属性赋值。 通过反射的技术将A对象的属性值赋给B对象(同一个类,不同对象相同属性赋值)
// 实例化两个对象A和B
Student stuA=new Student(1,"赵柳","女","18","湖北武汉","2018-12-1");
Student stuB=new Student();
// 获取Student B类对象
Class clazz = stuB.getClass();
// 获取Student类 所有属性
Field[] declaredFields = clazz.getDeclaredFields();
for (Field field : declaredFields) {
// 打破封装(私有属性)
field.setAccessible(true);
// 使用set方法赋值,参数一:需赋值的对象,参数二:获取值的对象
field.set(stuB, field.get(stuA));
}
System.out.println(stuB);
}
Student类
package com.simple.regflectOne;
public class Student {
private int studentId;
private String studentName;
private String studentGender;
private String studentAge;
private String adress;
private String birthDay;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentGender() {
return studentGender;
}
public void setStudentGender(String studentGender) {
this.studentGender = studentGender;
}
public String getStudentAge() {
return studentAge;
}
public void setStudentAge(String studentAge) {
this.studentAge = studentAge;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public String getBirthDay() {
return birthDay;
}
public void setBirthDay(String birthDay) {
this.birthDay = birthDay;
}
public Student(int studentId, String studentName, String studentGender,
String studentAge, String adress, String birthDay) {
super();
this.studentId = studentId;
this.studentName = studentName;
this.studentGender = studentGender;
this.studentAge = studentAge;
this.adress = adress;
this.birthDay = birthDay;
}
public Student() {
super();
}
@Override
public String toString() {
return "Student [studentId=" + studentId + ", studentName="
+ studentName + ", studentGender=" + studentGender
+ ", studentAge=" + studentAge + ", adress=" + adress
+ ", birthDay=" + birthDay + "]";
}
}
Java反射小案例(二)
如题: 两个不同实体类,实例化两个对象A和B,将A对象属性赋值。 通过反射的技术将A对象的属性值赋给B对象中相同的属性(不同类,相同属性赋值)
package RegflectOne;
import java.lang.reflect.Field;
public class TestTwo {
public static void main(String[] args) throws IllegalArgumentException,
IllegalAccessException {
// 实例化两个对象A和B
Student stuA = new Student(1, "王武", "男", "18", "湖北武汉", "2018-12-1");
Teacher teaB = new Teacher();
// 获取类对象
Class stu = Student.class;
Class tea = Teacher.class;
// 获取类类中属性
Field[] declaredFieldsStu = stu.getDeclaredFields();
Field[] declaredFieldsTea = tea.getDeclaredFields();
for (Field fieldStu : declaredFieldsStu) {
for (Field fieldTea : declaredFieldsTea) {
// 打破封装
fieldStu.setAccessible(true);
fieldTea.setAccessible(true);
//判断给相同的属性赋值
if (fieldStu.getName().equals(fieldTea.getName())) {
fieldTea.set(teaB, fieldStu.get(stuA));
}
}
}
System.out.println(teaB);
}
}
Student类
package RegflectOne;
public class Student {
private int studentId;
private String studentName;
private String studentGender;
private String studentAge;
private String adress;
private String birthDay;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentGender() {
return studentGender;
}
public void setStudentGender(String studentGender) {
this.studentGender = studentGender;
}
public String getStudentAge() {
return studentAge;
}
public void setStudentAge(String studentAge) {
this.studentAge = studentAge;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public String getBirthDay() {
return birthDay;
}
public void setBirthDay(String birthDay) {
this.birthDay = birthDay;
}
public Student(int studentId, String studentName, String studentGender,
String studentAge, String adress, String birthDay) {
super();
this.studentId = studentId;
this.studentName = studentName;
this.studentGender = studentGender;
this.studentAge = studentAge;
this.adress = adress;
this.birthDay = birthDay;
}
public Student() {
super();
}
@Override
public String toString() {
return "Student [studentId=" + studentId + ", studentName="
+ studentName + ", studentGender=" + studentGender
+ ", studentAge=" + studentAge + ", adress=" + adress
+ ", birthDay=" + birthDay + "]";
}
}
Teacher类
package RegflectOne;
public class Teacher {
private int teacherId;
public String studentName;
public String studentGender;
public int getTeacherId() {
return teacherId;
}
public void setTeacherId(int teacherId) {
this.teacherId = teacherId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentGender() {
return studentGender;
}
public void setStudentGender(String studentGender) {
this.studentGender = studentGender;
}
public Teacher(int teacherId, String studentName, String studentGender) {
super();
this.teacherId = teacherId;
this.studentName = studentName;
this.studentGender = studentGender;
}
public Teacher() {
super();
}
@Override
public String toString() {
return "Teacher [teacherId=" + teacherId + ", studentName="
+ studentName + ", studentGender=" + studentGender + "]";
}
}