import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//通过反射获取成员方法并使用
public class ReflectDemo5 {
public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
Class<Student> studentClass = Student.class;
//获取公共方法
// Method[] methods = studentClass.getMethods();
// for (Method method : methods) {
// System.out.println(method);
// }
// Method[] declaredMethods = studentClass.getDeclaredMethods();
// for (Method declaredMethod : declaredMethods) {
// System.out.println(declaredMethod);
// }
//获取单个方法并使用
Student student = studentClass.newInstance();
//获取各种方法
Method method1 = studentClass.getMethod("method1");
Method method2 = studentClass.getMethod("method2", String.class);
Method method3 = studentClass.getMethod("method3", String.class, int.class);
Method function = studentClass.getDeclaredMethod("function");
//私有方法,暴力反射
function.setAccessible(true);
//使用反射获取的方法
method1.invoke(student);
method2.invoke(student,"方法2");
System.out.println(method3.invoke(student,"gfx",24));
function.invoke(student);
}
}