我的困惑:Java 中的反射
我刚刚读完《Thinking in Java》中有关类型信息和反射的章节。虽然 instanceof
对我来说似乎很自然,但一些反射的例子让我感到困惑。我想知道Java项目中是否广泛使用反射?反思的“好处”是什么?您能推荐一些关于反思和类型信息的有趣讲座以及更多好的和有价值的例子吗?
编辑(还有一个问题):
为什么使用java.lang.reflect.Method.setAccesible()
访问私有方法和字段很有用?
提前致谢。
I've just finished reading the chapter of 'Thinking in Java' concerning type information and reflection. While instanceof
seems quite natural to me, some examples of reflection made me confused. I want to know if reflection is widely used in Java projects? What are 'the good parts' of reflection? Can you suggest any interesting lectures about reflection and type information with more good and worthy examples?
Edit (one more question):
Why is it useful to access private methods and fields withjava.lang.reflect.Method.setAccesible()
?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您可以发布一些示例,我很乐意为您解释。
反射广泛用于需要提取有关正在运行的对象的元信息的框架(例如,依赖于注释或对象中的字段的框架,请考虑 Hibernate、Spring 和许多其他框架)。
在更高的层上,我有时使用反射来提供通用功能(例如,对对象中的每个字符串进行编码、模拟鸭子类型等)。
我知道您已经读过一本涵盖反射基础知识的书,但我需要指出 Sun(呃.. Oracle)官方教程是必读的: http://download.oracle.com/javase/tutorial/reflect/
if you could post some of the examples I would be glad to explain it for you.
Reflection is wildly used with frameworks that need to extract meta-info about the running object (e.g. frameworks which depends on Annotations or the fields in your objets, think about Hibernate, Spring and a lot others).
On a higher layer, I sometimes use reflection to provide generic functionality (e.g. to encode every String in an object, emulate Duck Typing and such).
I know that you already read a book which covers the basics about reflection, but I need to point Sun (erm.. Oracle) official Tutorial as a must read: http://download.oracle.com/javase/tutorial/reflect/
我认为一个很好的例子是基于仅在运行时才知道的类名实例化对象,例如包含在配置文件中的类名。
您仍然需要知道动态实例化的类的通用接口,因此您也有一些东西可以转换它们。但这可以让配置驱动将使用哪个实现。
One good example in my opinion is instantiating objects based on class names that are known only at runtime, for example contained in a configuration file.
You will still need to know a common interface to the classes you're dynamically instantiating, so you have something to cast them too. But this lets a configuration drive which implementation will be used.
另一个例子可能是当您必须将一个对象转换为它的后代类时。如果您不确定该对象的类型,则可以使用instanceof来确保转换在运行时正确,避免类转换异常。
一个例子:
Another example could be when you have to cast an object to a class that it's a descendant. If you are not sure about the type of that object, you can use instanceof to assure that the cast will be correct at runtime avoiding a class cast exception.
An example:
在反射中提供此类功能的原因是由于工具/应用程序需要类、变量、方法的元信息的多种情况。例如:-
要阅读有关反射的完整文章,您可以查看 http://modernpathshala.com/Forum/Thread/Interview/308/give-some-examples-where-reflection-is-used
The reason to provide such features in Reflection is due to multiple situations where tool/application needs meta information of class, variables, methods. For example:-
To read full article about reflection you can check http://modernpathshala.com/Forum/Thread/Interview/308/give-some-examples-where-reflection-is-used