Most visited

Recently visited

Added in API level 1

Class

public final class Class
extends Object implements Serializable, GenericDeclaration, Type, AnnotatedElement

java.lang.Object
   ↳ java.lang.Class<T>


Class实例表示正在运行的Java应用程序中的类和接口。 枚举是一种类,注释是一种界面。 每个数组也都属于一个类,该类反映为具有相同元素类型和维数的所有数组共享的Class对象。 原始Java类型( booleanbytecharshortintlongfloat ,和double ),以及关键字void也表示为Class对象。

Class没有公共构造函数。 相反, Class对象是由Java虚拟机自动构造的,因为在加载类时以及在类加载器中调用defineClass方法。

以下示例使用 Class对象来打印对象的类名称:

     void printClassName(Object obj) {
         System.out.println("The class of " + obj +
                            " is " + obj.getClass().getName());
     }
 

也可以使用类文字来获取指定类型(或无效)的Class对象。 参见The Java™ Language Specification的 15.8.2节。 例如:

System.out.println("The name of class Foo is: "+Foo.class.getName());

也可以看看:

Summary

Public methods

<U> Class<? extends U> asSubclass(Class<U> clazz)

转换此 Class对象以表示由指定的类对象表示的类的子类。

T cast(Object obj)

将对象 Class到此 Class对象表示的类或接口。

boolean desiredAssertionStatus()

如果在调用此方法时初始化该类,则返回将分配给此类的断言状态。

static Class<?> forName(String name, boolean initialize, ClassLoader loader)

使用给定的类加载器返回与具有给定字符串名称的类或接口关联的 Class对象。

static Class<?> forName(String className)

返回与具有给定字符串名称的类或接口关联的 Class对象。

<A extends Annotation> A getAnnotation(Class<A> annotationClass)
Annotation[] getAnnotations()

返回包含此类的所有注释的数组。

<T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass)

返回 this元素(其类为 annotationClass的注释的关联列表,如果没有找到任何元素,则 annotationClass一个空数组。

String getCanonicalName()

返回Java语言规范定义的基础类的规范名称。

ClassLoader getClassLoader()

返回该类的类加载器。

Class[]<?> getClasses()

返回包含一个数组 Class代表所有的公共类和由此表示的类的成员接口的对象 Class对象。

Class<?> getComponentType()

返回表示数组的组件类型的 Class

Constructor<T> getConstructor(Class...<?> parameterTypes)

返回一个 Constructor对象,该对象反映此 Class对象表示的类的指定公共构造函数。

Constructor[]<?> getConstructors()

返回一个包含 Constructor对象的数组, Constructor对象反映此 Class对象表示的类的所有公共构造方法。

<T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass)

返回 this元素上直接存在的注释,其类别为 annotationClassnull如果没有找到。

Annotation[] getDeclaredAnnotations()

返回由此 Class表示的类直接定义的注释。

Class[]<?> getDeclaredClasses()

返回一个包含 Class对象的数组, Class对象声明所有声明为此 Class对象表示的类的成员的类和接口。

Constructor<T> getDeclaredConstructor(Class...<?> parameterTypes)

返回一个 Constructor对象,它反映此表示的类或接口的指定构造 Class对象。

Constructor[]<?> getDeclaredConstructors()

返回一个包含 Constructor对象的数组, Constructor对象反映由此 Class对象表示的类声明的所有构造函数。

Field getDeclaredField(String name)

返回一个 Field对象,它反映此表示的类或接口的指定已声明字段 Class对象。

Field[] getDeclaredFields()

返回一个包含 Field对象的数组, Field对象反映由此 Class对象表示的类或接口声明的所有字段。

方法 getDeclaredMethod(String name, Class...<?> parameterTypes)

返回一个 方法对象,它反映此表示的类或接口的指定声明的方法 Class对象。

Method[] getDeclaredMethods()

返回一个包含 方法对象的数组, 方法对象反映由此 Class对象表示的类或接口声明的所有方法。

Class<?> getDeclaringClass()

如果此 Class对象表示的类或接口是另一个类的成员,则返回表示声明它的类的 Class对象。

Class<?> getEnclosingClass()

返回基础类的直接封闭类。

Constructor<?> getEnclosingConstructor()

如果此 Class对象表示一个构造内的本地或匿名类,返回一个 Constructor表示基础类的立即封闭构造对象。

方法 getEnclosingMethod()

如果此 Class对象表示方法中的一个本地或匿名类,返回一个 方法表示基础类的立即封闭方法的对象。

T[] getEnumConstants()

返回此枚举类的元素,如果此Class对象不表示枚举类型,则返回null。

Field getField(String name)

返回一个 Field对象,它反映此表示的类或接口的指定公共成员字段 Class对象。

Field[] getFields()

返回一个包含 Field对象的数组, Field对象反映此 Class对象表示的类或接口的所有可访问公用字段。

Type[] getGenericInterfaces()

返回表示由此对象表示的类或接口直接实现的接口的 Type

Type getGenericSuperclass()

返回表示由此 Type表示的实体(类,接口,基元类型或void)的直接超类的 Class

Class[]<?> getInterfaces()

确定由此对象表示的类或接口实现的接口。

方法 getMethod(String name, Class...<?> parameterTypes)

返回一个 方法对象,它反映此表示的类或接口的指定公共成员方法 Class对象。

