zoukankan      html  css  js  c++  java
  • 6、获取Class中的方法

    6、获取Class中的方法

    6.1 getMethods() 获取的都是共有的方法(包括父类)

    • 返回包含一个数组 方法对象反射由此表示的类或接口的所有公共方法 类对象,包括那些由类或接口和那些从超类和超接口继承的声明。
    • 如果此类对象表示具有多个具有相同名称和参数类型但具有不同返回类型的公共方法的类型,则返回的数组对于每个此类方法都有一个方法对象。
    • 如果此类对象表示与类初始化方法的类型 ,则返回的阵列不具有相应的方法对象。
    • 如果此类对象表示一个数组类型,则返回的阵列具有方法对于每个由阵列类型从继承的公共方法对象Object 。 它不包含方法对象clone() 。
    • 如果此类对象表示一个接口,那么返回的数组不包含任何隐含声明的方法,从Object 。因此,如果在此接口或其任何超级接口中没有显式声明方法,则返回的数组的长度为0.(注意,表示类的类对象始终具有从Object公共方法)。
    • 如果此类对象表示原始类型或空值,则返回的数组的长度为0。
    • 由此类对象表示的类或接口的超级接口中声明的静态方法不被视为类或接口的成员。
    • 返回的数组中的元素不会被排序,并且不是以任何特定的顺序。
    /**
     * 获取指定Class中的公共函数
     */
    @Test
    public void getMethodDemo() throws Exception {
        Class clazz = Class.forName("com.hw.bean.Person");
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
    }
    

    运行:

    6.2 getDeclaredMethods() 获取本类中的所有的方法(包含私有的)

    • 返回包含一个数组方法对象反射的类或接口的所有声明的方法,通过此表示类对象,包括公共,保护,默认(包)访问和私有方法,但不包括继承的方法。
    • 如果此类对象表示具有多个具有相同名称和参数类型但具有不同返回类型的声明方法的类型,则返回的数组对于每个此类方法都有一个方法对象。
    • 如果此类对象表示具有类初始化方法的类型 ,则返回的阵列不具有相应的方法对象。
    • 如果此类对象表示没有声明方法的类或接口,则返回的数组的长度为0。
    • 如果这个类对象表示一个数组类型,一个基本类型,或者是void,则返回的数组的长度为0。
    • 返回的数组中的元素不会被排序,并且不是以任何特定的顺序。
    @Test
    public void getMethodDemo_2() throws Exception {
        Class clazz = Class.forName("com.hw.bean.Person");
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
    }
    

    运行:

    6.3 getMethod(String name, 类<?>... parameterTypes) 获取单个方法

    • 返回一个方法对象,它反映此表示的类或接口的指定公共成员方法类对象。
    • name参数是一个String它指定了所需方法的简单名称。
    • parameterTypes参数是以声明顺序标识方法的形式参数类型的类对象的数组。
    • 如果parameterTypes是null ,它被视为一个空数组。
    6.3.1 方法无参 方式一:使用无参构造器(默认) method.invoke(o, null)
    @Test
    public void getMethodDemo_3() throws Exception {
        Class clazz = Class.forName("com.hw.bean.Person");
        // 获取空参的一般方法
        Method method = clazz.getMethod("show", null);
        Object o = clazz.newInstance();
        method.invoke(o, null);
        System.out.println(method);
    }
    

    运行:

    6.3.2 方法无参 方式二:使用有参构造器 method.invoke(o, null)
    @Test
    public void getMethodDemo_4() throws Exception {
        Class clazz = Class.forName("com.hw.bean.Person");
        // 获取空参的一般方法
        Method method = clazz.getMethod("show", null);
        // 获取带参构造器
        Constructor constructor = clazz.getConstructor(String.class, int.class);
        Object o = constructor.newInstance("小明", 12);
        method.invoke(o, null);
        System.out.println(method);
    }
    

    运行:

    6.3.3 方法有参
    @Test
    public void getMethodDemo_5() throws Exception {
        Class clazz = Class.forName("com.hw.bean.Person");
        Method method = clazz.getMethod("paramMethod", String.class, int.class);
        // 获取带参构造器
        Constructor constructor = clazz.getConstructor(String.class, int.class);
        Object o = constructor.newInstance("小明", 12);
        method.invoke(o, "张三", 18);
        System.out.println(method);
    }
    

    运行:


    本章节源码: ReflectDemo4

  • 相关阅读:
    Go 语言机制之逃逸分析
    类型转换和类型断言
    浅析rune数据类型
    Go 文件操作(创建、打开、读、写)
    字符编码笔记:ASCII,Unicode 和 UTF-8
    cmd.exe启动参数详解
    linux下.so、.ko、.a的区别
    Python 和C#的交互
    Innodb表压缩过程中遇到的坑(innodb_file_format)
    更改mysql的加密方式和密码策略
  • 原文地址:https://www.cnblogs.com/Grand-Jon/p/10041982.html
Copyright © 2011-2022 走看看