代码:

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ObjectUtil {

	/**
	 * 利用Java反射根据类的名称获取属性信息和父类的属性信息
	 * @param className
	 * @return
	 * @throws ClassNotFoundException
	 */
	public static List<Field> getFiledsInfo(String className) throws ClassNotFoundException {
		List<Field> list = new ArrayList<>();
		Class<?> clazz = Class.forName(className);
		Field[] fields = clazz.getDeclaredFields();
		list.addAll(Arrays.asList(fields));
		Class<?> superClazz = clazz.getSuperclass();
		if (superClazz != null) {
			Field[] superFields = superClazz.getDeclaredFields();
			list.addAll(Arrays.asList(superFields));
		}
		return list;
	}
	
	public static void main(String[] args) {
		try {
			List<Field> fields = getFiledsInfo(Student.class.getName());
			for (Field field : fields) {
				// 设置访问对象权限,保证对私有属性的访问
				field.setAccessible(true);
				System.out.println(field.getName());
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 运行结果:

java 获取父目录 java获取父类的属性_java