通过java反射获得属性值
Java的反射非常强大,传递class, 可以动态的生成该类、取得这个类的所有信息,包括里面的属性、方法以及构造函数等,甚至可以取得其父类或父接口里面的内容。
- static Object create(Class clazz) throws Exception {
- Constructor con = clazz.getConstructor(String.class);
- Object obj = con.newInstance("test name");
- return obj;
- }
- static void invoke1(Object obj, String methodName)
- throws IllegalArgumentException, IllegalAccessException,
- InvocationTargetException, Exception, NoSuchMethodException {
- Method[] ms = obj.getClass().getDeclaredMethods();
- ms = obj.getClass().getMethods();
- for (Method m : ms) {
- // System.out.println(m.getName());
- if (methodName.equals(m.getName()))
- m.invoke(obj, null);
- }
- Method m = obj.getClass().getMethod(methodName, null);
- m.invoke(obj, null);
- }
- static void field(Class clazz) throws Exception {
- Field[] fs = clazz.getDeclaredFields();
- //fs = clazz.getFields();
- for (Field f : fs)
- System.out.println(f.getName());
- }
- static void annon(Class clazz) throws Exception {
- Annotation[] as = clazz.getAnnotations();
- }
例:
packagecom.juziku;
publicclassReflectPoint {
/**
* 私有变量x
*/
privateintx;
publicinty;
publicString str1 = "abc";
publicString str2 = "123";
publicReflectPoint(intx, inty) {
super();
this.x = x;
this.y = y;
}
}
packagecom.juziku;
importjava.lang.reflect.Field;
/**
* 反射例子
* @author sunlightcs
* 2011-3-5
* http://hi.juziku.com/sunlightcs/
*/
publicclassReflectTest {
publicstaticvoidmain(String[] args) throwsException{
ReflectPoint reflectPoint = newReflectPoint(3,5);
/**
* 通过反射,获得ReflectPoint属性的值
*
* reflectPoint.getClass()表示获得ReflectPoint的字节码,
* 当然也可以写成Field fieldY = ReflectPoint.class
* 或Class.forName("com.juziku.ReflectPoint");
* 获得某个类的字节码的方法有3种:
* ReflectPoint.class (类名.class)
* reflectPoint.getClass() (对象名.getClass())
* Class.forName("com.juziku.ReflectPoint") (Class.forName(类名的路径))
*
* reflectPoint.getClass().getField("y")表示获得ReflectPoint里y属性的位置
*/
Field fieldY = reflectPoint.getClass().getField("y");
/**
* fieldY.get(reflectPoint)表示获得reflectPoint对象里,y位置对应属性的值
*/
Object y = (Object)fieldY.get(reflectPoint);
/**
* 打印reflectPoint对象里,属于为y的值
*/
System.out.println(y);
/**
* 获得x属性的值,由于x属于为private类型,
* 所以要通过getDeclaredField方法查询x所在的位置
*/
Field fieldX = reflectPoint.getClass().getDeclaredField("x");
/**
* 由于x属于为private类型,也要设置成可访问,不然获取不到值
*/
fieldX.setAccessible(true);
Object x = (Object)fieldX.get(reflectPoint);
System.out.println(x);
/**
* 获得类型为String的所有值
*/
getStringValues(reflectPoint);
/**
* 修改str1属性的值
*/
onchageValue(reflectPoint);
}
privatestaticvoidgetStringValues(Object obj) throwsException{
/**
* 获得所有属性的位置
*/
Field[] fields = obj.getClass().getFields();
for(Field field : fields){
/**
* 这里建议用==,不建议用equals
* 如果field属性是String类型的话,那么跟String的字节码是同一份
*/
if(field.getType() == String.class){
String str = (String)field.get(obj);
System.out.println(str);
}
}
}
privatestaticvoidonchageValue(Object obj) throwsException{
Field field = obj.getClass().getField("str1");
System.out.println(field.get(obj));
field.set(obj, "22");
System.out.println(field.get(obj));
}
}
本文深入探讨了Java反射机制,详细介绍了如何通过反射动态获取类的属性、方法和构造函数,以及如何利用反射进行对象实例化、方法调用和属性访问。通过具体示例,展示了反射在实际开发中的应用场景。




1496

被折叠的 条评论
为什么被折叠?



