日期:2014-05-20 浏览次数:20728 次
package reflect;
import java.lang.reflect.Method;
public class ReflectTest {
public static void main(String[] args) {
TestReflect ref=new TestReflect();
ref.setLength(1);
ref.setWight(2);
try {
Class<?> obj=Class.forName(ref.getClass().getName());
Object object = obj.newInstance();
Method method=obj.getMethod("getLength");
System.out.println(method.invoke(object));
} catch (Exception e) {
e.printStackTrace();
}
}
}
class TestReflect{
int length;
int wight;
TestReflect(){
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWight() {
return wight;
}
public void setWight(int wight) {
this.wight = wight;
}
}