Java的.lang.reflect.Constructor类在Java中
Java.lang.reflect.Constructor 类用于管理构造函数元数据,例如构造函数的名称、构造函数的参数类型和构造函数的访问修饰符。我们可以在运行时检查类的构造函数并实例化对象。 Constructor[] 数组将为类中声明的每个公共构造函数提供一个 Constructor 实例。
为了获得Constructor对象,一 可以从 Class 对象中获取 Constructor 类对象。
参数: T- 声明构造函数的类
实现的接口如下:
- 注释元素
- 通用声明
- 成员
插图:
Class aClass = Employee.class;
Constructor[] constructors = aClass.getConstructors();
先决条件:
让我们讨论一些可以获取构造函数信息的方法
- getConstructors() :可以获取相应类中的所有公共构造函数。它返回一个构造函数数组。
- getDeclaredConstructors() :一个 无论 public 关键字如何,都可以获取相应类中的所有构造函数。
- getName():可以获取相应构造函数的名称。
- getModifiers():这个 以整数形式返回由此构造函数对象表示的构造函数的Java语言修饰符。 Modifier 类应该用于解码修饰符。
- getParameterTypes():这个 返回特定构造函数的参数类型。
此外,该 此类的主要方法以表格形式给出如下:Method Description equals(Object obj) Compares this Constructor against the specified object. getAnnotatedReceiverType() Returns an AnnotatedType object that represents the use of a type to specify the receiver type of the method/constructor represented by this Executable object. getAnnotatedReturnType() Returns an AnnotatedType object that represents the use of a type to specify the return type of the method/constructor represented by this Executable. getAnnotation(Class Returns this element’s annotation for the specified type if such an annotation is present, else null. getDeclaredAnnotations() Returns annotations that are directly present on this element. getDeclaringClass() Returns the Class object representing the class or interface that declares the executable represented by this object. getExceptionTypes() Returns an array of Class objects that represent the types of exceptions declared to be thrown by the underlying executable represented by this object. getGenericExceptionTypes() Returns an array of Type objects that represent the exceptions declared to be thrown by this executable object. getGenericParameterTypes() Returns an array of Type objects that represent the formal parameter types, in declaration order, of the executable represented by this object. hashcode() Return the hashcode for this constructor isSynthetic() Returns true if this constructor is a synthetic constructor isVarArgs() Returns true if this constructor was declared to take a variable number of arguments toGenericString() Returns a string describing this constructor toString() Returns a string describing this constructor
例子:
Java
// Java program to show uses of Constructor class
// present in java.lang.reflect package
// Importing package to that examine and
// introspect upon itself
import java.lang.reflect.*;
// Class 1
// Helper class
class Employee {
// Member variables of this class
int empno;
String name;
String address;
// Constructor of this class
// Constructor 1
public Employee(int empno, String name, String address)
{
// 'this' keyword refers to the
// current object itself
this.empno = empno;
this.name = name;
this.address = address;
}
// Constructor 2
public Employee(int empno, String name)
{
this.empno = empno;
this.name = name;
}
// Constructor 3
private Employee(String address)
{
this.address = address;
}
// Constructor 4
protected Employee(int empno) { this.empno = empno; }
}
// Class 2
// Main class
public class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an object of above class
// in the main() method
Class c = Employee.class;
// Display message
System.out.println("All public constructor are :");
Constructor[] cons = c.getConstructors();
for (Constructor con : cons)
// It will return all public constructor
System.out.print(con.getName() + " ");
// Display message for better readability
System.out.println(
"\n\nAll constructor irrespective of access modifiers");
// Getting constructors of this class
// using getDeclaredConstructors() method
cons = c.getDeclaredConstructors();
// Iterating to print all constructors
for (Constructor con : cons)
System.out.print(con.getName() + " ");
// Display message
System.out.println(
"\n\naccess modifiers of each constructor");
// Iterating to get all the access modifiers
for (Constructor con : cons)
// Print all the access modifiers for all
// constructors
System.out.print(
Modifier.toString(con.getModifiers())
+ " ");
// Parameters types
// Display message only
System.out.println(
"\n\ngetting parameters type of each constructor");
// Iteerating to get parameter types of each
// constructor using for-each loop
for (Constructor con : cons) {
Class[] parameratertypes
= con.getParameterTypes();
for (Class c1 : parameratertypes) {
// Print and display parameter types of all
// cnstructors
System.out.print(c1.getName() + " ");
}
// Here we are done with inner loop
// New line
System.out.println();
}
}
}
All public constructor are :
Employee Employee
All constructor irrespective of access modifiers
Employee Employee Employee Employee
access modifiers of each constructor
protected private public public
getting parameters type of each constructor
int
java.lang.String
int java.lang.String
int java.lang.String java.lang.String