怎么用 class.forName(),来获得一个构造函数是private的类的实例?

解决方案 »

  1.   

    构造函数是private的类是不能被实例化的,一般来说这种类都会提供静态的工厂方法
      

  2.   

    你的意思相当于说怎么直接调用private Method.
      

  3.   

    不能获得,既然它定义为private,就是不希望别人通过构造函数来创建实例.
        不过你可以看看它有没有静态工厂方法,通过工厂方法得到一个实例
      

  4.   

    在类里面 定义一个自己的实例化 要是static 的 
    外面的类要用就调用一个方法 来返回这个对象在
      

  5.   

    这个singleton类必须实现一个接口过来的.我需要根据这个类的名字和路径用class.forName()获得class.再获得一个接口类型的实例. 我采用class.forName的时候用什么方式获得singleton的实例.
      

  6.   

    package tony.designpattern.singleton;import java.lang.reflect.*;public class Test {    public static void main(String[] args) {
            try {
                Class c = Class
                        .forName("tony.designpattern.singleton.LazySingleton");
                Method[] methods = c.getMethods();
                for (int i = 0; i < methods.length; i++) {
                    String className = methods[i].getReturnType().toString();
                    if (className.indexOf(".") > 0) {
                        className = className
                                .substring(className.lastIndexOf(".") + 1);
                        System.out.println("className:" + className);
                        if (className.equals("LazySingleton")) {//返回类型是这个的类
                            // if (methods[i].getModifiers() == Method.) {//原先想判断是否是static的
                            LazySingleton singleton = (LazySingleton) methods[i]
                                    .invoke(null, null);//静态方法可以不用实例,所以可以用null
                            // System.out.println(singleton instanceof
                            // LazySingleton);
                            singleton.print();
                            // }
                        }
                    }
                }
            } catch (Exception e) {
            }    }}
    ------------------------------------
    package tony.designpattern.singleton;/**
     * Description:
     * 
     * @author Tony.Fang
     * @version 0.1
     *  
     */
    public class LazySingleton {    private static LazySingleton lazySingleton = null;
        private int i = 0;
        
        private LazySingleton() {
        }    synchronized public static LazySingleton getInstance() {
            if (lazySingleton == null) {
                lazySingleton = new LazySingleton();
            }
            return lazySingleton;
        }    public void print() {
            
            System.out.println("lazySingleton:"+i++);
        }
    }
      

  7.   

    U can call private methods , r/w private fields by JNI .
      

  8.   

    把另一帖的回复 copy 过来,大家帮忙检查一下
    回复人: haha1903(中国人) ( ) 信誉:100  2005-07-13 09:30:00  得分: 0  
     
     
       要用反射,做个例子给你
    //test.Test1
    package test;public class Test1 {
    private int i; private Test1(int i) {
    this.i = i;
    }
    public void test() {
    System.out.println(i);
    }
    }
    //test.Test2
    package test;import java.lang.reflect.Constructor;
    import java.lang.reflect.Method;public class Test2 {
    public static void main(String[] args) throws Exception {
    Class c = Class.forName("test.Test1");
    Constructor constructor = c.getDeclaredConstructor(new Class[] { int.class });
    constructor.setAccessible(true);
    Object o = constructor.newInstance(new Object[] { new Integer(10) });
    Method testMethod = c.getMethod("test", null);
    testMethod.invoke(o, null);
    }
    }
    你试一下就明白了。
    但构造函数一定要存在。
      
     
    Top  
     
     回复人: haha1903(中国人) ( ) 信誉:100  2005-07-13 09:33:00  得分: 0  
     
     
       私有方法不等于不能调用,虽然这样调用是不好的,很容易引入问题。
    但有时做 UT 的时候,不得不用这样的办法。
      
     
    Top  
     
     回复人: haha1903(中国人) ( ) 信誉:100  2005-07-13 09:35:00  得分: 0  
     
     
       spring 中的 bean 默认就是 singleton 的,但也不一定有静态的工厂方法啊。
      
     
    Top  
     
      

  9.   

    把另一帖的回复转过来
    回复人: haha1903(中国人) ( ) 信誉:100  2005-07-13 09:30:00  得分: 0  
     
     
       要用反射,做个例子给你
    //test.Test1
    package test;public class Test1 {
    private int i; private Test1(int i) {
    this.i = i;
    }
    public void test() {
    System.out.println(i);
    }
    }
    //test.Test2
    package test;import java.lang.reflect.Constructor;
    import java.lang.reflect.Method;public class Test2 {
    public static void main(String[] args) throws Exception {
    Class c = Class.forName("test.Test1");
    Constructor constructor = c.getDeclaredConstructor(new Class[] { int.class });
    constructor.setAccessible(true);
    Object o = constructor.newInstance(new Object[] { new Integer(10) });
    Method testMethod = c.getMethod("test", null);
    testMethod.invoke(o, null);
    }
    }
    你试一下就明白了。
    但构造函数一定要存在。
      
     
    Top  
     
     回复人: haha1903(中国人) ( ) 信誉:100  2005-07-13 09:33:00  得分: 0  
     
     
       私有方法不等于不能调用,虽然这样调用是不好的,很容易引入问题。
    但有时做 UT 的时候,不得不用这样的办法。
      
     
    Top  
     
     回复人: haha1903(中国人) ( ) 信誉:100  2005-07-13 09:35:00  得分: 0  
     
     
       spring 中的 bean 默认就是 singleton 的,但也不一定有静态的工厂方法啊。
      
     
    Top