package com.reflect.demo; public class ReflectDemo { /* *Java反射机制是在运行状态中,对于任意一个类(class文件),都能知道这个类所有属性和方法 * */ public static void main(String[] args) { System.out.println(createClassDemo_3().getName()); } //利用Object类的getClass方法获得 public static Class createClassDemo_1() { Person p =new Person(); Class clazz=p.getClass(); return clazz; } //利用属性.Class得到 public static Class createClassDemo_2() { Class clazz=Person.class; return clazz; } public static Class createClassDemo_3() { String className="com.reflect.demo.Person"; Class clazz=null; try { clazz=Class.forName(className); }catch(ClassNotFoundException e) { e.printStackTrace(); } return clazz; } }
package com.reflect.demo; public class Person { private int age; private String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Person(int age, String name) { super(); this.age = age; this.name = name; System.out.println("Person param run"); } public Person() { super(); System.out.println("Person run"); } public void show() { System.out.println(name+"...show run..."+age); } private void method() { System.out.println("method run"); } public void paramMethod(String str,int num) { System.out.println("paramMethod run"+str+":"+num); } public static void staticMethod() { System.out.println("staticMethod run..."); } }
package com.reflect.demo; import java.lang.reflect.Constructor; public class ReflectDemo2 { public static void main(String[] args) { constructorDemo_2(); } //得到无参的构造函数 public static void constructorDemo_1() { String className="com.reflect.demo.Person"; try { Class clazz= Class.forName(className); Constructor cons= clazz.getConstructor(); Person p=(Person)cons.newInstance(); }catch(Exception e) { e.printStackTrace(); } } //得到有参的构造函数。 public static void constructorDemo_2() { String className="com.reflect.demo.Person"; try { Class clazz= Class.forName(className); Constructor cons= clazz.getConstructor(int.class,String.class); Person p =(Person)cons.newInstance(21,"zhangsan"); System.out.println(p.getAge()); }catch(Exception e) { e.printStackTrace(); } } }
package com.reflect.demo; import java.lang.reflect.Field; public class ReflectDemo3 { public static void main(String[] args) { fieldDemo_1(); } //Filed类存在两种得到字段的方法,一个是得到所有的非私有的字段。另外一个是 //得到所有的字段(包括私有的),但是此时,只提供了对字段的“跟踪”,却不能访问该字段,若想访问,需要暴力破解 public static void fieldDemo_1() { String className = "com.reflect.demo.Person"; try { Class clazz=Class.forName(className); Field f=clazz.getDeclaredField("age"); Person p=(Person)clazz.newInstance(); f.setAccessible(true);//暴力破解,让虚拟机不检查该字段的安全性 f.set(p, 22);//修改该对象对应的字段值。 int age =f.getInt(p);//访问字段 System.out.println(age); }catch(Exception e) { e.printStackTrace(); } } }
package com.reflect.demo; import java.lang.reflect.Constructor; import java.lang.reflect.Method; /* * 获取指定类中的方法。 * */ public class ReflectDemo4 { public static void main(String[] args) { methodDemo_2(); } //得到无参的方法,并回调该对象代表的方法。 public static void methodDemo_1() { String className="com.reflect.demo.Person"; try { Class clazz =Class.forName(className); Method m=clazz.getMethod("show", null); Person p =(Person)clazz.newInstance(); m.invoke(p, null); }catch(Exception e) { e.printStackTrace(); } } public static void methodDemo_2() { String className="com.reflect.demo.Person"; try { Class clazz =Class.forName(className); Method m=clazz.getMethod("paramMethod", String.class,int.class); Person p =(Person)clazz.newInstance(); m.invoke(p, "wangwu",22); }catch(Exception e) { e.printStackTrace(); } } }
原文:http://blog.csdn.net/yuan514168845/article/details/18626181