The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Reflection defines an interface 反射定义了一个接口java.lang.reflect.Member
which is implemented by java.lang.reflect.Field
, java.lang.reflect.Method
, and java.lang.reflect.Constructor
. java.lang.reflect.Member
,它由java.lang.reflect.Field
、java.lang.reflect.Method
和java.lang.reflect.Constructor
实现。These objects will be discussed in this lesson. 本课程将讨论这些对象。For each member, the lesson will describe the associated APIs to retrieve declaration and type information, any operations unique to the member (for example, setting the value of a field or invoking a method), and commonly encountered errors. 对于每个成员,本课程将描述检索声明和类型信息的相关API、成员特有的任何操作(例如,设置字段值或调用方法)以及常见错误。Each concept will be illustrated with code samples and related output which approximate some expected reflection uses.每个概念都将用代码示例和相关输出进行说明,这些示例和输出将近似于预期的反射用途。
java.lang.reflect.Member
. java.lang.reflect.Member
的实现类不同。Fields have a type and a value. 字段有一个类型和一个值。The java.lang.reflect.Field
class provides methods for accessing type information and setting and getting values of a field on a given object.java.lang.reflect.Field
类提供了访问类型信息、设置和获取给定对象上字段值的方法。
public
or transient
public
或transient
Methods have return values, parameters, and may throw exceptions. 方法具有返回值、参数,并且可能引发异常。The java.lang.reflect.Method
class provides methods for obtaining the type information for the parameters and return value. It may also be used to invoke methods on a given object.java.lang.reflect.Method
类提供了获取参数和返回值的类型信息的方法。它还可以用于调用给定对象上的方法。
The Reflection APIs for constructors are defined in 构造函数的反射API在java.lang.reflect.Constructor
and are similar to those for methods, with two major exceptions: first, constructors have no return values; second, the invocation of a constructor creates a new instance of an object for a given class.java.lang.reflect.Constructor
中定义,与方法的反射API类似,但有两个主要例外:第一,构造函数没有返回值;其次,调用构造函数为给定类创建对象的新实例。