• 方法一:直接通过一个class的静态变量class获取:

    1. Class cls = String.class;
  • 方法二:如果我们有一个实例变量,可以通过该实例变量提供的getClass()方法获取:

    1. String s = "Hello";
    2. Class cls = s.getClass();
  • 方法三:如果知道一个class的完整类名,可以通过静态方法Class.forName()获取:

    1. Class cls = Class.forName("java.lang.String");

    访问字段

    ```java public class Reflection02 { public static void Run() throws NoSuchFieldException {

    1. Class<Student> stdClass = Student.class;
    2. //获取public字段"score":
    3. System.out.println(stdClass.getField("score"));
    4. //获取继承的public字段"name"
    5. System.out.println(stdClass.getField("name"));
    6. //获取继承的private字段"grade"
    7. System.out.println(stdClass.getDeclaredField("grade"));

    } }

class Student extends Person { public int score; private int grade; }

class Person { public String name; }

  1. <a name="HbEsk"></a>
  2. ### 修改字段
  3. ```java
  4. public class Main {
  5. public static void main(String[] args) throws Exception {
  6. Person p = new Person("Xiao Ming");
  7. System.out.println(p.getName()); // "Xiao Ming"
  8. Class c = p.getClass();
  9. Field f = c.getDeclaredField("name");
  10. f.setAccessible(true);
  11. f.set(p, "Xiao Hong");
  12. System.out.println(p.getName()); // "Xiao Hong"
  13. }
  14. }
  15. class Person {
  16. private String name;
  17. public Person(String name) {
  18. this.name = name;
  19. }
  20. public String getName() {
  21. return this.name;
  22. }
  23. }

调用方法

  1. import java.lang.reflect.Method;
  2. public class Main {
  3. public static void main(String[] args) throws NoSuchMethodException {
  4. Class<Student> stdClass = Student.class;
  5. //获取public方法的getScore,参数为String
  6. Method getScore = stdClass.getMethod("getScore", String.class);
  7. System.out.println(getScore);
  8. //获取继承的public方法getName 无参数
  9. System.out.println(stdClass.getMethod("getName"));
  10. //获取继承的private方法getGrade参数为int
  11. System.out.println(stdClass.getDeclaredMethod("getGrade", int.class));
  12. }
  13. }
  14. class Student extends Person {
  15. public int getScore(String type) {
  16. return 99;
  17. }
  18. private int getGrade(int year) {
  19. return 1;
  20. }
  21. }
  22. class Person {
  23. public String getName() {
  24. return "Person";
  25. }
  26. }
  1. public class Main {
  2. public static void main(String[] args) throws Exception {
  3. // String对象:
  4. String s = "Hello world";
  5. // 获取String substring(int)方法,参数为int:
  6. Method m = String.class.getMethod("substring", int.class);
  7. // 在s对象上调用该方法并获取结果:
  8. String r = (String) m.invoke(s, 6);
  9. // 打印调用结果:
  10. System.out.println(r);
  11. }
  12. }
  1. /*
  2. 调用静态方法
  3. */
  4. public class Main {
  5. public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
  6. Method m = Integer.class.getMethod("parseInt", String.class);
  7. Integer n = (Integer) m.invoke(null, "12345");
  8. System.out.println(n);
  9. }
  10. }
  1. import java.lang.reflect.InvocationTargetException;
  2. import java.lang.reflect.Method;
  3. /*
  4. 调用非public方法
  5. */
  6. public class Main {
  7. public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
  8. Person p = new Person();
  9. Method m = p.getClass().getDeclaredMethod("setName", String.class);
  10. m.setAccessible(true);
  11. m.invoke(p, "Bob");
  12. System.out.println(p.name);
  13. }
  14. }
  15. class Person {
  16. String name;
  17. private void setName(String name) {
  18. this.name = name;
  19. }
  20. }
  1. import java.lang.reflect.Constructor;
  2. import java.lang.reflect.InvocationTargetException;
  3. /*
  4. 调用构造方法
  5. */
  6. public class Main {
  7. public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
  8. //获取构造方法
  9. Constructor<Integer> cons1 = Integer.class.getConstructor(int.class);
  10. //调用构造方法
  11. Integer n1 = cons1.newInstance(123);
  12. System.out.println(n1);
  13. //获取构造方法
  14. Constructor<Integer> cons2 = Integer.class.getConstructor(String.class);
  15. Integer n2 = cons2.newInstance("456");
  16. System.out.println(n2);
  17. }
  18. }
  1. /*
  2. 获取父类的class
  3. */
  4. public class Main {
  5. public static void main(String[] args) {
  6. Class<Integer> i = Integer.class;
  7. Class<? super Integer> n = i.getSuperclass();
  8. System.out.println(n);
  9. Class<? super Integer> o = n.getSuperclass();
  10. System.out.println(o);
  11. System.out.println(o.getSuperclass());
  12. }
  13. }
  1. /*
  2. 获取interface接口
  3. */
  4. public class Main {
  5. public static void main(String[] args) {
  6. Class<Integer> integerClass = Integer.class;
  7. Class<?>[] is = integerClass.getInterfaces();
  8. for (Class<?> i : is) {
  9. System.out.println(i);
  10. }
  11. }
  12. }

动态代理

  1. import java.lang.reflect.InvocationHandler;
  2. import java.lang.reflect.Method;
  3. import java.lang.reflect.Proxy;
  4. public class Main {
  5. public static void main(String[] args) {
  6. InvocationHandler handler = new InvocationHandler() {
  7. @Override
  8. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  9. System.out.println(method);
  10. if (method.getName().equals("morning")) {
  11. System.out.println("Good morning, " + args[0]);
  12. }
  13. return null;
  14. }
  15. };
  16. Hello hello = (Hello) Proxy.newProxyInstance(
  17. Hello.class.getClassLoader(), // 传入ClassLoader
  18. new Class[]{Hello.class}, // 传入要实现的接口
  19. handler); // 传入处理调用方法的InvocationHandler
  20. hello.morning("Bob");
  21. }
  22. }
  23. interface Hello {
  24. void morning(String name);
  25. }