黑马程序员技术交流社区

标题: 编写一个类,增加一个实例方法用于打印一条字符串。并... [打印本页]

作者: 闭关修行小和尚    时间: 2016-1-2 23:49
标题: 编写一个类,增加一个实例方法用于打印一条字符串。并...
//对象的打印方法
class Println {
        public void println(String str) {
                System.out.println(str);
        }
}

public class ExamTest13 {
        public static void main(String[] args) throws Exception {
                Class<?> c = Class.forName("com.itheima.Println");// 通过反射的方式获取该类的字节码文件
                Method method = c.getMethod("println", String.class);// 获取字节码对象对应的方法,并指定参数类型。
                method.invoke(c.newInstance(), "黑马程序员训练营我来了");// 运行对象中的方法,并传入参数。
        }
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2