论坛首页 Java企业应用论坛

java中类的加载Demo

浏览 2015 次
精华帖 (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);
    }

}
 
 

 

   发表时间:2011-06-10  
不怕新手贴啊
0 请登录后投票
   发表时间:2011-06-10  
freish 写道
不怕新手贴啊

木有关系
0 请登录后投票
   发表时间:2011-06-10  
楼主在学习 反射?还是?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics