浏览 2015 次
锁定老帖子 主题:java中类的加载Demo
精华帖 (0) :: 良好帖 (0) :: 新手帖 (10) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-06-10
package com.cash.utils; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import com.cash.server.ICommonService; public class ClassUtil { public static String exec(String classString, Object[] objects) { Class<?> convertorClass; String result = null; if (null == classString || "".equalsIgnoreCase(classString)) { return null; } try { convertorClass = Class.forName(classString); ICommonService convertor = (ICommonService) convertorClass .newInstance(); Method istance = convertorClass.getMethod("run", java.lang.String.class,java.lang.String.class,java.lang.String.class); result = (String) istance.invoke(convertor, objects); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } return result; } } package com.cash.server; public interface ICommonService { public String run(String para1, String para2, String para3); } package com.cash.server; public class AService implements ICommonService { @Override public String run(String para1, String para2, String para3) { return para1 + ":" + para2 + ":" + para3; } } package com.cash.server; import com.cash.utils.ClassUtil; public class Main { public static void main(String[] args) { Object[] objects = {"I Am "," The " , " Best"}; String result = ClassUtil.exec("com.cash.server.AService", objects); System.out.println(result); } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-06-10
不怕新手贴啊
|
|
返回顶楼 | |
发表时间:2011-06-10
freish 写道 不怕新手贴啊 木有关系 |
|
返回顶楼 | |
发表时间:2011-06-10
楼主在学习 反射?还是?
|
|
返回顶楼 | |