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.