类型转换做过没?   DAN100 dan100 = (DAN100)你取到的实例

解决方案 »

  1.   

    我想这些类应该不是很多的。。你可以事先先存在到一张HastTable中去。。然后用的时候掉出这些类。。 可以用一个抽象工厂得到。//接口
    先写个接口ClassInterface,接口里有所有的方法如golfMethod 等等。
    //工厂类
    public class ClassFactory {
    public static ClassFactory factory(String class_name) throws Exception { if (ClassHashTable.OBJTABLE.get(class_name) != null)
    return (ClassInterface) ClassFactory.OBJTABLE.get(class_name);
    else
    throw new Exception("类获取失败");
    }
    }//哈西表
    public class ClassHashTable { public static final Hashtable CLASSTABLE = new Hashtable();
    static {
    CLASSTABLE.put("DAN100", DAN100.class);
    CLASSTABLE.put("DAN101", DAN101.class);
    CLASSTABLE.put("DAN102", DAN102.class);
    .....
    .....
    }
    }通用,方法也可以通过这样一个抽象工厂得到:)
      

  2.   

    Class.forName()
    Class.getMethod()
    Method.invoke()
      

  3.   

    我想你的错误是在invoke(Object ,Object[])
    第一个Object DAN100类的实例,注意是实例,可以用Class类的.newInstance()方法就可以了,我写了一个测试例子,,很乱,但希望对你有所帮助
    package t2;import java.lang.ref.*;
    import java.lang.reflect.*;public class test {    public test() {
        }
        public void t2(Integer s,String s2)
        {
            System.out.println(s2+s);
        }
        public static void main(String[] args) throws Exception{
            Class c = "test".getClass().forName("t2.test");
            Method[] m = c.getMethods();
            Object[] i = new Object[2];
            i[0]=new Integer(0);
            i[1]=new String("sss");
            int j=0;
            System.out.println("m.lenth="+m.length);
            for( j=0;j<m.length;j++)
            {
                if(m[j].getName().equals("t2"))
                    break;
            }
            System.out.println(j);
            m[1].invoke(c.newInstance(),i);
            //System.out.println(c);
        }
    }
      

  4.   

    你是不是要调用DAN100类的golfMethod方法,如果是,这样就可以了:import java.lang.reflect.Method;public class Test
    {
        public static void main(String[] args)
        {
            String className = "DAN100";
            String methodName = "golfMethod";        Class cls = Class.forName(className);
            Class[] paraTypes = new Class[]{Integer.class, Integer.class};
            Method method = cls.getDeclaredMethod(methodName, paraTypes);        //create instance
            Object instance = cls.newInstance();        //prepare parameters
            int value1 = 1;
            int value2 = 2;
            Object[] parameters = new Object[2];
            parameters[0] = new Integer(value1);
            parameters[1] = new Integer(value2);        //invoke the method
            method.invoke(instance, parameters);
        }
    }
      

  5.   

    但我做字符串比较,他们不相等(晕!),我想你用的是==而不是equals另外这种程序应该不需要太多代码吧,把关键代码贴出来