如何在Java中找到给定类的所有子类? [英] How do you find all subclasses of a given class in Java?

查看:1156
本文介绍了如何在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:


  1. 获取类路径上存在的所有类名称的列表

  2. 加载每个类并测试它是否是所需类或接口的子类或实现。



<有一个很好的功能称为类型层次结构,设法显示这相当有效。

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中找到给定类的所有子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