How to Find and Open a Class with Eclipse – 如何用Eclipse查找和打开一个类

最后修改: 2018年 3月 20日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

In this article, we’re going to take a look at a number of ways to find a class in Eclipse. All the examples are based on Eclipse Oxygen.

在这篇文章中,我们要看一下在Eclipse中寻找类的一些方法。所有的例子都是基于Eclipse Oxygen

2. Overview

2.概述

In Eclipse, we often need to look for a class or an interface. We have many ways to do that:

在Eclipse中,我们经常需要寻找一个类或一个接口。我们有很多方法可以做到这一点。

  • The Open Type dialog
  • The Open Resource dialog
  • The Package Explorer view
  • The Open Declaration function
  • The Type Hierarchy view

3. Open Type

3.开放式

One of the most powerful ways to do this is with the Open Type dialog.

做到这一点的最有力的方法之一是使用 “打开类型 “对话框。

3.1. Accessing the Tool

3.1.访问该工具

We can access it in three ways:

我们可以通过三种方式访问它。

  1. Using the keyboard shortcut, which is Ctrl + Shift + T on a PC or Cmd + Shift + T on a Mac.
  2. Opening the menu under Navigate > Open Type
  3. Clicking on the icon in the main toolbar:

Senzanome-1

3.2. Using It to Find a Class

3.2.用它来寻找一个类别

Once we have Open Type up, we simply need to start typing, and we’ll see results:

一旦我们有了Open Type,我们只需要开始打字,就能看到结果。

Senzanome-2

The results will contain classes in the build path of our open projects, which includes project classes, libraries, and the JRE itself.

结果将包含我们开放项目的构建路径中的类,其中包括项目类、库和JRE本身。

In addition, it shows the package and its location in our environment.

此外,它还显示了包和它在我们环境中的位置。

As we can see in the image, the results are any classes whose name starts with what we typed. This type of search is not case sensitive.

正如我们在图片中看到的,结果是任何名称以我们输入的内容开头的类。这种类型的搜索是不分大小写的。

We can search in camel case too. For example, to find the class ArraysParallelSortHelpers we could just type APSH or ArrayPSH. This type of search is case sensitive.

我们也可以用骆驼的大小写搜索。例如,要找到ArraysParallelSortHelpers类,我们可以直接输入APSHArrayPSH。这种类型的搜索是区分大小写的。

In addition, it’s also possible to use the wildcard characters “*” or “?” in the search text. “*” is for any string, including the empty string and “?” for any character, excluding the empty string.

此外,还可以在搜索文本中使用通配符“*”或”?”。”*”用于任何字符串,包括空字符串;”? “用于任何字符,不包括空字符串。

So, for example, let’s say we would like to find a class that we remember contains Linked, and then something else, and then Multi. “*” comes in handy:

因此,举例来说,假设我们想找到一个我们记得包含Linked,的类,然后是其他东西,然后是Multi。“*”就派上用场了。

Senzanome-3

Or if we add a “?”:

或者如果我们添加一个”?”。

Senzanome-4

The “?” here excludes the empty string so LinkedMultiValueMap is removed from the results.

这里的”?”排除了空字符串,所以LinkedMultiValueMap被从结果中删除。

Note also that there is an implicit “*” at the end of every input, but not at the beginning.

还要注意的是,在每个输入的末尾都有一个隐含的 “*”,而不是在开头。

4. Open Resource

4.开放资源

Another simple way to find and open a class in Eclipse is Open Resource.

在Eclipse中找到并打开一个类的另一个简单方法是Open Resource

4.1. Accessing the Tool

4.1.访问该工具

We can access it in two ways:

我们可以通过两种方式访问它。

  • Using the keyboard shortcut, which is Ctrl + Shift + R on a PC or Cmd + Shift + R on a Mac.
  • Opening the menu under Navigate > Open Resource

4.2. Using It to Find a Class

4.2.用它来寻找一个类别

Once we have the dialog up, we simply need to start typing, and we’ll see results:

一旦我们有了这个对话框,我们只需要开始输入,就能看到结果。

Senzanome-9

The results will contain classes as well as all other files in the build path of our open projects.

结果将包含类,以及我们打开的项目的构建路径中的所有其他文件。

For usage details about wildcards and camel case search, check out the Open Type section above.

关于通配符和骆驼字母搜索的使用细节,请查看上面的开放类型部分。

5. Package Explorer

5.软件包资源管理器

When we know the package to which our class belongs, we can use Package Explorer.

当我们知道我们的类属于哪个包时,我们可以使用包管理器

5.1. Accessing the Tool

5.1.访问该工具

If it isn’t already visible, then we can open this Eclipse view through the menu under Window > Show View > Package Explorer.

如果它还不可见,那么我们可以通过Window > Show View > Package Explorer下的菜单打开这个Eclipse视图。

5.2. Using the Tool to Find a Class