Method[] getMethods()

返回一个包含 方法对象的数组, 方法对象反映此 Class对象表示的类或接口的所有公共 成员方法,包括由类或接口声明的那些以及从超类和超接口继承的那些方法。

int getModifiers()

返回此类或接口的Java语言修饰符,以整数编码。

String getName()

返回此 Class对象表示的实体(类,接口,数组类,或基本类型)的 Class ,如 String

软件包 getPackage()

获取这个类的包。

ProtectionDomain getProtectionDomain()

返回 ProtectionDomain

URL getResource(String name)

查找具有给定名称的资源。

InputStream getResourceAsStream(String name)

查找具有给定名称的资源。

Object[] getSigners()

获取这个类的签名者。

String getSimpleName()

返回源代码中给出的基础类的简单名称。

Class<? super T> getSuperclass()

返回表示由此 Class表示的实体(类,接口,基元类型或void)超类的 Class

TypeVariable[]<Class<T>> getTypeParameters()

以声明顺序 GenericDeclaration表示由此 GenericDeclaration对象表示的通用声明声明的类型变量的 TypeVariable对象数组。

boolean isAnnotation()

如果此 Class对象表示注释类型,则返回true。

boolean isAnnotationPresent(Class<? extends Annotation> annotationType)

如果此元素上存在指定类型的注释,则返回true,否则返回false。

boolean isAnonymousClass()

当且仅当基础类是匿名类时返回 true

boolean isArray()

确定此 Class对象是否表示数组类。

boolean isAssignableFrom(Class<?> c)

确定此 Class对象表示的类或接口是否与指定的 Class参数所表示的类或接口的类或接口相同,或者是否为超类或超接口。

boolean isEnum()

当且仅当此类在源代码中声明为枚举时才返回true。

boolean isInstance(Object object)

确定指定的 Object是否与此 Class表示的对象分配兼容。

boolean isInterface()

确定指定的 Class对象是否表示接口类型。

boolean isLocalClass()

当且仅当基础类是本地类时返回 true

boolean isMemberClass()

当且仅当基础类是成员类时返回 true

boolean isPrimitive()

确定指定的 Class对象是否表示基元类型。

boolean isSynthetic()

如果此类是一个合成类,则返回true ; 否则返回false

T newInstance()

创建由此 Class对象表示的类的新实例。

String toString()

将对象转换为字符串。

Inherited methods

From class java.lang.Object
From interface java.lang.reflect.GenericDeclaration
From interface java.lang.reflect.AnnotatedElement

Public methods

asSubclass

Added in API level 1
Class<? extends U> asSubclass (Class<U> clazz)

转换此Class对象以表示由指定的类对象表示的类的子类。 检查演员是否有效,如果不是,则抛出ClassCastException 如果此方法成功,它总是返回对此类对象的引用。

当客户需要“缩小” Class对象的类型以将其传递给限制其愿意接受的Class对象的API时,此方法非常有用。 由于无法在运行时检查强制转换的正确性(因为通用类型是通过擦除实现的),所以强制转换会生成编译时警告。

Parameters
clazz Class
Returns
Class<? extends U> this Class object, cast to represent a subclass of the specified class object.
Throws
ClassCastException if this Class object does not represent a subclass of the specified class (here "subclass" includes the class itself).

cast

Added in API level 1
T cast (Object obj)

将对象 Class到此 Class对象表示的类或接口。

Parameters
obj Object: the object to be cast
Returns
T the object after casting, or null if obj is null
Throws
ClassCastException if the object is not null and is not assignable to the type T.

desiredAssertionStatus

Added in API level 1
boolean desiredAssertionStatus ()

如果在调用此方法时初始化该类,则返回将分配给此类的断言状态。 如果这个类已经设置了断言状态,则返回最近的设置; 否则,如果任何软件包默认断言状态属于该类,则返回最具体的相关软件包默认断言状态的最新设置; 否则,如果这个类不是一个系统类(即它有一个类加载器),它的类加载器的默认断言状态被返回; 否则,返回系统类的默认断言状态。

很少有程序员会对这种方法有任何需求; 它提供了JRE本身的好处。 (它允许类在它被初始化的时候确定是否应该启用断言。)注意,这个方法不保证返回当(或将要)与指定的类关联的实际断言状态,或将被)初始化。

Returns
boolean the desired assertion status of the specified class.

也可以看看:

forName

Added in API level 1
Class<?> forName (String name, 
                boolean initialize, 
                ClassLoader loader)

使用给定的类加载器返回与具有给定字符串名称的类或接口关联的Class对象。 给定类或接口的完全限定名称(采用与getName相同的格式),此方法尝试查找,加载和链接类或接口。 指定的类加载器用于加载类或接口。 如果参数loader为空,则通过引导类加载器加载该类。 仅当initialize参数为true并且它尚未初始化时才会初始化该类。

如果name表示原始类型或void,则将尝试在名称为name的未命名包中找到用户定义的类。 因此,此方法不能用于获取表示原始类型或void的任何Class对象。

如果 name表示一个数组类,则该数组类的组件类型将被加载但未初始化。

例如,在一个实例方法中,表达式:

