new()与newInstance() 的参数版本与无参数版本详解

来源:互联网 发布:linux启动mysql客户端 编辑:程序博客网 时间:2024/06/16 12:11

参考:http://liliugen.iteye.com/blog/259199

http://xiaohuafyle.iteye.com/blog/1607258

实例化一个类可以使用newInstance()或new()

前者创建对象的方式是通过类加载机制,Java中工厂模式经常使用newInstance来创建对象

例如:
  Class c = Class.forName(“A”);factory = (AInterface)c.newInstance();
  其中AInterface是A的接口,如果下面这样写,你可能会理解:
  String className = "A";Class c = Class.forName(className);factory = (AInterface)c.newInstance();
  进一步,如果下面写,你可能会理解:
  String className = readfromXMlConfig;//从xml 配置文件中获得字符串Class c = Class.forName(className);factory = (AInterface)c.newInstance();
  上面代码就消灭了A类名称,优点:无论A类怎么变化,上述代码不变,甚至可以更换A的兄弟类B , C , D....等,只要他们继承Ainterface就可以。

newInstance: 弱类型。低效率。只能调用无参构造。
new: 强类型。相对高效。能调用任何public构造。
newInstance()是实现IOC、反射、面对接口编程 和 依赖倒置 等技术方法的必然选择,new 只能实现具体类的实例化,不适合于接口编程。


newInstance() 的参数版本与无参数版本详解

通过反射创建新的类示例,有两种方式:
Class.newInstance()
Constructor.newInstance()

以下对两种调用方式给以比较说明:
Class.newInstance() 只能够调用无参的构造函数,即默认的构造函数;
Constructor.newInstance() 可以根据传入的参数,调用任意构造构造函数。

Class.newInstance() 抛出所有由被调用构造函数抛出的异常。

Class.newInstance() 要求被调用的构造函数是可见的,也即必须是public类型的;
Constructor.newInstance() 在特定的情况下,可以调用私有的构造函数。

Class A(被调用的示例):

Java代码  收藏代码
  1. public class A {  
  2.     private A() {  
  3.         System.out.println("A's constructor is called.");  
  4.     }  
  5.   
  6.     private A(int a, int b) {  
  7.         System.out.println("a:" + a + " b:" + b);  
  8.     }  
  9. }  

Class B(调用者):
Java代码  收藏代码
  1. public class B {   
  2.     public static void main(String[] args) {   
  3.         B b=new B();   
  4.         out.println("通过Class.NewInstance()调用私有构造函数:");   
  5.         b.newInstanceByClassNewInstance();   
  6.         out.println("通过Constructor.newInstance()调用私有构造函数:");   
  7.         b.newInstanceByConstructorNewInstance();   
  8.     }   
  9.     /*通过Class.NewInstance()创建新的类示例*/   
  10.     private void newInstanceByClassNewInstance(){   
  11.         try {/*当前包名为reflect,必须使用全路径*/   
  12.             A a=(A)Class.forName("reflect.A").newInstance();   
  13.         } catch (Exception e) {   
  14.             out.println("通过Class.NewInstance()调用私有构造函数【失败】");   
  15.         }  
  16.     }  
  17.       
  18.     /*通过Constructor.newInstance()创建新的类示例*/   
  19.     private void newInstanceByConstructorNewInstance(){   
  20.         try {/*可以使用相对路径,同一个包中可以不用带包路径*/   
  21.             Class c=Class.forName("A");   
  22.             /*以下调用无参的、私有构造函数*/   
  23.             Constructor c0=c.getDeclaredConstructor();   
  24.             c0.setAccessible(true);   
  25.             A a0=(A)c0.newInstance();   
  26.             /*以下调用带参的、私有构造函数*/   
  27.             Constructor c1=c.getDeclaredConstructor(new Class[]{int.class,int.class});   
  28.             c1.setAccessible(true);   
  29.             A a1=(A)c1.newInstance(new Object[]{5,6});   
  30.         } catch (Exception e) {   
  31.             e.printStackTrace();   
  32.         }   
  33.     }   
  34. }  

输入结果如下:
通过Class.NewInstance()调用私有构造函数:
通过Class.NewInstance()调用私有构造函数【失败】
通过Constructor.newInstance()调用私有构造函数:
A's constructor is called.
a:5 b:6

说明方法newInstanceByClassNewInstance调用失败,而方法newInstanceByConstructorNewInstance则调用成功。
如果被调用的类的构造函数为默认的构造函数,采用Class.newInstance()则是比较好的选择,
一句代码就OK;如果是老百姓调用被调用的类带参构造函数、私有构造函数,
就需要采用Constractor.newInstance(),两种情况视使用情况而定。
不过Java Totorial中推荐采用Constractor.newInstance()。

0 0
原创粉丝点击