我动态生成一个类的class
然后要调用他的方法,如果构造函数不含参数 则调用没问题
构造函数需要传一个参数,调用应该怎么写啊 
请大家帮忙构造函数不含参数:
public class Server{
Server(){}
public String getServiceName(){
return "HP";

}
 
 调用方法
Class server= Class.forName("test.server");
Object object = server.newInstance();
Method method = server.getMethod("getServiceName", String.class)
setMethod.invoke(object, MODELDATA[i]);如果构造函数要传个参数进去应该怎么调用啊 public class Server{
private Servername;
Server(String name){Servername = name;}
public String getServiceName(){
return Servername;

}help!!!!!!

解决方案 »

  1.   

    import java.lang.reflect.Constructor;
    import java.lang.reflect.Method;/*public class Server {
    Server() {
    } public String getServiceName() {
    return "HP";
    } public static void main(String[] args) throws Exception {
    Class server = Class.forName("Server");
    Object object = server.newInstance();
    Method method = server.getMethod("getServiceName");
    System.out.println(method.invoke(object));
    }
    }*/public class Server {
    private String Servername; Server(String name) {
    Servername = name;
    } public String getServiceName() {
    return Servername;
    }

    public static void main(String[] args) throws Exception {
    Class server = Class.forName("Server");
    Method method = server.getMethod("getServiceName");
    Constructor cs = server.getDeclaredConstructor(new Class[]{String.class});//构造方法
    Object object = cs.newInstance(new Object[]{"My Servers"});
    System.out.println(method.invoke(object));
    }
    }
      

  2.   


        public static void main(String[] args) throws Exception {
            Class server = Class.forName("Server");
            Method method = server.getMethod("getServiceName");
            Constructor cs = server.getConstructor(new Class[]{String.class});//构造方法
            Object object = cs.newInstance(new Object[]{"My Servers"});
    //继续调用...
        }
      

  3.   

    继续问 如果我 有2个类 a b 都是以上方法load到虚拟机的的那么如果a的一个方法里要b做参数.应该怎么实现比如
     public class A{
        private String Servername;    Server(String name) {
            Servername = name;
        }    public String getServiceName(B b) {
            return b.geName();
        }
     public class B{
        private String ClientName;    B(String name) {
            ClientName= name;
        }    public String getName() {
            return ClientName;
        }就是要用Method 的方法
      

  4.   

    下面是以前写的一个封装数据库连接类的类
    其中:Main函数中的ConnPoolLcl.DatabaseAccess为数据库连接的类名,这个类里有query(strsql)、updatesql(strsql)、close()等方法,下面的程序中涉及到用Method调用ConnPoolLcl.DatabaseAccess类中带有参数和不带参数的方法,以及分别调用带参数和不带参数的构造函数的方法,具体方法参看主函数,看后你就懂了
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */package nciutil;import java.lang.reflect.Constructor;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.logging.Level;
    import java.util.logging.Logger;/**
     *
     * @author heavens
     */
    public class ExecuteSqlUtil {    private Class clsDBAccess;
        private Object objClass;    /**
         * 调用默认构造方法,不带参数
         * @param classname 调用的类名
         */
        public ExecuteSqlUtil(String classname){
            try {
                clsDBAccess = Class.forName(classname);
                objClass = clsDBAccess.newInstance();
            } catch (InstantiationException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
        }    /**
         * 调用带有参数的构造方法
         * @param classname 调用类名
         * @param parameterTypes 参数类型组
         * @param parameterValues 参数值组
         */
        public ExecuteSqlUtil(String classname, Class[] parameterTypes, Object[] parameterValues){
            try {
                clsDBAccess = Class.forName(classname);
                Constructor cs = clsDBAccess.getDeclaredConstructor(parameterTypes);//构造方法
                objClass = cs.newInstance(parameterValues);
            } catch (InstantiationException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvocationTargetException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchMethodException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
        }    /**
         * 对update方法的封装
         * @param sql
         * @return
         */
        public boolean updateSql(String sql){
            boolean flag = false;
            try {
                Class[] types = new Class[1];
                types[0] = String.class;
                Method mtd = clsDBAccess.getMethod("update", types);
                mtd.invoke(objClass, sql);
                Method mtd2 = clsDBAccess.getMethod("close");
                mtd2.invoke(objClass);
                flag = true;
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvocationTargetException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchMethodException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
            return flag;
        }    /**
         * 对query方法的封装
         * @param sql
         * @return
         */
        public ResultSet querySql(String sql){
            ResultSet rs = null;
            try {
                Class[] types = new Class[1];
                types[0] = String.class;
                Method mtd = clsDBAccess.getMethod("query", types);
                rs = (ResultSet)mtd.invoke(objClass, sql);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvocationTargetException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchMethodException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
            return rs;
        }    /**
         * 关闭数据库连接
         * @return
         */
        public boolean closeConnection(){
            boolean flag = false;
            try {
                Method mtd = clsDBAccess.getMethod("close");
                mtd.invoke(objClass);
                flag = true;
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvocationTargetException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchMethodException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
            return flag;
        }    public static void main(String argv[]){
            try {
                ExecuteSqlUtil mysql = new ExecuteSqlUtil("ConnPoolLcl.DatabaseAccess", new Class[]{boolean.class}, new Object[]{true});
                ResultSet rs = mysql.querySql("select * from user");
                while(rs.next()){
                    System.out.println(rs.getString(1)+"======"+rs.getString(2));
                }
                System.exit(0);
            }
            catch (SQLException ex) {
                Logger.getLogger(ExecuteSqlUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
      

  5.   

    请参看http://www.javaren.net.cn/dispbbs.asp?boardid=5&Id=68