import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class test {
/**
* @param args
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
person p=new person();
p.setName("lyc");
System.out.println(p.getName());
//反射无参方法,new Class[]{}使其为空数组即可;带参数的就不能是空数组了
Method method=p.getClass().getMethod("getName", new Class[]{});
System.out.println((String)method.invoke(p, new Object[]{}));
}
}
class person{
String name="";
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}