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

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");
    }
}

你可能感兴趣的:(编写一个类,增加一个实例方法 打印字符串 用反射)