我的困惑:Java 中的反射

发布于 2024-11-04 02:26:28 字数 275 浏览 4 评论 0原文

我刚刚读完《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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风情万种。 2024-11-11 02:26:28

如果您可以发布一些示例,我很乐意为您解释。
反射广泛用于需要提取有关正在运行的对象的元信息的框架(例如,依赖于注释或对象中的字段的框架,请考虑 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/

绾颜 2024-11-11 02:26:28

我认为一个很好的例子是基于仅在运行时才知道的类名实例化对象,例如包含在配置文件中的类名。
您仍然需要知道动态实例化的类的通用接口,因此您也有一些东西可以转换它们。但这可以让配置驱动将使用哪个实现。

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.

羁拥 2024-11-11 02:26:28

另一个例子可能是当您必须将一个对象转换为它的后代类时。如果您不确定该对象的类型,则可以使用instanceof来确保转换在运行时正确,避免类转换异常。

一个例子:

public void actionPerformed (ActionEvent e){
    Object obj = e.getSource();

    if (obj instanceof objType)
    objType t = (objType) obj;   // you can check the type using instanceof if you are not sure about obj class at runtime
}

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:

public void actionPerformed (ActionEvent e){
    Object obj = e.getSource();

    if (obj instanceof objType)
    objType t = (objType) obj;   // you can check the type using instanceof if you are not sure about obj class at runtime
}
紅太極 2024-11-11 02:26:28

在反射中提供此类功能的原因是由于工具/应用程序需要类、变量、方法的元信息的多种情况。例如:-

  1. IDE 使用自动完成功能来获取方法名称和属性名称。
  2. Tomcat Web 容器通过解析其 web.xml 文件和请求 URI 将请求转发到正确的模块。
  3. JUnit使用反射来枚举类中的所有方法;假设 testXXX 命名的方法作为测试方法或由 @Test 注释的方法。

要阅读有关反射的完整文章,您可以查看 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:-

  1. IDEs using auto completion functionality to get method names and attribute names.
  2. Tomcat web container to forward the request to correct module by parsing their web.xml files and request URI.
  3. JUnit uses reflection to enumerate all methods in a class; assuming either testXXX named methods as test methods or methods annoted by @Test.

To read full article about reflection you can check http://modernpathshala.com/Forum/Thread/Interview/308/give-some-examples-where-reflection-is-used

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文