您的位置:首页 > 其它

编写一个类,增加一个实例方法 打印字符串 用反射

2016-04-12 23:03 483 查看
package com.heima.test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test9 {

/**编写一个类,增加一个实例方法 打印字符串  用反射
* @param args
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws SecurityException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
Class clazz = Class.forName("com.heima.test.Print");
Constructor con = clazz.getDeclaredConstructor();
Print p = (Print)con.newInstance();
Method me = clazz.getMethod("run", null);
me.invoke(p, null);
}

}
class Print{
public void run(){
System.out.println("1231212");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: