反射
1>字节码文件的获取:
Class.forName("the nama of class");
object name.getClass();
Class name.class;
2>构造函数的反射
Constructor<String> constr = String.class.getConstructor(StringBuffer.class);
//.getConstructors<>();用于获得所有的构造方法。
String str = constr.newInstance(new StringBuffer("abc"));
3>成员变量的反射
public void setProperty(Object obj,String propertyName,obj Value){
Class clazz = obj.getClass();
Field field = clazz.getField(propertyName);
field.set(obj,value);
|
|