前言
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制。
应用场景
Android中最熟悉的反射,莫过于Json数据的转换,例如网络数据,数据库数据和类之间的相互转化。使用反射机制可以直接创建对象,方便代码管理。
相关类
. Class
. Method
. Field
. Constructor
示例
- 反射实体类
public class GloryOfKings {
private String role; private String skills; private String explain;
public GloryOfKings() { }
private GloryOfKings(String role, String skills) { this.role = role; this.skills = skills; }
public String getRole() { return role; }
public void setRole(String role) { this.role = role; }
public String getSkills() { return skills; }
public void setSkills(String skills) { this.skills = skills; }
public void setExplain(String explain) { this.explain = explain; }
public String getExplain() { return explain; }
private String week(int flag) { String string = ""; switch (flag) { case FlagMethod.monday: string = "天气晴朗"; break; case FlagMethod.tuesday: string = "多云"; break; case FlagMethod.Wednesday: string = "微风"; break; case FlagMethod.Thursday: string = "阵风6级"; break; case FlagMethod.Friday: string = "中雨"; break; case FlagMethod.Saturday: string = "大雨"; break; case FlagMethod.Sunday: string = "晴朗"; break; default: string = "-.-"; }
return string; } }
|
- 逻辑封装类
public class Reflect {
public static void newInstance(int flag) { String outMessage = ""; try { switch (flag) { case FlagSource.newInstanceOne: Class<GloryOfKings> cls = GloryOfKings.class; GloryOfKings gloryOfKings = cls.newInstance(); gloryOfKings.setExplain("创建对象方式- GloryOfKings.class"); gloryOfKings.setRole("李元芳"); gloryOfKings.setSkills("利刃风暴"); outMessage = gloryOfKings.toString(); break; case FlagSource.newInstanceTwo: Class<? extends GloryOfKings> cls1 = new GloryOfKings().getClass(); GloryOfKings gloryOfKings1 = cls1.newInstance(); gloryOfKings1.setExplain("创建对象方式- new GloryOfKings().getClass()"); gloryOfKings1.setRole("后羿"); gloryOfKings1.setSkills("灼日之矢”"); outMessage = gloryOfKings1.toString(); break; case FlagSource.newInstanceThree: Class<?> cls2 = Class.forName("com.linked.business.reflect.GloryOfKings"); GloryOfKings gloryOfKings2 = (GloryOfKings) cls2.newInstance(); gloryOfKings2.setExplain("创建对象方式- Class.forName()"); gloryOfKings2.setRole("诸葛亮"); gloryOfKings2.setSkills("时空穿梭”"); outMessage = gloryOfKings2.toString(); break; } } catch (Exception e) { e.printStackTrace(); } System.out.println(outMessage); }
public static void construction() { try { Class<?> cls = Class.forName("com.linked.business.reflect.GloryOfKings"); Constructor<?> constructor = cls.getDeclaredConstructor(String.class, String.class); constructor.setAccessible(true); Object obj = constructor.newInstance(" 反射-设置私有构造方法:名称", " 反射-获取私有构造方法:福利"); GloryOfKings go = (GloryOfKings) obj; System.out.println("反射-获取私有构造方法" + go); } catch (Exception e) { e.printStackTrace(); } }
public static void reflectDeclaredAttribute() { try { Class<?> cls = Class.forName("com.linked.business.reflect.GloryOfKings"); Object obj = cls.newInstance(); Field field = cls.getDeclaredField("TAG"); field.setAccessible(true); String str = (String) field.get(obj); System.out.println("反射-获取私有属性" + str); } catch (Exception e) { e.printStackTrace(); } }
public static void privateMethod() { try { Class<?> cls = Class.forName("com.linked.business.reflect.GloryOfKings"); Method method = cls.getDeclaredMethod("week", int.class); method.setAccessible(true); Object obj = cls.newInstance(); String str = (String) method.invoke(obj, FlagMethod.monday); System.out.println(str); } catch (Exception e) { e.printStackTrace(); } } }
|
- 测试类
public class ReflectTest { public static void main(String[] args) { try { Reflect.newInstance(FlagSource.newInstanceTwo); Reflect.attribute(); Reflect.construction(); Reflect.privateMethod(); } catch (Exception e) { e.printStackTrace(); } } }
|