提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用反射按名称从类中查找方法
//package com.nowjava; import java.lang.reflect.Method; /* nowjava.com - 时代Java 提供 */ import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] argv) throws Exception { Class clazz = String.class; String name = "nowjava.com"; /* *来 自 nowjava.com */ System.out.println(findMethod(clazz, name)); } public static List<Method> findMethod(Class<?> clazz, String name) { List<Method> ret = new ArrayList<Method>(); for (Class<?> c = clazz; c != null; c = c.getSuperclass()) { Method[] methods = (c.isInterface() ? c.getMethods() : c .getDeclaredMethods());