首页 > 系统服务 > 详细

Eclipse里面有包下面的测试类调用默认包下面的类里面的方法

时间:2016-08-27 21:51:06      阅读:457      评论:0      收藏:0      [点我收藏+]

1、通过Class的静态方法获取.

Class clazz = Class.forName("类名");//将传入的类名所指定的类load到JVM内存中
Method[] methods = clazz.getMethods();//获取该相关类所有的public类型的方法
for (Method method : methods) {
  method.invoke(clazz.newInstance());//执行该对象的无参构造方法来创建该类的实例调用方法
}
Method method = clazz.getMethod("方法名");//根据方法名获取该相关类的public类型的方法
method.invoke(clazz.newInstance());

2、通过类的加载器

ClassLoader classLoader = this.getClass().getClassLoader();//获取当前对象的类装载器
Class clazz1 = classLoader.loadClass("类名");//通过类装载器加载需要的类

Method[] methods = clazz1.getMethods();
for (Method method : methods) {
  method.invoke(clazz1.newInstance());
}
Method method = clazz1.getMethod("方法名");
method.invoke(clazz1.newInstance());

Eclipse里面有包下面的测试类调用默认包下面的类里面的方法

原文:http://www.cnblogs.com/bd195746/p/5813787.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!