集册 Java实例教程 返回一个Method对象数组,该对象反映该类声明的所有方法,包括超类的方法。

返回一个Method对象数组,该对象反映该类声明的所有方法,包括超类的方法。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

426
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
返回一个Method对象数组,该对象反映该类声明的所有方法,包括超类的方法。


//package com.nowjava;
/*from 时   代    Java - nowjava.com*/

import java.lang.reflect.Method;


import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;


public class Main {

    public static void main(String[] argv) throws Exception {

        Class clazz = String.class;

        System.out.println(getDeclaredMethodsInLineage(clazz));

    }


    /**

     * Returns an array of {@code Method} objects reflecting all the methods declared by the class including the methods

     * of the superclasses as well.

     * <p/>

     * This method matches the same kinds of methods that are returned by {@link Class#getDeclaredMethods()} with the

     * exception that it includes methods inherited from the superclasses. This means public, protected, default

     * (package) access, and private methods AND methods of those types inherited from the superclasses.

     * <p/>

     * The returned list is partially ordered. Results from more derived classes show up earlier in the list. Within the

     * set of fields for the same class, however, the order is undefined.

     *

     * @param clazz the class

     * @return the array of {@code Method} objects representing all the declared methods of this class

     * @see Class#getDeclaredMethods() for more details

     */

    public static List<Method> getDeclaredMethodsInLineage(Class<?> clazz) {

        List<Method> methods = new ArrayList<Method>(Arrays.asList(clazz

                .getDeclaredMethods()));

        Class<?
展开阅读全文