Class.forName("Foo")
is equivalent to:
Class.forName("Foo", true, this.getClass().getClassLoader())
Note that this method throws errors related to loading, linking or initializing as specified in Sections 12.2, 12.3 and 12.4 of The Java Language Specification. Note that this method does not check whether the requested class is accessible to its caller.

如果 loadernull ,并且存在安全管理器,并且调用者的类加载器不为空,则此方法使用 RuntimePermission("getClassLoader")权限调用安全管理器的 checkPermission方法,以确保可以访问引导类加载器。

Parameters
name String: fully qualified name of the desired class
initialize boolean: whether the class must be initialized
loader ClassLoader: class loader from which the class must be loaded
Returns
Class<?> class object representing the desired class
Throws
LinkageError if the linkage fails
ExceptionInInitializerError if the initialization provoked by this method fails
ClassNotFoundException if the class cannot be located by the specified class loader

也可以看看:

forName

Added in API level 1
Class<?> forName (String className)

返回与具有给定字符串名称的类或接口关联的Class对象。 调用此方法相当于:

Class.forName(className, true, currentLoader)
where currentLoader denotes the defining class loader of the current class.

例如,下面的代码段返回运行时 Class描述符名为类 java.lang.Thread

Class t = Class.forName("java.lang.Thread")

调用 forName("X")会导致名为 X的类被初始化。

Parameters
className String: the fully qualified name of the desired class.
Returns
Class<?> the Class object for the class with the specified name.
Throws
LinkageError if the linkage fails
ExceptionInInitializerError if the initialization provoked by this method fails
ClassNotFoundException if the class cannot be located

getAnnotation

Added in API level 1
A getAnnotation (Class<A> annotationClass)

Parameters
annotationClass Class
Returns
A
Throws
NullPointerException

getAnnotations

Added in API level 1
Annotation[] getAnnotations ()

返回包含此类的所有注释的数组。 如果没有注释,则返回空数组。

Returns
Annotation[] all annotations present on this element

也可以看看:

getAnnotationsByType

T[] getAnnotationsByType (Class<T> annotationClass)

返回 this元素(其类为 annotationClass的注释的关联列表,如果没有找到任何元素,则 annotationClass一个空数组。

Parameters
annotationClass Class
Returns
T[]

getCanonicalName

Added in API level 1
String getCanonicalName ()

返回Java语言规范定义的基础类的规范名称。 如果基础类没有规范名称(例如,如果它是本地或匿名类或其组件类型没有规范名称的数组),则返回null。

Returns
String the canonical name of the underlying class if it exists, and null otherwise.

getClassLoader

Added in API level 1
ClassLoader getClassLoader ()

返回该类的类加载器。 某些实现可能使用null来表示引导类加载器。 如果此类由引导类加载器加载,则此方法将在此类实现中返回null。

如果存在安全管理器,并且调用者的类加载器不为空,并且调用者的类加载器与请求类加载器的类的类加载器不相同或是其类的祖先,则此方法调用安全管理器的方法 checkPermission具有 RuntimePermission("getClassLoader")权限以确保可以访问该类的类加载器。

如果此对象表示原始类型或void,则返回null。

Returns
ClassLoader the class loader that loaded the class or interface represented by this object.
Throws
SecurityException if a security manager exists and its checkPermission method denies access to the class loader for the class.

也可以看看:

getClasses

Added in API level 1
Class[]<?> getClasses ()

返回包含一个数组Class代表所有的公共类和由此表示的类的成员接口的对象Class对象。 这包括从超类继承的公共类和接口成员,以及由类声明的公共类和接口成员。 如果此Class对象没有公共成员类或接口,则此方法返回长度为0的数组。 如果此Class对象表示原始类型,数组类或void,则此方法还会返回长度为0的数组。

Returns
Class[]<?> the array of Class objects representing the public members of this class
Throws
SecurityException If a security manager, s, is present and any of the following conditions is met:

getComponentType

Added in API level 1
Class<?> getComponentType ()

返回表示数组的组件类型的Class 如果此类不表示数组类,则此方法返回null。

Returns
Class<?> the Class representing the component type of this class if this class is an array

也可以看看:

getConstructor

Added in API level 9
Constructor<T> getConstructor (Class...<?> parameterTypes)

返回一个Constructor对象,该对象反映此Class对象所代表的类的指定公共构造函数。 parameterTypes参数是一组Class对象,以声明的顺序标识构造函数的形式参数类型。 如果此Class对象表示在非静态上下文中声明的内部类,则形式参数类型将显式封闭实例作为第一个参数。

要反映的构造函数是由此 Class对象表示的类的公共构造函数,其形式参数类型与 parameterTypes指定的类型匹配。

Parameters
parameterTypes Class: the parameter array
Returns
Constructor<T> the Constructor object of the public constructor that matches the specified parameterTypes
Throws
NoSuchMethodException if a matching method is not found.
SecurityException If a security manager, s, is present and any of the following conditions is met:

getConstructors

Added in API level 1
Constructor[]<?> getConstructors ()

返回一个包含Constructor对象的数组, Constructor对象反映此Class对象所代表的类的所有公共构造函数。 如果类没有公共构造函数,或者类是数组类,或者类反映了原始类型或void,则返回长度为0的数组。 请注意,虽然此方法返回一个包含Constructor<T>对象的数组(该类为构造函数的数组),但此方法的返回类型为Constructor<?>[]不是可能预期的Constructor<T>[] 这种信息量较少的返回类型是必要的,因为在从这种方法返回之后,可以修改该数组以Constructor不同类别的Constructor对象,这违反了Constructor<T>[]的类型保证。

Returns
Constructor[]<?> the array of Constructor objects representing the public constructors of this class
Throws
SecurityException If a security manager, s, is present and any of the following conditions is met:

getDeclaredAnnotation

T getDeclaredAnnotation (Class<T> annotationClass)

返回 this元素上直接存在的注释,其类别为 annotationClassnull如果未找到任何内容)。

