Class.newInstance()和Constructor.newInstance() 的區別,詳細!!!

原文鏈接:https://www.cnblogs.com/keyi/p/7205686.html

通過反射創建新的類示例,有兩種方式: 
Class.newInstance() 
Constructor.newInstance() 



以下對兩種調用方式給以比較說明: 
Class.newInstance() 只能夠調用無參的構造函數,即默認的構造函數; 
Constructor.newInstance() 可以根據傳入的參數,調用任意構造構造函數。 


Class.newInstance() 拋出所有由被調用構造函數拋出的異常。 


Class.newInstance() 要求被調用的構造函數是可見的,也即必須是public類型的; 
Constructor.newInstance() 在特定的情況下,可以調用私有的構造函數。 



Class A(被調用的示例): 
Java代碼  

public class A {  
    private A() {  
        System.out.println("A's constructor is called.");  
    }  
  
    private A(int a, int b) {  
        System.out.println("a:" + a + " b:" + b);  
    }  
} 

 


Class B(調用者): 
Java代碼  

public class B {   
    public static void main(String[] args) {   
        B b=new B();   
        out.println("通過Class.NewInstance()調用私有構造函數:");   
        b.newInstanceByClassNewInstance();   
        out.println("通過Constructor.newInstance()調用私有構造函數:");   
        b.newInstanceByConstructorNewInstance();   
    }   
    /*通過Class.NewInstance()創建新的類示例*/   
    private void newInstanceByClassNewInstance(){   
        try {/*當前包名爲reflect,必須使用全路徑*/   
            A a=(A)Class.forName("reflect.A").newInstance();   
        } catch (Exception e) {   
            out.println("通過Class.NewInstance()調用私有構造函數【失敗】");   
        }  
    }  
      
    /*通過Constructor.newInstance()創建新的類示例*/   
    private void newInstanceByConstructorNewInstance(){   
        try {/*可以使用相對路徑,同一個包中可以不用帶包路徑*/   
            Class c=Class.forName("A");   
            /*以下調用無參的、私有構造函數*/   
            Constructor c0=c.getDeclaredConstructor();   
            c0.setAccessible(true);   
            A a0=(A)c0.newInstance();   
            /*以下調用帶參的、私有構造函數*/   
            Constructor c1=c.getDeclaredConstructor(new Class[]{int.class,int.class});   
            c1.setAccessible(true);   
            A a1=(A)c1.newInstance(new Object[]{5,6});   
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
    }   
}  




輸入結果如下: 

通過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()。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章