Java 如何找到在 Eclipse 中实现抽象方法的位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18916185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:18:38  来源:igfitidea点击:

How to find where an abstract method is implemented in Eclipse?

javaeclipsehierarchyabstract

提问by ZelelB

I would like to find where an abstract method is implemented, in which class? Is there a link in Eclipse like call hierarchy/open declarationor something like that that shows where the method implemented is?

我想找到抽象方法的实现位置,在哪个类中?Eclipse 中是否有一个链接,比如调用层次结构/打开声明或类似的东西,显示了实现的方法在哪里?

采纳答案by Konstantin Yovkov

Quick methods:

快速方法:

  • Hold Ctrl, hover over the method name, and select "Open Implementation".

  • Click on the method name and press CtrlT.

  • Right-click on the method name → "Quick Type Hierarchy".

  • 按住Ctrl,将鼠标悬停在方法名称上,然后选择“打开实现”。

  • 单击方法名称并按CtrlT

  • 右键单击方法名称→“快速类型层次结构”。

For more navigation power, see the post by ADTC.

有关更多导航功能,请参阅ADTC 的帖子

回答by Adam Arold

If you Ctrl + mouse hoverover a method you can see a popup where you can choose from an array of options and one of them is "Open Implementation". If you click you will be presented with a list of all implementations.

如果您Ctrl + mouse hover通过一个方法,您可以看到一个弹出窗口,您可以在其中从一系列选项中进行选择,其中一个是“开放实现”。如果单击,您将看到所有实现的列表。

It can also be used to see the declaration and/or super implementation, so it is pretty useful.

它还可用于查看声明和/或超级实现,因此非常有用。

If you have the cursor over the method you can simply press Ctrl + Tfor the same effect.

如果您将光标悬停在方法上,您只需按下即可Ctrl + T获得相同的效果。

If you want to display the results in the "Type Hierarchy" view use the F4key.

如果要在“类型层次结构”视图中显示结果,请使用F4键。

回答by Alex

You can use the TypeHierarchy view for this. Here's an example of the type hierarchy view for the Spring framework abstract class FrameworkServlet:

您可以为此使用 TypeHierarchy 视图。以下是 Spring 框架抽象类 FrameworkServlet 的类型层次结构视图示例:

enter image description here

在此处输入图片说明

In the left pane you see the class inheritance tree. The 'A' icon next to the class name in the left pane indicates that FrameworkServlet is an abstract class. In the right pane, the highlighted method doService( request, response ) also has the 'A' icon, indicating that this is an abstract method.

在左窗格中,您会看到类继承树。左窗格中类名旁边的“A”图标表示 FrameworkServlet 是一个抽象类。在右侧窗格中,突出显示的方法 doService( request, response ) 也有“A”图标,表明这是一个抽象方法。

Now if we click on the class DispatcherServlet, which is a concrete (not abstract) class, you see this:

现在,如果我们单击类 DispatcherServlet,它是一个具体(非抽象)类,您会看到:

enter image description here

在此处输入图片说明

In this case, the DispatcherServlet's doService method does not have the 'A' icon and instead has the up triangle, indicating that it overrides a superclass method.

在这种情况下,DispatcherServlet 的 doService 方法没有“A”图标,而是具有向上三角形,表明它覆盖了超类方法。

回答by ADTC

Great answers here!I found an additional trick to quickly navigate from implementation to implementation.

很棒的答案在这里!我发现了一个额外的技巧来快速从实现导航到实现。

It is rather strange that Eclipse uses a temporary pop-up to show "Types implementing or defining 'Class.method()'"instead of a permanent view that can be pinned to the sides.

Eclipse 使用临时弹出窗口来显示“实现或定义'Class.method()'的类型”而不是可以固定在两侧的永久视图,这很奇怪。

The advantage of this pop-up is, when you click on an implementing type, you are taken directlyto the method implementation. The disadvantageof this pop-up is that it closes as soon as you do so, and there is no way to pin it permanently.

此弹出窗口的优点是,当您单击实现类型时,您会直接进入方法实现。此弹出窗口的缺点是它会在您执行此操作后立即关闭,并且无法永久固定。

The Type Hierarchyshows the same list of implementing types (as they are subclasses of the abstract class). Since it is a view, it can be pinned permanently on the side. However,if you double-click on a type name here, you are taken to the declaration line of the type. You then have to find the implementation of the abstract method yourself.

类型层次显示实现类型相同的列表(因为它们是抽象类的子类)。由于它是一个视图,它可以永久固定在侧面。但是,如果在此处双击类型名称,则会转到类型的声明行。然后你必须自己找到抽象方法的实现。

Except, you don't have to, with the following trick:

除了,您不必使用以下技巧:

  1. Right-click on the abstract method and click Open Type Hierarchy.1In the Type Hierarchyview, you will notice the following:
    • All the subtypes of the abstract class type are listed in the primary list (P).2
    • All the members of the abstract class are listed in the secondary list (S).3
    • The abstract method you right-clicked on is selected in the secondary list.4

  2. Click on the Lock View and Show Members in Hierarchybutton (1)above the secondary list.
    • The selected method and its implementations will be listed in the primary list under each of the subtypes as well as the parent type.
  1. 右键单击抽象方法,然后单击Open Type Hierarchy1类型层次结构视图中,您会注意到以下内容:
    • 抽象类类型的所有子类型都列在主列表(P) 中2
    • 抽象类的所有成员都列在辅助列表(S) 中3
    • 在二级列表中选择了您右键单击的抽象方法。4

  2. 单击二级列表上方的锁定视图并在层次结构中显示成员按钮(1)
    • 所选方法及其实现将列在每个子类型以及父类型下的主要列表中。

You can now quickly navigate between implementations by clicking or double-clicking on the methods listed in the primary list.

您现在可以通过单击或双击主列表中列出的方法在实现之间快速导航。

Screenshot indicating various parts mentioned

屏幕截图显示了提到的各个部分

1You can also click on the method name and press F4.
2If they are not listed, click either one of the Show the Type Hierarchy(2)or Show the Subtype Hierarchy(3)buttons above the list.
3If there's no secondary list, click on the View Menu(4), choose Layout> [any option other than Hierarchy Only].
4If it is not selected, just click on it to select it.

1您也可以单击方法名称并按 F4。
2如果未列出,请单击列表上方的显示类型层次结构(2)显示子类型层次结构(3)按钮之一。
3如果没有二级列表,点击查看菜单(4),选择布局> [除Hierarchy Only之外的任何选项]。
4如果没有选中,直接点击选中即可。