Documentation

The Java™ Tutorials
Hide TOC
Members成员
Trail: The Reflection API

Lesson: Members课程:成员

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.Fieldjava.lang.reflect.Methodjava.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.每个概念都将用代码示例和相关输出进行说明,这些示例和输出将近似于预期的反射用途。


Note: According to The Java Language Specification, Java SE 7 Edition, the members of a class are the inherited components of the class body including fields, methods, nested classes, interfaces, and enumerated types. 根据《Java语言规范Java SE第7版》,类的成员是类主体的继承组件,包括字段、方法、嵌套类、接口和枚举类型。Since constructors are not inherited, they are not members. 由于构造函数不是继承的,所以它们不是成员。This differs from the implementing classes of java.lang.reflect.Member. 这与java.lang.reflect.Member的实现类不同。

Fields领域

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类提供了访问类型信息、设置和获取给定对象上字段值的方法。

Methods方法

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类提供了获取参数和返回值的类型信息的方法。它还可以用于调用给定对象上的方法。

Constructors构造函数

The Reflection APIs for constructors are defined in 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.构造函数的反射API在java.lang.reflect.Constructor中定义,与方法的反射API类似,但有两个主要例外:第一,构造函数没有返回值;其次,调用构造函数为给定类创建对象的新实例。


Previous page: Previous Lesson
Next page: Fields