提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
获取所有字段包括超级类
//package com.nowjava;/*来自 时代Java公众号 - nowjava.com*/ import java.lang.reflect.Field; 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(getAllFieldsIncludeSuperClass(clazz)); } public static List<Field> getAllFieldsIncludeSuperClass( final Class<?> clazz) { List<Field> lf = new ArrayList<Field>(); /** N o w J a v a . c o m 提供 **/ for (Class<?> superClass = clazz; superClass != Object.class; superClass = superClass .getSuperclass()) { Field[] f = superClass.g