Java Class 的 getConstructors() 方法



描述

Java Class 的 getConstructors() 方法返回一個包含 Constructor 物件的陣列,這些物件反映了由該 Class 物件表示的類的所有公共建構函式。如果類沒有公共建構函式,或者該類是陣列類,或者該類反映的是原始型別或 void,則返回長度為 0 的陣列。

宣告

以下是java.lang.Class.getConstructors() 方法的宣告

public Constructor<?>[] getConstructors() throws SecurityException

引數

返回值

此方法返回表示此類的公共建構函式的 Constructor 物件陣列。

異常

SecurityException − 如果存在安全管理器 s。

獲取類的建構函式示例

以下示例顯示了 java.lang.Class.getConstructors() 方法的用法。在這個程式中,我們為 java.awt.Panel 類建立了一個 Class 例項。現在使用 getConstructors() 方法,檢索類的建構函式並列印結果。

package com.tutorialspoint;

import java.lang.reflect.Constructor;

public class ClassDemo {

   public static void main(String[] args) {

      try {
         Class cls = Class.forName("java.awt.Panel");
         System.out.println("Panel Constructors =");

         /* returns the array of Constructor objects representing the public 
            constructors of this class */
         Constructor constructors[] = cls.getConstructors();
         for(int i = 0; i < constructors.length; i++) {
            System.out.println(constructors[i]);
         }
      } catch (Exception e) {
         System.out.println("Exception: " + e);
      }
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Panel Constructors =
public java.awt.Panel()
public java.awt.Panel(java.awt.LayoutManager)

獲取自定義類的建構函式示例

以下示例顯示了 java.lang.Class.getConstructors() 方法的用法。在這個程式中,我們建立了一個 ClassDemo 例項,然後使用 getClass() 方法檢索該例項的類。現在使用 getConstructors() 方法,檢索類的建構函式並列印結果。

package com.tutorialspoint;

import java.lang.reflect.Constructor;

public class ClassDemo {

   public static void main(String[] args) {

      try {
         ClassDemo c = new ClassDemo();
         Class cls = c.getClass();
         System.out.print("Class Constructors = ");

         /* returns the array of Constructor objects representing the public 
            constructors of this class */
         Constructor[] constructors = cls.getConstructors();
         for(int i = 0; i < constructors.length; i++) {
            System.out.println(constructors[i]);
         }
      } catch (Exception e) {
         System.out.println("Exception: " + e);
      }
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Class Constructors = 
public com.tutorialspoint.ClassDemo()

獲取 ArrayList 類建構函式示例

以下示例顯示了 java.lang.Class.getConstructors() 方法的用法。在這個程式中,我們檢索了 ArrayList 類。現在使用 getConstructors() 方法,檢索類的建構函式並列印結果。

package com.tutorialspoint;

import java.lang.reflect.Constructor;
import java.util.ArrayList;

public class ClassDemo {

   public static void main(String[] args) {

      try {
         Class cls = ArrayList.class;
         System.out.println("List Constructors =");

         /* returns the array of Constructor objects representing the public 
            constructors of this class */
         Constructor constructors[] = cls.getConstructors();
         for(int i = 0; i < constructors.length; i++) {
            System.out.println(constructors[i]);
         }
      } catch (Exception e) {
         System.out.println("Exception: " + e);
      }
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

List Constructors =
public java.util.ArrayList(java.util.Collection)
public java.util.ArrayList()
public java.util.ArrayList(int)
java_lang_class.htm
廣告
© . All rights reserved.