提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用反射按名称查找类中的字段
//package com.nowjava; import java.lang.reflect.Field;/* 来自 时 代 J a v a - nowjava.com*/ public class Main { public static void main(String[] argv) throws Exception { Class clazz = String.class; String name = "nowjava.com"; System.out.println(findField(clazz, name)); } public static Field findField(Class<?> clazz, String name) { for (Class<?> c = clazz; c != null && !Object.class.equals(c); c = c .getSuperclass()) { Field[] fields = c.getDeclaredFields(); for (Field field : fields) { if (name.equals