Parameters
annotationClass Class
Returns
T

getDeclaredAnnotations

Added in API level 1
Annotation[] getDeclaredAnnotations ()

返回由此Class表示的类直接定义的注释。 结果中不包括继承的注释。 如果根本没有注释,则返回一个空数组。

Returns
Annotation[] All annotations directly present on this element

也可以看看:

getDeclaredClasses

Added in API level 1
Class[]<?> getDeclaredClasses ()

返回一个包含Class对象的数组, Class对象声明所有声明为此Class对象表示的类的成员的类和接口。 这包括public,protected,default(包)访问,以及由类声明的私有类和接口,但不包括继承的类和接口。 如果该类没有声明类或接口作为成员此方法返回长度为0的数组,如果此Class对象表示一个基本类型,一个数组类,或空隙。

Returns
Class[]<?> the array of Class objects representing all the declared members of this class
Throws
SecurityException If a security manager, s, is present and any of the following conditions is met:

getDeclaredConstructor

Added in API level 9
Constructor<T> getDeclaredConstructor (Class...<?> parameterTypes)

返回一个Constructor对象,它反映此表示的类或接口的指定构造Class对象。 parameterTypes参数是一组Class对象,以声明顺序标识构造函数的形式参数类型。 如果此Class对象表示在非静态上下文中声明的内部类,则形式参数类型将显式封闭实例作为第一个参数。

Parameters
parameterTypes Class: the parameter array
Returns
Constructor<T> The Constructor object for the constructor with the specified parameter list
Throws
NoSuchMethodException if a matching method is not found.
SecurityException If a security manager, s, is present and any of the following conditions is met:

getDeclaredConstructors

Added in API level 1
Constructor[]<?> getDeclaredConstructors ()

返回反映由此Class对象表示的类声明的所有构造函数的Constructor对象的数组。 这些是公共的,受保护的,默认(包)访问和私有构造函数。 返回的数组中的元素没有排序并且没有任何特定的顺序。 如果类有一个默认构造函数,它将包含在返回的数组中。 如果此Class对象表示接口,基元类型,数组类或void,则此方法返回长度为0的数组。

请参阅 Java语言规范 ,第8.2节。

Returns
Constructor[]<?> the array of Constructor objects representing all the declared constructors of this class
Throws
SecurityException If a security manager, s, is present and any of the following conditions is met:

getDeclaredField

Added in API level 1
Field getDeclaredField (String name)

返回一个Field对象,它反映此表示的类或接口的指定已声明字段Class对象。 name参数是一个String ,它指定了所需字段的简单名称。 请注意,此方法不会反映数组类的length字段。

Parameters
name String: the name of the field
Returns
Field the Field object for the specified field in this class
Throws
NoSuchFieldException if a field with the specified name is not found.
NullPointerException if name is null
SecurityException If a security manager, s, is present and any of the following conditions is met:

getDeclaredFields

Added in API level 1
Field[] getDeclaredFields ()

返回反映由此Class对象表示的类或接口声明的所有字段的Field对象的数组。 这包括公共,受保护,默认(包)访问和专用字段,但不包括继承字段。 返回的数组中的元素没有排序并且没有任何特定的顺序。 如果类或接口声明任何字段此方法返回长度为0的数组,如果此Class对象表示一个基本类型,一个数组类,或空隙。

请参阅 Java语言规范 ,第8.2节和第8.3节。

Returns
Field[] the array of Field objects representing all the declared fields of this class
Throws
SecurityException If a security manager, s, is present and any of the following conditions is met:

getDeclaredMethod

Added in API level 9
方法 getDeclaredMethod (String name, 
                Class...<?> parameterTypes)

返回一个方法对象,它反映此表示的类或接口的指定声明的方法Class对象。 name参数是一个String ,它指定了所需方法的简单名称,而parameterTypes参数是一个由Class对象组成的数组,它以声明的顺序标识该方法的形式参数类型。 如果在一个类中声明了多个具有相同参数类型的方法,并且其中一个方法的返回类型比其他方法更具体,则返回该方法; 否则其中一种方法是任意选择的。 如果名称是“<init>”或“<clinit>”,则引发NoSuchMethodException

Parameters
name String: the name of the method
parameterTypes Class: the parameter array
Returns
方法 the 方法 object for the method of this class matching the specified name and parameters
Throws
NoSuchMethodException if a matching method is not found.
NullPointerException if name is null
SecurityException If a security manager, s, is present and any of the following conditions is met:

getDeclaredMethods

Added in API level 1
Method[] getDeclaredMethods ()

