Java Reflect

1, Class

描述类的类

如何获得Class

1)Class.forName("com.test.Test");

2)  Test.class;

3)  test.getClass();

2, Field

1)  class.getField(String paraName); //获取类class名为paraName的属性对象

2)  class.getFields();//获取类class的所有属性对象

3)  field.setAccessible(true); //设置类属性field为可访问。

4)  field.get(Object object);  //获取对象object的field属性值

5)  field.set(Object object, Object value); //给对象object 的filed属性赋值

3, Method

1) class.getMethod(String methodName, Class... paraTypes) //根据方法名称和各个参数类型获取class的这个方法对象

2) class.getMethods();//获取类class的所有方法对象。

3)  method.setAccessible(true);//

4)  method.invoke(Object object, Object... paras); //调用对象object的method方法,并传入参数paras

4, Constructor

1) class.getConstructor(Class... paraTypes)

2) class.getConstructors()

3) constructor.setAccessible(true);//

4) constructor.newInstance(Object paras);

原文地址:https://www.cnblogs.com/zhonghan/p/3604011.html