如何在 Eclipse 上的包中制作子包?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25378734/
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
How to make child packages in packages on Eclipse?
提问by Permanently
I have recently started coding on Eclipse for Minecraft Bukkit plugins, and I have been going fine with it. Recently, I've been trying to make a child package into a package, but if I try New > Package, then it makes a package separate to my main package. I am a beginner, so please make instructions simple. Is there anything else I can try?
我最近开始在 Eclipse 上为 Minecraft Bukkit 插件编写代码,而且我一直在使用它。最近,我一直在尝试将子包制作成一个包,但是如果我尝试New > Package,那么它会将一个包与我的主包分开。我是初学者,所以请使说明简单。还有什么我可以尝试的吗?
回答by NoDataFound
Normally, if you separate each package component with dots (.
), it should create intermediate entries.
通常,如果您用点 ( .
)分隔每个包组件,它应该创建中间条目。
Try create a new package with the following test case:
尝试使用以下测试用例创建一个新包:
com.foobar.example
com.foobar.test
com.foobar.example.a
com.foobar.example
com.foobar.test
com.foobar.example.a
And Eclipse should do the tricks.
Eclipse 应该可以解决问题。
If however, you are stuck with finding those empty packages, perhaps you should simply play with the Package Exploreroptions: the Java filtersis especially useful, and there might be an option enabled by default which would filter/hide empty package.
但是,如果您一直在寻找那些空包,也许您应该简单地使用包资源管理器选项:Java 过滤器特别有用,并且可能有一个默认启用的选项可以过滤/隐藏空包。
You also have Package presentations.
您也有包演示文稿。
Since an image is better than long lines of text:
由于图像优于长行文本:
- Click the arrow to open the presentations options (see the Package Explorer on the left)
- The Filters optionsdoes have filters which might hide empty parent packages.
- The hierarchical package presentation behave like the Explorer left pane.
- 单击箭头打开演示选项(请参阅左侧的包资源管理器)
- 该过滤器的选项确实有可能隐藏空父包过滤器。
- 分层包表示的行为类似于资源管理器左窗格。
回答by Narmer
When creating a new package you can create a hierarchy by simply separating your parent folder with child folder with a dot '.'
. So if you want to create a sub package of foo.bar
you have to create a package with name foo.bar.myfoo
. Eclipse will do the rest.
创建新包时,您可以通过简单地用点分隔父文件夹和子文件夹来创建层次结构'.'
。所以如果你想创建一个子包,foo.bar
你必须创建一个 name 的包foo.bar.myfoo
。Eclipse 将完成剩下的工作。
If you instead want to visualize the folder type hierachy as here:
如果您想将文件夹类型层次结构可视化,如下所示:
you can click in the little arrow top left of the project explorer view menù and go to Package Presentation > Hierarchical.
您可以单击项目资源管理器视图菜单左上角的小箭头,然后转到Package Presentation > Hierarchical。
回答by Kai
A folder-like hierachy is implicitly created by the package names in Java. Lets say you have a package com.company
and you want a sub-package in that package. Then you have to create a package with the name com.company.product
. It is notdone by creating a package with the name product
while com.company
is selected!
Java 中的包名称隐式地创建了类似文件夹的层次结构。假设您有一个包,com.company
并且您想要该包中的一个子包。然后你必须创建一个名为 的包com.company.product
。这不是通过创建一个名为product
while的包来完成的com.company
!
回答by user1821961
For eclipse Version: 2019-03(4.11.0) Build id: 20190314-1200
对于 Eclipse版本:2019-03(4.11.0) 构建 ID:20190314-1200
I found it easier to create the desired package path along with the new class I wanted to exist at the new package path.
我发现创建所需的包路径以及我希望在新包路径中存在的新类更容易。
From within the Project Explorerview, right click on the corresponding project to create a New Class.
在Project Explorer视图中,右键单击相应的项目以创建一个新类。
Then on the class creation popup I fill in the corresponding package field as I please to build out the new path as desired, along with the new class to sit in that new path.
然后在类创建弹出窗口中,我根据需要填写相应的包字段,以根据需要构建新路径,以及位于新路径中的新类。