返回反映由此Class对象表示的类或接口声明的所有方法的方法对象的数组。 这包括公共,受保护,默认(包)访问和私有方法,但不包括继承的方法。 返回的数组中的元素没有排序并且没有任何特定的顺序。 如果类或接口声明没有方法此方法返回长度为0的数组,如果此Class对象表示一个基本类型,一个数组类,或空隙。 类初始化方法<clinit>不包含在返回的数组中。 如果类声明多个具有相同参数类型的公共成员方法,则它们都包含在返回的数组中。

请参阅 Java语言规范 ,第8.2节。

Returns
Method[] the array of 方法 objects representing all the declared methods of this class
Throws
SecurityException If a security manager, s, is present and any of the following conditions is met:

getDeclaringClass

Added in API level 1
Class<?> getDeclaringClass ()

如果此Class对象表示的类或接口是另一个类的成员,则返回表示声明它的类的Class对象。 如果此类或接口不是任何其他类的成员,则此方法返回null。 如果这个Class对象表示一个数组类,一个原始类型或void,那么这个方法返回null。

Returns
Class<?> the declaring class for this class

getEnclosingClass

Added in API level 1
Class<?> getEnclosingClass ()

返回基础类的直接封闭类。 如果基础类是顶级类,则此方法返回null

Returns
Class<?> the immediately enclosing class of the underlying class

getEnclosingConstructor

Added in API level 1
Constructor<?> getEnclosingConstructor ()

如果此Class对象表示一个构造内的本地或匿名类,返回一个Constructor表示基础类的立即封闭构造对象。 否则返回null 特别是,如果基础类是立即由类型声明,实例初始值设定项或静态初始值设定项包围的本地或匿名类,则此方法返回null

Returns
Constructor<?> the immediately enclosing constructor of the underlying class, if that class is a local or anonymous class; otherwise null.

getEnclosingMethod

Added in API level 1
方法 getEnclosingMethod ()

如果此Class对象表示方法中的一个本地或匿名类,返回一个方法表示基础类的立即封闭方法的对象。 否则返回null 特别是,如果基础类是立即由类型声明,实例初始值设定项或静态初始值设定项包围的本地或匿名类,则此方法返回null

Returns
方法 the immediately enclosing method of the underlying class, if that class is a local or anonymous class; otherwise null.

getEnumConstants

Added in API level 1
T[] getEnumConstants ()

返回此枚举类的元素,如果此Class对象不表示枚举类型,则返回null。

Returns
T[] an array containing the values comprising the enum class represented by this Class object in the order they're declared, or null if this Class object does not represent an enum type

getField

Added in API level 1
Field getField (String name)

返回一个Field对象,它反映此表示的类或接口的指定公共成员字段Class对象。 name参数是指定所需字段的简单名称的String

要反映的字段由以下算法决定。 设C为此对象表示的类:

  1. If C declares a public field with the name specified, that is the field to be reflected.
  2. If no field was found in step 1 above, this algorithm is applied recursively to each direct superinterface of C. The direct superinterfaces are searched in the order they were declared.
  3. If no field was found in steps 1 and 2 above, and C has a superclass S, then this algorithm is invoked recursively upon S. If C has no superclass, then a NoSuchFieldException is thrown.

请参阅 Java语言规范 ,第8.2节和第8.3节。

Parameters
name String: the field name
Returns
Field the Field object of this class specified by name
Throws
NoSuchFieldException if a field with the specified name is not found.
NullPointerException if name is null
SecurityException If a security manager, s, is present and any of the following conditions is met:

getFields

Added in API level 1
Field[] getFields ()

返回包含Field对象的数组, Field对象反映此Class对象表示的类或接口的所有可访问公用字段。 返回的数组中的元素没有排序并且没有任何特定的顺序。 如果类或接口没有可访问的公共字段,或者它表示数组类,基元类型或void,则此方法返回长度为0的数组。

特别是,如果这个Class对象代表一个类,这个方法返回这个类及其所有超类的公共字段。 如果此Class对象表示一个接口,则此方法将返回此接口及其所有超接口的字段。

此类方法不反映数组类的隐式长度字段。 用户代码应该使用类Array的方法来操作数组。

请参阅 Java语言规范 ,第8.2节和第8.3节。

Returns
Field[] the array of Field objects representing the public fields
Throws
SecurityException If a security manager, s, is present and any of the following conditions is met:

getGenericInterfaces

Added in API level 1
Type[] getGenericInterfaces ()

返回表示由此对象表示的类或接口直接实现的接口的 Type

如果superinterface是参数化类型,则返回的Type对象必须准确反映源代码中使用的实际类型参数。 如果它以前没有创建过,则会创建表示每个超级界面的参数化类型。 有关参数化类型的创建过程的语义,请参阅ParameterizedType的声明。

如果此对象表示一个类,则返回值是一个包含表示该类实现的所有接口的对象的数组。 数组中接口对象的顺序与此对象表示的类的声明的implements子句中的接口名称的顺序相对应。 在数组类的情况下,按照该顺序返回接口CloneableSerializable

如果此对象表示一个接口,则该数组包含表示由该接口直接扩展的所有接口的对象。 数组中接口对象的顺序对应于此对象表示的接口声明的extends子句中接口名称的顺序。

如果此对象表示不实现接口的类或接口,则该方法返回长度为0的数组。

如果此对象表示原始类型或void,则该方法返回长度为0的数组。

