实现一个通用的方法的时候,我们有时候需要实现通过反射机制去获取对应的属性值。
下面是通过java.beans包实现这个功能的代码。
首先我们定义一个实体类。
public class Student {
private Integer age;
private String name;
private String address;
private Integer teacher;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getTeacher() {
return teacher;
}
public void setTeacher(Integer teacher) {
this.teacher = teacher;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + ((age == null) ? 0 : age.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((teacher == null) ? 0 : teacher.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (age == null) {
if (other.age != null)
return false;
} else if (!age.equals(other.age))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (teacher == null) {
if (other.teacher != null)
return false;
} else if (!teacher.equals(other.teacher))
return false;
return true;
}
然后,写一个main方法测试beans下面获取属性值得步骤
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
public class ReflectDemo {
public static void main(String[] args) {
Student s = new Student();
s.setAddress("huashan");
s.setAge(18);
s.setName("Nic");
s.setTeacher(3);
get(s);
}
public static void get(Object obj) {
try {
BeanInfo info = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] arr = info.getPropertyDescriptors();
for (PropertyDescriptor pd : arr) {
String key = pd.getName();
java.lang.reflect.Method g = pd.getReadMethod();//获取属性对应的getter方法
Object v = g.invoke(obj);
System.out.println(g + " == " + key + ":" + v);
}
} catch (IntrospectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
最后输出的结果是:
public java.lang.String com.wc.www.ScBus.entity.Student.getAddress() == address:huashan
public java.lang.Integer com.wc.www.ScBus.entity.Student.getAge() == age:18
public final native java.lang.Class java.lang.Object.getClass() == class:class com.wc.www.ScBus.entity.Student
public java.lang.String com.wc.www.ScBus.entity.Student.getName() == name:Nic
public java.lang.Integer com.wc.www.ScBus.entity.Student.getTeacher() == teacher:3
从结果看可以很好地满足要求。
知识使我快乐