• 00113_通过反射获取成员方法并使用


    1、在反射机制中,把类中的成员方法使用类Method表示;

    2、通过Class类中提供的方法获取成员方法:

      (1)返回获取一个方法

    public Method getMethod(String name, Class<?>... parameterTypes)
    获取public 修饰的方法
    public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
    获取任意的方法,包含私有的
    参数1: name 要查找的方法名称; 参数2: parameterTypes 该方法的参数类型

      (2)返回获取多个方法

    public Method[] getMethods() 获取本类与父类中所有public 修饰的方法
    public Method[] getDeclaredMethods() 获取本类中所有的方法(包含私有的

      (3)代码演示

     1 package cn.gzdlh_01_Reflect;
     2 
     3 import java.lang.reflect.Method;
     4 
     5 public class MethodDemo {
     6     public static void main(String[] args) throws ClassNotFoundException,
     7             NoSuchMethodException, SecurityException {
     8         // 获取Class对象
     9         Class c = Class.forName("cn.gzdlh_01_Reflect.Person");
    10 
    11         // 获取多个方法
    12         // Method[] methods = c.getMethods();
    13         Method[] methods = c.getDeclaredMethods();
    14         for (Method method : methods) {
    15             System.out.println(method);
    16         }
    17 
    18         System.out.println("-----------------------");
    19         // 获取一个方法:
    20         // public void method1()
    21         Method method = c.getMethod("method1", null);
    22         System.out.println(method);
    23         // public String method4(String name){
    24         method = c.getMethod("method4", String.class);
    25         System.out.println(method);
    26         // 私有方法
    27         // private void method5()
    28         method = c.getDeclaredMethod("method5", null);
    29         System.out.println(method);
    30     }
    31 }

    3、通过反射,创建对象,调用指定的方法

      (1)获取成员方法,步骤如下:

        ①获取Class对象;

        ② 获取构造方法;

        ③通过构造方法,创建对象;

        ④获取指定的方法;

        ⑤执行找到的方法。

    public Object invoke(Object obj,  Object... args) 
    执行指定对象obj中,当前Method对象所代表的方法,方法要传入的参数通过args指定

      (2)代码演示

     1 package cn.gzdlh_01_Reflect;
     2 
     3 import java.lang.reflect.Constructor;
     4 import java.lang.reflect.InvocationTargetException;
     5 import java.lang.reflect.Method;
     6 
     7 public class MethodDemo2 {
     8     public static void main(String[] args) throws ClassNotFoundException,
     9             NoSuchMethodException, SecurityException, InstantiationException,
    10             IllegalAccessException, IllegalArgumentException,
    11             InvocationTargetException {
    12         // 1, 获取Class对象
    13         Class c = Class.forName("cn.gzdlh_01_Reflect.Person");
    14         // 2,获取构造方法
    15         // public Person(String name, int age, String address){
    16         Constructor con = c.getConstructor(String.class, int.class,
    17                 String.class);
    18         // 3,通过构造方法,创建对象
    19         Object obj = con.newInstance("gzdlh", 23, "广州");
    20         // 4,获取指定的方法
    21         // public void method1() 没有返回值没有参数的方法
    22         // Method m1 = c.getMethod("method1", null);
    23 
    24         // public String method4(String name)
    25         Method m4 = c.getMethod("method4", String.class);
    26 
    27         // 5,执行找到的方法
    28         // m1.invoke(obj, null);
    29 
    30         Object result = m4.invoke(obj, "gzdlh");
    31         System.out.println("result = " + result);
    32     }
    33 }

    4、通过反射,创建对象,调用指定的private 方法

      (1)获取私有成员方法,步骤如下:

        ①获取Class对象;

        ②获取构造方法;

        ③通过构造方法,创建对象;

        ④获取指定的方法;

        ⑤开启暴力访问;

        ⑥执行找到的方法。

    public Object invoke(Object obj,  Object... args)
    执行指定对象obj中,当前Method对象所代表的方法,方法要传入的参数通过args指定。

      (2)代码演示

     1 package cn.gzdlh_01_Reflect;
     2 
     3 import java.lang.reflect.Constructor;
     4 import java.lang.reflect.InvocationTargetException;
     5 import java.lang.reflect.Method;
     6 
     7 public class MethodDemo3 {
     8     public static void main(String[] args) throws ClassNotFoundException,
     9             NoSuchMethodException, SecurityException, InstantiationException,
    10             IllegalAccessException, IllegalArgumentException,
    11             InvocationTargetException {
    12         // 1, 获取Class对象
    13         Class c = Class.forName("cn.gzdlh_01_Reflect.Person");
    14         // 2,获取构造方法
    15         // public Person(String name, int age, String address){
    16         Constructor con = c.getConstructor(String.class, int.class,
    17                 String.class);
    18         // 3,通过构造方法,创建对象
    19         Object obj = con.newInstance("gzdlh", 23, "广州");
    20         // 4,获取指定的方法
    21         // private void method5(){
    22         Method m5 = c.getDeclaredMethod("method5", null);
    23         // 5,开启暴力访问
    24         m5.setAccessible(true);
    25         // 6,执行找到的方法
    26         m5.invoke(obj, null);
    27     }
    28 }

    在反射机制中,把类中的成员方法使用类Method表示

  • 相关阅读:
    linux下java调用.so文件的方法1: JNI
    在Eclipse中用SWT设计界面
    转:中文编码杂谈
    使用ObjectInputStream的readObject()方法如何判断读取到多个对象的结尾
    Java log4j详细教程
    java没有条件编译
    HTML参考手册
    javadoc 生成帮助文档时,注意以下几点
    Java中取小数点后两位(四种方法)
    iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(二)
  • 原文地址:https://www.cnblogs.com/gzdlh/p/8159262.html
Copyright © 2020-2023  润新知