Returns
Type[] an array of interfaces implemented by this class
Throws
GenericSignatureFormatError if the generic class signature does not conform to the format specified in The Java™ Virtual Machine Specification
TypeNotPresentException if any of the generic superinterfaces refers to a non-existent type declaration
MalformedParameterizedTypeException if any of the generic superinterfaces refer to a parameterized type that cannot be instantiated for any reason

getGenericSuperclass

Added in API level 1
Type getGenericSuperclass ()

返回表示由此 Type表示的实体(类,接口,基元类型或void)的直接超类的 Class

如果超类是参数化类型,则返回的Type对象必须准确反映源代码中使用的实际类型参数。 如果它以前没有创建过,则会创建表示超类的参数化类型。 有关参数化类型的创建过程的语义,请参阅ParameterizedType的声明。 如果此Class代表Object类,接口,基元类型或void,则返回null。 如果此对象表示一个数组类,则返回代表Object类的Class对象。

Returns
Type the superclass of the class represented by this object
Throws
GenericSignatureFormatError if the generic class signature does not conform to the format specified in The Java™ Virtual Machine Specification
TypeNotPresentException if the generic superclass refers to a non-existent type declaration
MalformedParameterizedTypeException if the generic superclass refers to a parameterized type that cannot be instantiated for any reason

getInterfaces

Added in API level 1
Class[]<?> getInterfaces ()

确定由此对象表示的类或接口实现的接口。

如果此对象表示一个类,则返回值是一个包含表示该类实现的所有接口的对象的数组。 数组中接口对象的顺序对应于此对象表示的类的声明的implements子句中的接口名称的顺序。 例如,给出声明:

class Shimmer implements FloorWax, DessertTopping { ... }
suppose the value of s is an instance of Shimmer; the value of the expression:
s.getClass().getInterfaces()[0]
is the Class object that represents interface FloorWax; and the value of:
s.getClass().getInterfaces()[1]
is the Class object that represents interface DessertTopping.

如果此对象表示一个接口,则该数组包含表示接口扩展的所有接口的对象。 数组中接口对象的顺序与此对象表示的接口声明的extends子句中的接口名称的顺序相对应。

如果此对象表示不实现接口的类或接口,则该方法返回长度为0的数组。

如果此对象表示原始类型或void,则该方法返回长度为0的数组。

Returns
Class[]<?> an array of interfaces implemented by this class.

getMethod

Added in API level 9
方法 getMethod (String name, 
                Class...<?> parameterTypes)

返回方法对象,该对象反映此Class对象所表示的类或接口的指定公共成员方法。 name参数是指定所需方法的简单名称的String parameterTypes参数是一组Class对象,以声明的顺序标识方法的形式参数类型。 如果parameterTypesnull ,则将其视为空数组。

如果name是“ <init> ;”或“ <clinit> ”,则引发NoSuchMethodException 否则,要反映的方法由下面的算法确定。 设C为此对象表示的类:

  1. C is searched for any matching methods. If no matching method is found, the algorithm of step 1 is invoked recursively on the superclass of C.
  2. If no method was found in step 1 above, the superinterfaces of C are searched for a matching method. If any such method is found, it is reflected.
To find a matching method in a class C:  If C declares exactly one public method with the specified name and exactly the same formal parameter types, that is the method reflected. If more than one such method is found in C, and one of these methods has a return type that is more specific than any of the others, that method is reflected; otherwise one of the methods is chosen arbitrarily.

请注意,类中可能存在多个匹配方法,因为尽管Java语言禁止类声明具有相同签名但返回类型不同的多个方法,但Java虚拟机不会。 虚拟机中增加的灵活性可用于实现各种语言功能。 例如,协变回报可以用bridge methods来实现; 桥接方法和被覆盖的方法将具有相同的签名但不同的返回类型。

请参阅 Java语言规范第8.2和8.4节。

Parameters
name String: the name of the method
parameterTypes Class: the list of parameters
Returns
方法 the 方法 object that matches the specified name and parameterTypes
Throws
NoSuchMethodException if a matching method is not found or if the name is "<init>"or "<clinit>".
NullPointerException if name is null
SecurityException If a security manager, s, is present and any of the following conditions is met:

getMethods

Added in API level 1
Method[] getMethods ()

返回一个包含方法对象的数组, 方法对象反映此Class对象表示的类或接口的所有公共成员方法,包括由类或接口声明的那些以及从超类和超接口继承的那些方法。 数组类返回从Object类继承的所有(公共)成员方法。 返回的数组中的元素没有排序并且没有任何特定的顺序。 如果这此方法返回长度为0的数组Class对象表示一个类或接口没有公共成员方法,或者如果此Class对象表示一个基本类型或空隙。

类初始化方法<clinit>不包含在返回的数组中。 如果类声明多个具有相同参数类型的公共成员方法,则它们都包含在返回的数组中。

请参阅 Java语言规范第8.2和8.4节。

Returns
Method[] the array of 方法 objects representing the public methods of this class
Throws
SecurityException If a security manager, s, is present and any of the following conditions is met:

getModifiers

Added in API level 1
int getModifiers ()

返回此类或接口的Java语言修饰符,以整数编码。 修饰符由Java虚拟机的常数为publicprotectedprivatefinalstaticabstractinterface ; 他们应该使用类Modifier的方法解码。

