java newInstance() 的參數版本與無參數版本詳解

來源:互聯網
上載者:User

標籤:

newInstance() 的參數版本與無參數版本詳解 部落格分類:
     
  • Core Java
 通過反射建立新的類樣本,有兩種方式: 
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()。 

java newInstance() 的參數版本與無參數版本詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.