时间:2021-05-19
反射的用途 Uses of Reflection
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. 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.
反射被广泛地用于那些需要在运行时检测或修改程序行为的程序中。这是一个相对高级
的特性,只有那些语言基础非常扎实的开发者才应该使用它。如果能把这句警示时刻放在心
里,那么反射机制就会成为一项强大的技术,可以让应用程序做一些几乎不可能做到的事情。
反射的缺点 Drawbacks of Reflection
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.
尽管反射非常强大,但也不能滥用。如果一个功能可以不用反射完成,那么最好就不用。
在我们使用反射技术时,下面几条内容应该牢记于心:
性能第一 Performance Overhead
Because reflection involves types that are dynamically resolved, certain Java virtual machine
optimizations can not be performed. Consequently, reflective operations have slower performance
than their non-reflective counterparts, and should be avoided in sections of code which are called
frequently in performance-sensitive applications.
反射包括了一些动态类型,所以 JVM 无法对这些代码进行优化。因此,反射操作的效
率要比那些非反射操作低得多。我们应该避免在经常被 执行的代码或对性能要求很高的程
序中使用反射。
安全限制 Security Restrictions
Reflection requires a runtime permission which may not be present when running under a
security manager. This is in an important consideration for code which has to run in a restricted
security context, such as in an Applet.
使用反射技术要求程序必须在一个没有安全限制的环境中运行。如果一个程序必须在有
安全限制的环境中运行,如 Applet,那么这就是个问题了。
内部暴露 Exposure of Internals
Since reflection allows code to perform operations that would be illegal in non-reflective
code, such as accessing private fields and methods, the use of reflection can result in
unexpected side-effects, which may render code dysfunctional and may destroy portability.
Reflective code breaks abstractions and therefore may change behavior with upgrades of the
platform.
由于反射允许代码执行一些在正常情况下不被允许的操作(比如访问私有的属性和方
法),所以使用反射可能会导致意料之外的副作用--代码有功能上的错误,降低可移植性。
反射代码破坏了抽象性,因此当平台发生改变的时候,代码的行为就有可能也随着变化。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Java反射机制原理与用法。分享给大家供大家参考,具体如下:Java反射机制Reflection(反射)是被视为动态语言的关键,反射机制允许程序在
Java反射机制与动态代理,使得Java更加强大,Spring核心概念IoC、AOP就是通过反射机制与动态代理实现的。1Java反射示例:Useruser=ne
Java反射机制一、什么是反射机制简单的来说,反射机制指的是程序在运行时能够获取自身的信息。在java中,只要给定类的名字,那么就可以通过反射机制来获得类的所有
java反射机制是什么反射机制是在运行状态中,可以知道任何一个类的属性和方法,并且调用类的属性和方法;反射机制能够做什么1、判断运行对象的所属类2、构造任意一个
java反射java从很早的版本开始就引入了反射机制,java如今的框架底层大部分也都是使用反射实现的。这篇博客就去探索下java反射使用的方便之处。要说jav