如何在Java中找到给定类的所有子类?

编程入门 行业动态 更新时间:2024-10-27 02:22:12
本文介绍了如何在Java中找到给定类的所有子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何一个人去尝试找到一个给定类(或给定接口的所有实现)的所有子类在Java? 到目前为止,我有一个方法来做到这一点,但我觉得它效率很低(至少说)。 方法是:

How does one go about and try to find all subclasses of a given class (or all implementors of a given interface) in Java? As of now, I have a method to do this, but I find it quite inefficient (to say the least). The method is:

  • 获取类路径上存在的所有类名称的列表
  • 加载每个类并测试它是否是所需类或接口的子类或实现。
  • <有一个很好的功能称为类型层次结构,设法显示这相当有效。

    In Eclipse, there is a nice feature called the Type Hierarchy that manages to show this quite efficiently. How does one go about and do it programmatically?

    推荐答案

    没有其他方法可以做到除了什么你描述。想想吧 - 怎么可能有人知道什么类扩展ClassX没有扫描类路径上的每个类?

    There is no other way to do it other than what you described. Think about it - how can anyone know what classes extend ClassX without scanning each class on the classpath?

    Eclipse只能告诉你关于超类和子类似乎一个有效的时间量,因为它已经在您按下显示在类型层次结构按钮(因为它不断地编译您的类,知道类路径上的一切,等等)加载的所有类型的数据。

    Eclipse can only tell you about the super and subclasses in what seems to be an "efficient" amount of time because it already has all of the type data loaded at the point where you press the "Display in Type Hierarchy" button (since it is constantly compiling your classes, knows about everything on the classpath, etc).

    更多推荐

    如何在Java中找到给定类的所有子类?

    本文发布于:2023-11-29 19:28:23,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1647404.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:子类   中找到   如何在   Java

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!