5.2.使用该工具来寻找一个班级

Here the classes are displayed in alphabetical order:

这里的班级是按字母顺序显示的。

Senzanome-5

If the list is very long, we can use a trick: we click anywhere on the package tree and then we start typing the name of the class. We’ll see the selection scrolling automatically among the classes until it matches our class.

如果列表非常长,我们可以使用一个技巧:我们点击包树上的任何地方,然后开始输入类的名字。我们会看到选择在类中自动滚动,直到与我们的类相匹配。

There’s also the Navigator view, which works nearly the same way.

还有一个Navigator视图,其工作方式几乎相同。

The main difference is that while Package Explorer shows classes relative to packages, Navigator shows classes relative to the underlying file system.

主要区别在于,Package Explorer显示相对于包的类,而Navigator显示相对于底层文件系统的类。

To open this view, we can find it in the menu under Window > Show View > Navigator.

要打开这个视图,我们可以在Window > Show View > Navigator下的菜单中找到它。

6. Open Declaration

6.公开声明

In the case where we’re looking at code that references our class, Open Declaration is a very quick way to jump to it.

在我们查看引用我们的类的代码的情况下,Open Declaration是一个非常快速的跳转到它的方法。

6.1. Accessing the Tool

6.1.访问该工具

There are three ways to access this function:

有三种方法可以访问这个功能。

  1. Clicking anywhere on the class name that we want to open and pressing F3
  2. Clicking anywhere on the class name and going to the menu under Navigate > Open Declaration
  3. While keeping the Ctrl button pressed, mousing over the class name and then just clicking on it

6.2. Using It to Find a Class

6.2.用它来寻找一个类别

Thinking about the screenshot below, if we press Ctrl and hover over ModelMap, then a link appears:

想想下面的截图,如果我们按Ctrl并将鼠标悬停在ModelMap上,那么会出现一个链接。

Senzanome-6

Notice that the color changed to light blue and it became underlined. This indicates that it is now available as a direct link to the class. If we click the link, Eclipse will open ModelMap in the editor.

请注意,颜色变成了浅蓝色,而且变成了下划线。这表明它现在可以作为类的直接链接。如果我们单击该链接,Eclipse 将在编辑器中打开 ModelMap

7. Type Hierarchy

7.类型层次结构

In an object-oriented language like Java, we can also think about types relative to their hierarchy of super- and sub classes.

在像Java这样的面向对象的语言中,我们也可以相对于超类和子类的层次结构来考虑类型。

Type Hierarchy is a view similar to Package Explorer and Navigator, this time focused on hierarchy.

Type Hierarchy是一个类似于Package ExplorerNavigator的视图,这次重点是层次结构。

7.1. Accessing the Tool

7.1.访问该工具

We can access this view in three ways:

我们可以通过三种方式访问这个视图。

  1. Clicking anywhere in a class name and then pressing F4
  2. Clicking anywhere in a class name and going to the menu under Navigate > Open Type Hierarchy
  3. Using the Open Type in Hierarchy dialog

The Open Type in Hierarchy dialog behaves just like Open Type we saw in section 3.

在层次结构中打开类型对话框的行为就像我们在第3节看到的打开类型

To get there, we go to the menu under Navigate > Open Type in Hierarchy or we use the shortcut: Ctrl + Shift + H on a PC or Cmd + Shift + H on a Mac.

要到达那里,我们去到Navigate > Open Type in Hierarchy 下的菜单,或者我们使用快捷键。Ctrl + Shift + H在PC上或Cmd + Shift + H在Mac上。

Senzanome

This dialog is similar to the Open Type dialog. Except for this time when we click on a class, then we get the Type Hierarchy view.

这个对话框类似于打开类型对话框。除了这次我们点击一个类,然后我们得到类型层次视图。

7.2. Using the Tool to Find a Class

7.2.使用该工具来寻找一个类

Once we know a superclass or subclass of the class we want to open, we can navigate through the hierarchy tree, and look for the class there:

一旦我们知道了我们想要打开的类的超类或子类,我们就可以在层次结构树中导航,并在那里寻找该类。

Senzanome-7

If the list is very long, we can use the same trick we used with Package Explorer: we click anywhere on the tree and then we start typing the name of the class. We’ll see the selection scrolling automatically among the classes until it matches our class.

如果列表非常长,我们可以使用与包资源管理器相同的技巧:我们点击树上的任何地方,然后我们开始输入类的名称。我们会看到选择在类中自动滚动,直到与我们的类相匹配。

8. Conclusion

8.结论

In this article, we looked at the most common ways to find and open a Java class with the Eclipse IDE including Open Type, Open Resource, Package Explorer, Open Declaration, and Type Hierarchy.

在这篇文章中,我们看了用Eclipse IDE查找和打开Java类的最常见的方法,包括Open Type,Open Resource, Package Explorer, Open Declaration, 以及Type Hierarchy