/**
* @author 万星明
* @version 创建时间:2018年10月26日 下午12:01:36
* 类说明
*/
public class 反射调用对象方法 {
public static void main(String[] args) throws Exception {
//创建反射对象
Class<?> clazz = Class.forName("反射与URL.Student");
//通过反射对象创建实例对象
Object ob1 = clazz.newInstance();
//通过反射对象获得方法
Method show=clazz.getDeclaredMethod("show2",String.class,Integer.class);
//设置方法的访问权限
show.setAccessible(true);
System.out.println("方法的访问修饰符:"+show.getModifiers());
System.out.println("方法的返回值:"+show.getReturnType());
//调用方法
show.invoke(ob1,"万星明",23);
}
}
反射:通过反射调用对象方法
最新推荐文章于 2024-05-27 20:55:58 发布