利用反射机制构造一个类对象

    public Object newInstance(String ClassName, Object[] args) throws Exception
    {
        Class newone = Class.forName(ClassName);
        Class[] argsClass = new Class[args.length];
        for (int i = 0; i < args.length; i++)
        {
            argsClass[i] = args[i].getClass();
        }
        Constructor cons = newone.getConstructor(argsClass);
        return cons.newInstance(args);
    }

你可能感兴趣的:(反射机制)