我刚学会JAVA,请问各位高手怎么在程序中调用动态类?用什么语句实现?

解决方案 »

  1.   

    Class c=Class.forName("foo.myClass");
      

  2.   

    Class c=Class.forName("foo.myClass");
    是说调用的类是foo.myClass吗?
      

  3.   

    哪位高手能写个完整点的程序段?譬如说我要调用一个叫OBS的动态类?
      

  4.   

    import java.lang.reflect.*;
    import java.util.*;public class ClassUtil {
        
        private String className;
        private Object obj;
        
        public ClassUtil(String className) {
            this.className = className;
            loadClass(className);
        }
        public void loadClass(String className) {
            try {
                Class clazz = Class.forName(className);
                obj = clazz.newInstance();
                
            }
            catch (Exception e) {e.printStackTrace();}
        }
        
        public Map getMethodDetail() {
            Class clazz = obj.getClass();
            Method [] theMethods = clazz.getMethods();
            Map map = new HashMap();
            int i = 0;
            for (; i < theMethods.length; i++) {
                String methodName = theMethods[i].getName();
                Class [] methodParameters = theMethods[i].getParameterTypes();
                map.put(methodName , methodParameters);
            }
            return map;
        }
        
        public Object invoke(String methodName,Object args) {
            if (obj != null) {
                Class c = obj.getClass();
                Method [] methods = c.getMethods();
                for (int i = 0; i < methods.length; i++) {
                    String theName = methods[i].getName();
                    if (theName.equalsIgnoreCase(theName)) {
                        Object result = null;
                        try {
                            result = methods[i].invoke(this,args);
                        }
                        catch (Exception e) {e.printStackTrace();}
                        return result;
                    }
                }        
            }
            return null;
            
        }}class app {
        public static void main(String [] args) throws Exception {
            String className = "OBS";
            if (args.length > 0) {
                className = args[0];
            }
            ClassUtil cu = new ClassUtil(className);        Map map = cu.getMethodDetail();
            Set keys = map.keySet();
            Iterator it = keys.iterator();
            System.out.println("您加载的类有以下方法:");
            int num = 1;
            Vector methodNames = new Vector();
            while (it.hasNext()) {
                Object o = it.next();
                System.out.print("No." + (num++) + " method name : " + o);
                Class [] temp = (Class [])map.get((String)o);
                if (temp.length > 0) {
                System.out.print(" parameters : " );
                for (int i = 0; i < temp.length; i++) {
                    System.out.print(temp[i].getName() + "  ");
                } 
                }
                System.out.println();
                methodNames.add(o);
            }
            System.out.print("请选择您要调用的方法 : ");
            int input = System.in.read();
            char c = (char)input;
            String methodName = (String)methodNames.get(Integer.parseInt(c + "") - 1);
            Class [] theArgs = (Class [])map.get(methodName);
            Object result = null;
            Object [] parameters = null;
            if (theArgs.length > 0) {
            parameters = new Object[theArgs.length];
            for (int i = 0; i < args.length; i++) {
                parameters[i] = theArgs[i].newInstance();
            }
            
        }
            result = cu.invoke(methodName,parameters);
            System.out.println("执行结果 : " + result);
            
            
        }
    }
      

  5.   

    更正 方法 invoke 里的语句 result = methods[i].invoke(this,args); 为   result = methods[i].invoke(obj,args);