集册 Java实例教程 通过反射获取字段名称

通过反射获取字段名称

欢马劈雪     最近更新时间:2020-01-02 10:19:05

549
通过反射获取字段名称

import java.lang.reflect.Field;

import java.util.List;


public class FieldSpy<T> {
/**
NowJava.com
**/

    public boolean[][] b = { { false, false }, { true, true } };

    public String name = "Alice";

    public List<Integer> list;

    public T val;


    public static void main(String... args) {

        try {

            Class<?> c = Class.forName(args[0]);

            Field f = c.getField(args[1]);

            System.out.format("Type: %s%n", f.getType());

            System.out.format("GenericType: %s%n", f.getGenericType());

            /*来自 
             时 代 J     a    v  a - nowjava.com*/

            // production code should handle these exceptions more gracefully

        } catch (ClassNotFoundException x) {

            x.printStackTrace();

        } catch (NoSuchFieldException x) {

            x.printStackTrace();

        }

    }

}


展开阅读全文