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 is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. 反射通常用于需要能够检查或修改Java虚拟机中运行的应用程序的运行时行为的程序。This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.这是一个相对高级的特性,应该只由对该语言的基本知识有很强掌握的开发人员使用。考虑到这一点,反射是一种强大的技术,可以使应用程序执行本来不可能执行的操作。
Reflection is powerful, but should not be used indiscriminately. 反射很强大,但不应不加区别地使用。If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. 如果可以在不使用反射的情况下执行操作,则最好避免使用反射。The following concerns should be kept in mind when accessing code via reflection.在通过反射访问代码时,应记住以下注意事项。
private
fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. private
字段和方法,因此使用反射可能会产生意外的副作用,这可能导致代码功能失调,并可能破坏可移植性。This trail covers common uses of reflection for accessing and manipulating classes, fields, methods, and constructors. 本教程介绍了反射在访问和操作类、字段、方法和构造函数时的常见用法。Each lesson contains code examples, tips, and troubleshooting information.每一课都包含代码示例、提示和疑难解答信息。
Class
object and use it to examine properties of a class, including its declaration and contents.Class
对象并使用它检查类的属性(包括其声明和内容)的各种方法。enum
types, which define unique named object instances. enum
类型。enum
types.enum
类型的字段。The examples in this trail are designed for experimenting with the Reflection APIs. 本教程中的示例是为实验反射API而设计的。The handling of exceptions therefore is not the same as would be used in production code. 因此,异常的处理与生产代码中使用的不同。In particular, in production code it is not recommended to dump stack traces that are visible to the user.特别是,在生产代码中,不建议转储用户可见的堆栈跟踪。