反射:通过反射调用对象方法

版权声明:版权所有@万星明 https://blog.csdn.net/qq_19533277/article/details/83714004
/** 
* @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);
		
	}
}

猜你喜欢

转载自blog.csdn.net/qq_19533277/article/details/83714004