Constructor<T> Class.getConstructor(Class<?>... parameterTypes): 返回一个 Constructor 对象,它反映此 Class 对象所表示的类的指定公共构造方法
T Constructor.newInstance(Object... initargs):
Field类:代表某个类中的成员变量
例子:
Class ReflectPiont
{
public int y;
private int x;
ReflectPoint(int y,int x){
This.y=y;
This.x=x;
}
}
Class FieldDemo
{
public static void main(String[] args){
ReflectPoint rp=new ReflectPoint();
Field y=rp.getClass().getField(“y”);
System.out.println(y.get(rp));
Field x=rp.getClass().getField(“x”)
x.setAccessible(true);
System.out.println(x.get(rp));
}
}
Field Class.getField(String name): 返回一个 Field 对象,它反映此 Class 对象所表示的类或接口的指定公共成员字段。
Object Field.get(Object obj): 返回指定对象上此 Field 表示的字段的值。
void Field.set(Object obj, Object value): 将指定对象变量上此 Field 对象表示的字段设置为指定的新值。