如何获取方法继承信息?

发布于 2024-10-31 06:29:21 字数 457 浏览 0 评论 0原文

假设 Dog 类扩展了抽象类 Animal 并实现了 Play 类。 然后,在类 Dog 中,它实现了继承的抽象方法 Animal.eat() 和 Play.jump()。 想象一下,在 Eclipse 中,Dog.eat() 和 Dog.jump() 的左边会有一个小三角形。如果你把鼠标放在小三角形上,它会告诉你这个方法实现了哪个类的哪个抽象方法。

我想知道这个方法继承信息是如何获得的。我的问题是:如果我有一组 .java 文件,在没有任何 IDE 帮助的情况下,如何提取与 Eclipse 相同的方法继承信息(比如这个方法从哪个类实现哪个抽象方法)?

我可以想到一种方法:给定 Dog 类,我首先获取该类 Animal 和 Play 的继承信息。然后我比较 Dog 和 Animal/Play 的所有方法签名,看看哪一个实现了什么。但这种做法似乎有点幼稚,而且效率可能不是很高。因此,请告诉我是否有任何好的方法可以用来获取方法继承信息。

谢谢!

Suppose class Dog extends abstract class Animal and implements class Play.
Then, in class Dog, it implements the inherited abstract method Animal.eat() and Play.jump().
Imagine in Eclipse, there will be a little triangle on the left of Dog.eat() and Dog.jump(). And if you put your mouse on the little triangle, it will tell you that this method implements which abstract method from which class.

I was wondering how this method inheritance info is obtained. And my question is: if I have a set of .java files, without any help of IDE, how can I extract the same method inherit info as Eclipse does (like this method implements which abstract method from what class)?

I can think of one approach: given class Dog, I first obtain the inherit info of this class, Animal and Play. Then I compare all the method signature from Dog and Animal/Play and see which one implements what. But this approach seems a little naive, and may not be very efficient. So please let me know if there is any good approach that I can use to obtain the method inheritance info.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

请止步禁区 2024-11-07 06:29:21

您可以使用 java Reflection 来查找超类。

You can use java Reflection to find out the superclass.

翻了热茶 2024-11-07 06:29:21

我认为您概述的方法是实现这一目标的唯一方法。我对此可能是错误的,但了解哪些方法是从给定接口/类继承的唯一方法是检查接口,因此,您必须在某一时刻搜索给定接口以查找给定方法。我想象 Eclipse 基于继承结构构建一个方法列表,当您将鼠标悬停在给定类中的方法上时,会查阅该表以查看它来自哪个类/接口。最后一部分只是猜测。

I think the approach you've outlined is the only way to do it. I could be wrong about this, but the only way to know which methods are inherited from a given interface/class is to examine the interface and consequently, you must search a given interface for a given method at one point or another. I would imagine that eclipse builds a list of methods based on the inheritance structure and when you hover over a method in a given class, consults that table to see which class/interface it came from. That last part is merely a guess though.

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