编写一个类,增加一个实例方法 打印字符串 用反射
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"); } }
相关文章推荐
- [深入浅出Cocoa]iOS网络编程之NSStream
- Cocos2d动作:移动、变形、闪烁
- bzoj 4524 [Cqoi2016]伪光滑数
- Rust 1.7.0 语法基础 sep_token 和 non_special_token
- Spring中bean的scope
- 责任链模式
- Maven配置Spring+SpringMVC+MyBatis(3.2.2)Pom
- (1) 写一个 Properties 格式的配置文件,配置类的完整名称。 * (2) 写一个程序,读取这个 Properties 配置文件,获得类的完整名称并加载这个类,用 反射 的方式运行
- 题目:定义一个交通灯枚举,包含红灯、绿灯、黄灯,需要有获得下一个灯的方法, * 例如:红灯获取下一个灯是绿灯,绿灯获取下一个灯是黄灯
- 博弈论——acm
- Java链式方法 连贯接口(fluent interface)
- jquery-validation 使用
- 进程内外的session
- 第六周 项目1 分数类的雏形
- win系统cmd运行Linux命令
- Java I/O(一) NIO概述
- redis简单使用及用JSON字符串的方式解决对象存储问题
- 一个 ArrayList 对象 aList 中存有若干个字符串元素,现欲遍历该 ArrayList 对象,删除其中 所有值为"abc"的字符串元素,请用代码实现。
- 编写程序,循环接收用户从键盘输入多个字符串,直到输入“ end”时循环结束,并将所有 已输入的字符串按字典顺序倒序打印。
- 定义一个文件输入流,调用 read(byte[] b)方法将 exercise.txt 文件中的所有内容打印出来 (byte 数组的大小限制为 5)。