提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
返回一个Field对象数组,其中反映了该类声明的所有字段,包括超类的字段。
//package com.nowjava;/** from 时代Java公众号 - nowjava.com**/ import java.lang.reflect.Field; 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(getDeclaredFieldsInLineage(clazz)); } /** 来自 n o w j a v a . c o m - 时 代 Java**/ /** * Returns an array of {@code Field} objects reflecting all the fields declared by the class including the fields of * the superclasses as well. * <p/> * This method matches the same kinds of fields that are returned by {@link Class#getDeclaredFields()} with the * exception that it includes fields inherited from the superclasses. This means public, protected, default * (package) access, and private fields AND fields 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 Field} objects representing all the declared fields of this class * @see Class#getDeclaredFields() for more details */ public static List<Field> getDeclaredFieldsInLineage(Class<?> clazz) { List<Field> fields = new ArrayList<Field>(Arrays.asList(clazz