提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
获取所有的二传手,包括超级班级
//package com.nowjava; /** 来 自 时 代 Java - nowjava.com **/ import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] argv) throws Exception { Class clazz = String.class; System.out.println(getAllSettersIncludeSuperClass(clazz)); } private static final String SETMETHOD_PREFIX = "set"; /* n o w j a v a . c o m 提 供 */ public static List<Method> getAllSettersIncludeSuperClass( final Class<?> clazz) { List<Method> ls = new ArrayList<Method>(); for (Class<?> superClass = clazz; superClass != null && superClass != Object.class; superClass = superClass .getSuperclass()) { Method[] methods = superClass.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { Method m = methods[i]; if (m.getName().startsWith(SETMETHOD_PREFI