如果底层类是一个数组类,则其publicprivateprotected改性剂是相同的组分的类型。 如果此Class表示一个基本类型或空隙,其public改性剂总是true ,其protectedprivate改性剂是总是false 如果该对象表示数组类,一个基本类型或void,则其final改性剂总是true及其界面改性总是false 其他修饰符的值不由本规范决定。

修改器编码在 Java虚拟机规范表4.1中定义。

Returns
int the int representing the modifiers for this class

也可以看看:

getName

Added in API level 1
String getName ()

Class形式返回此 Class对象所表示的实体(类,接口,数组类,或基本类型)的 String

如果此类对象表示不是数组类型的引用类型,则返回类的二进制名称,如 The Java™ Language Specification所指定的 那样

如果这个类对象表示一个原始类型或void,那么返回的名称是 String等于对应于原始类型或void的Java语言关键字。

如果此类对象表示一个数组类,则名称的内部形式由元素类型的名称组成,该名称前面有一个或多个表示数组嵌套深度的“ [ ”字符。 元素类型名称的编码如下所示:

Element Type     Encoding
boolean     Z
byte     B
char     C
class or interface     Lclassname;
double     D
float     F
int     I
long     J
short     S

类或接口名称 classname是上面指定的类的二进制名称。

例子:

 String.class.getName()
     returns "java.lang.String"
 byte.class.getName()
     returns "byte"
 (new Object[3]).getClass().getName()
     returns "[Ljava.lang.Object;"
 (new int[3][4][5][6][7][8][9]).getClass().getName()
     returns "[[[[[[[I"
 

Returns
String the name of the class or interface represented by this object.

getPackage

Added in API level 1
软件包 getPackage ()

获取这个类的包。 这个类的类加载器用于查找包。 如果该类是由引导类加载器加载的,则将搜索从CLASSPATH加载的一组包以查找该类的包。 如果该类的类加载器未创建包对象,则返回空值。

仅当信息在类中附带的清单中定义时,以及如果类加载器使用清单中的属性创建了包实例,那么程序包才具有版本和规范的属性。

Returns
软件包 the package of the class, or null if no package information is available from the archive or codebase.

getProtectionDomain

Added in API level 1
ProtectionDomain getProtectionDomain ()

返回这个类的ProtectionDomain 如果安装了安全管理器,则此方法首先使用RuntimePermission("getProtectionDomain")权限调用安全管理器的checkPermission方法,以确保获得ProtectionDomain

Returns
ProtectionDomain the ProtectionDomain of this class
Throws
SecurityException if a security manager exists and its checkPermission method doesn't allow getting the ProtectionDomain.

也可以看看:

getResource

Added in API level 1
URL getResource (String name)

查找具有给定名称的资源。 搜索与给定类关联的资源的规则由类的定义class loader实现。 这个方法委托给这个对象的类加载器。 如果此对象由引导类加载器加载,则该方法委托给getSystemResource(String)

在委派前,使用此算法从给定资源名称构建绝对资源名称:

  • If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
  • Otherwise, the absolute name is of the following form:
    modified_package_name/name

    其中 modified_package_name是此对象的包名称,其中 '/'代替 '.''\u002e' )。

Parameters
name String: name of the desired resource
Returns
URL A URL object or null if no resource with this name is found

getResourceAsStream

Added in API level 1
InputStream getResourceAsStream (String name)

查找具有给定名称的资源。 搜索与给定类关联的资源的规则由类的定义class loader实现。 这个方法委托给这个对象的类加载器。 如果此对象由引导类加载器加载,则该方法委托给getSystemResourceAsStream(String)

在委派前,使用此算法从给定资源名称构建绝对资源名称:

  • If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
  • Otherwise, the absolute name is of the following form:
    modified_package_name/name

    其中 modified_package_name是此对象的包名, '/'代替 '.''\u002e' )。

Parameters
name String: name of the desired resource
Returns
InputStream A InputStream object or null if no resource with this name is found
Throws
NullPointerException If name is null

getSigners

Added in API level 1
Object[] getSigners ()

获取这个类的签名者。

Returns
Object[] the signers of this class, or null if there are no signers. In particular, this method returns null if this object represents a primitive type or void.

getSimpleName

Added in API level 1
String getSimpleName ()

返回源代码中给出的基础类的简单名称。 如果基础类是匿名的,则返回空字符串。

数组的简单名称是附加“[]”的组件类型的简单名称。 特别是组件类型为匿名的数组的简单名称是“[]”。

Returns
String the simple name of the underlying class

getSuperclass

Added in API level 1
Class<? super T> getSuperclass ()

返回表示由此Class表示的实体(类,接口,基元类型或void)的超类的Class 如果此Class表示Object类,接口,基元类型或void,则返回null。 如果此对象表示一个数组类,则返回代表Object类的Class对象。

Returns
Class<? super T> the superclass of the class represented by this object.

getTypeParameters

Added in API level 1
TypeVariable[]<Class<T>> getTypeParameters ()

以声明顺序GenericDeclaration表示由此GenericDeclaration对象表示的通用声明声明的类型变量的TypeVariable对象数组。 如果基础泛型声明声明没有类型变量,则返回长度为0的数组。

Returns
TypeVariable[]<Class<T>> an array of TypeVariable objects that represent the type variables declared by this generic declaration
Throws
GenericSignatureFormatError if the generic signature of this generic declaration does not conform to the format specified in The Java™ Virtual Machine Specification

