好像需要用到类似struts 中的配置文件

解决方案 »

  1.   

    这是很简单的反射功能的使用了,拜托lz先去google一下,马上就有结果。要学东西最好自己能主动去找一下,只会用是不够的。
    知其用,了其然,研其究,发其思
      

  2.   

    楼上的说得好轻松,网上的反射都只能动态的执行方法,反射出其中的某些东西,对于怎么样动态的把类生成出来,并且执行其中的方法,这很难的,我问我们项目经理,他搞了六年JAVA了,他都没办法
      

  3.   

    难就难在Class.forName("Test1").newInstance(); 反回的是Object 类型,你这时候要强制转换成Test1但是Test1 都是动态的,这时候怎么办。
      

  4.   

    我刚写完就看到你要用。真巧
    /**
     * 类解析器
     * @author sl 
     */
    public class ClassParseAdp {
    /**
     * <p>执行方法并返回方法的返回值</p>
     * @param className 类名
     * @param methodName 方法名
     * @param cs 方法参数列表
     * @return 方法的返回值
     */
    public static Object execute(String className, String methodName, Class[] cs){
    Object reObj = null;
    Object obj = null;
    Method method = null;

    try{
    obj = Class.forName(className).newInstance();
    method = obj.getClass().getMethod(methodName, cs);
    reObj = method.invoke(obj, getObjects(cs));
    }catch(Exception e){
    throw new RuntimeException(e);
    }
    return reObj;
    }

    /**
     * 生成Class组的类实例
     * @param cs
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    private static Object[] getObjects(Class[] cs) throws InstantiationException, IllegalAccessException{
    Object[] objs = new Object[null == cs? 0: cs.length];
    for(int i = 0; i < objs.length; i++){
    objs[i] = cs[i].newInstance();
    }

    return objs;
    }

    /**
     * 同上
     */
    public static Object execute(Class c, Method method){
    return execute(c.getName(), method.getName());
    }

    /**
     * <p>执行</p>
     * 根据className实例化一个类,并运行为methodName的方法
     * @param className 类名
     * @param methodName 方法名
     * @return 方法的返回值<br>
     * 注:如果此方法无返回值,则返回null
     */
    public static Object execute(String className, String methodName){
    return execute(className, methodName, null);
    }
    }
      

  5.   

    楼上的同志,谢谢你的代码,不过经过测试,你的代码有问题,报错
    Exception in thread "main" java.lang.RuntimeException: java.lang.NoSuchMethodException: Action.click()
    at ClassParseAdp.execute(ClassParseAdp.java:25)
    at ClassParseAdp.execute(ClassParseAdp.java:62)
    at Test.main(Test.java:22)
    Caused by: java.lang.NoSuchMethodException: Action.click()
    at java.lang.Class.getMethod(Class.java:1581)
    at ClassParseAdp.execute(ClassParseAdp.java:22)
    ... 2 more
      

  6.   

    原来我的代码是这样的,就不会报错
    Class[] partypes = new Class[1];
    partypes[0] = Event.class;Event[] argtypes = new Event[1];
    argtypes[0] = new Event();

    try {
    Action action = (Action) Class.forName(className).newInstance();

    Class.forName(className).getMethod(methodName,partypes).invoke(action,argtypes);} catch (Exception ex) {
    ex.printStackTrace();
    }
      

  7.   

    不好意思,我一直没用过那个带参数的方法,原本以为都可以用.
    如下改进:
    getObjects不再需要,新加getClasses方法
    大体试了一下,应该可以了.public static Object execute(String className, String methodName, Object[] objs){
    Object reObj = null;
    Object obj = null;
    Method method = null;

    try{
    obj = Class.forName(className).newInstance();
    method = obj.getClass().getMethod(methodName, getClasses(objs));
    reObj = method.invoke(obj, objs);
    }catch(Exception e){
    throw new RuntimeException(e);
    }
    return reObj;
    }
    private static Class[] getClasses(Object[] objs){
    Class[] cs = new Class[null == objs? 0: objs.length];
    for(int i = 0; i < cs.length; i++){
    cs[i] = objs[i].getClass();
    }
    return cs;
    }
      

  8.   

    Exception in thread "main" java.lang.RuntimeException: java.lang.NoSuchMethodException: Action.click()
    at ClassParseAdp.execute(ClassParseAdp.java:33)
    at ClassParseAdp.execute(ClassParseAdp.java:67)
    at Test.main(Test.java:22)
    Caused by: java.lang.NoSuchMethodException: Action.click()
    at java.lang.Class.getMethod(Class.java:1581)
    at ClassParseAdp.execute(ClassParseAdp.java:30)
    ... 2 more