Javareflect 简明教程

java.lang.reflect - Constructor<T> Class

Introduction

java.lang.reflect.Constructor 类为某个类的单个构造器提供信息和访问权限。当使用 newInstance() 将实际参数与底层构造器的形式参数匹配时,Constructor 允许出现扩大转换,但如果出现缩小转换,则会抛出 IllegalArgumentException。

The java.lang.reflect.Constructor class provides information about, and access to, a single constructor for a class. Constructor permits widening conversions to occur when matching the actual parameters to newInstance() with the underlying constructor’s formal parameters, but throws an IllegalArgumentException if a narrowing conversion would occur.

Class declaration

以下是 java.lang.reflect.Constructor 类的声明−

Following is the declaration for java.lang.reflect.Constructor class −

public final class Constructor<T>
   extends AccessibleObject
      implements GenericDeclaration, Member

Class methods

Sr.No.

Method & Description

1

boolean equals(Object obj)Compares this Constructor against the specified object.

2

<T extends Annotation> T getAnnotation(Class<T> annotationClass)Returns this element’s annotation for the specified type if such an annotation is present, else null.

3

Annotation[] getDeclaredAnnotations()Returns all annotations that are directly present on this element.

4

Class<T> getDeclaringClass()Returns the Class object representing the class that declares the constructor represented by this Constructor object.

5

Class<?>[] getExceptionTypes()Returns an array of Class objects that represent the types of exceptions declared to be thrown by the underlying constructor represented by this Constructor object.

6

Type[] getGenericExceptionTypes()Returns an array of Type objects that represent the exceptions declared to be thrown by this Constructor object.

7

Type[] getGenericParameterTypes()Returns an array of Type objects that represent the formal parameter types, in declaration order, of the method represented by this Constructor object.

8

int getModifiers()Returns the Java language modifiers for the constructor represented by this Constructor object, as an integer.

9

String getName()Returns the name of this constructor, as a string.

10

Annotation[][] getParameterAnnotations()Returns an array of arrays that represent the annotations on the formal parameters, in declaration order, of the method represented by this Constructor object.

11

Class<?>[] getParameterTypes()Returns an array of Class objects that represent the formal parameter types, in declaration order, of the constructor represented by this Constructor object.

12

int hashCode()Returns a hashcode for this Constructor.

13

boolean isSynthetic()Returns true if this constructor is a synthetic constructor; returns false otherwise.

14

boolean isVarArgs()Returns true if this constructor was declared to take a variable number of arguments; returns false otherwise.

15

T newInstance(Object…​ initargs)Uses the constructor represented by this Constructor object to create and initialize a new instance of the constructor’s declaring class, with the specified initialization parameters.

16

String toGenericString()Returns a string describing this Constructor, including type parameters.

17

String toString()Returns a string describing this Constructor.

Methods inherited

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. java.lang.reflect.AccessibleObject

  2. java.lang.Object