isAnnotation

Added in API level 1
boolean isAnnotation ()

如果此Class对象表示注释类型,则返回true。 请注意,如果此方法返回true,则isInterface()也会返回true,因为所有注记类型也都是接口。

Returns
boolean true if this class object represents an annotation type; false otherwise

isAnnotationPresent

Added in API level 1
boolean isAnnotationPresent (Class<? extends Annotation> annotationType)

如果此元素上存在指定类型的注释,则返回true,否则返回false。 此方法主要用于方便地访问标记注释。

Parameters
annotationType Class: the Class object corresponding to the annotation type
Returns
boolean true if an annotation for the specified annotation type is present on this element, else false

isAnonymousClass

Added in API level 1
boolean isAnonymousClass ()

当且仅当基础类是匿名类时返回 true

Returns
boolean true if and only if this class is an anonymous class.

isArray

Added in API level 1
boolean isArray ()

确定此 Class对象是否代表数组类。

Returns
boolean true if this object represents an array class; false otherwise.

isAssignableFrom

Added in API level 1
boolean isAssignableFrom (Class<?> c)

确定此Class对象表示的类或接口是否与指定的Class参数所表示的类或接口相同,或者是否为超类或超接口。 如果是,则返回true ; 否则返回false 如果此Class对象表示基元类型,则此方法返回true如果指定的Class参数恰好是此Class对象; 否则返回false

具体而言,此方法测试指定的Class参数所表示的类型是否可以通过标识转换或通过扩展参考转换转换为此Class对象表示的类型。 有关详细信息 ,请参阅Java语言规范 ,第5.1.1和5.1.4节。

Parameters
c Class: the Class object to be checked
Returns
boolean the boolean value indicating whether objects of the type cls can be assigned to objects of this class
Throws
NullPointerException if the specified Class parameter is null.

isEnum

Added in API level 1
boolean isEnum ()

当且仅当此类在源代码中声明为枚举时才返回true。

Returns
boolean true if and only if this class was declared as an enum in the source code

isInstance

Added in API level 1
boolean isInstance (Object object)

确定指定的Object是否与此Class表示的对象分配兼容。 此方法与Java语言instanceof操作符的动态等效。 该方法返回true如果指定Object参数为非空并且可以转换为通过此表示的引用类型Class对象不提高一个ClassCastException.它返回false否则。

特别是,如果此Class对象表示一个声明的类,则此方法返回true如果指定的Object参数是表示的类(或其任何子类)的实例; 否则返回false 如果此Class对象表示一个数组类,则如果指定的Object参数可通过标识转换或扩展参考转换转换为数组类的对象,则此方法返回true ; 否则返回false 如果此Class对象表示一个接口,则此方法返回true如果指定的Object参数的类或任何超类实现此接口; 否则返回false 如果这个Class对象表示一个原始类型,则此方法返回false

Parameters
object Object: the object to check
Returns
boolean true if obj is an instance of this class

isInterface

Added in API level 1
boolean isInterface ()

确定指定的 Class对象是否表示接口类型。

Returns
boolean true if this object represents an interface; false otherwise.

isLocalClass

Added in API level 1
boolean isLocalClass ()

当且仅当基础类是本地类时返回 true

Returns
boolean true if and only if this class is a local class.

isMemberClass

Added in API level 1
boolean isMemberClass ()

当且仅当基础类是成员类时返回 true

Returns
boolean true if and only if this class is a member class.

isPrimitive

Added in API level 1
boolean isPrimitive ()

确定指定的 Class对象是否表示基元类型。

有九个预定义的Class对象来表示八种基本类型和void。 这些是由Java虚拟机创建,并且具有相同的名称为他们所代表的基本类型,即booleanbytecharshortintlongfloat ,并double

这些对象只能通过以下公共静态最终变量访问,并且是此方法返回的唯一 Class对象 true

Returns
boolean true if and only if this class represents a primitive type

也可以看看:

isSynthetic

Added in API level 1
boolean isSynthetic ()

如果此类是一个合成类,则返回true ; 否则返回false

Returns
boolean true if and only if this class is a synthetic class as defined by the Java Language Specification.

newInstance

Added in API level 1
T newInstance ()

创建由此Class对象表示的类的新实例。 该类被实例化,就像具有空参数列表的new表达式一样。 如果该类尚未初始化,则该类将被初始化。

请注意,此方法传播由nullary构造函数抛出的任何异常,包括检查的异常。 使用此方法可以有效绕过编译时异常检查,否则编译器会执行该异常。 Constructor.newInstance方法通过将构造函数抛出的任何异常包装在(checked) InvocationTargetException避免此问题。

Returns
T a newly allocated instance of the class represented by this object.
Throws
IllegalAccessException if the class or its nullary constructor is not accessible.
InstantiationException if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.
ExceptionInInitializerError if the initialization provoked by this method fails.
SecurityException If a security manager, s, is present and any of the following conditions is met:

toString

Added in API level 1
String toString ()

将对象转换为字符串。 字符串表示形式是字符串“class”或“interface”,后跟一个空格,然后是由getName返回格式的类的完全限定名称。 如果此Class对象表示基元类型,则此方法返回基元类型的名称。 如果此Class对象表示void,则此方法返回“void”。

Returns
String a string representation of this class object.

Hooray!