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

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());

posted @ 2016-08-27 20:57  快乐留给你  阅读(329)  评论(0编辑  收藏  举报