Java中如何避免类和包之间的名称冲突?
我有两个项目,Main
和 Core
。我有一个名为 com.package.Sample
的包(及其内容)和一个名为 Sample
的类,位于包 com.package
中。如果我将两者都包含在 Main
中,我会遇到无法解析的错误 - 在我的实际情况中,是无法解析 Sample
类。我们有可以构建并且一直有效的 ant 脚本。
然而,仔细检查每个项目的需求,我注意到我可以将包 com.package.Sample
及其位于 中的依赖项移动到
。从技术上讲,Core
中主要Sample
类属于 Main
内部,因此我没有移动它。这使我能够在 Eclipse 中成功构建。
我想知道一些事情:
- 这在大型项目中常见吗?
- 对于这种情况,最好的方法是什么?在检查冲突之前,我认为最好的解决方案是重构
Sample
类。 - 但是,如果我无法将包
com.package.Sample
移出Main
。我应该怎么办?或者这是一个永远不会建立的案例?
I have two projects, Main
and Core
. I have a package called com.package.Sample
(along with its contents) and a class called Sample
in package com.package
. If I were to include both in Main
, I would run into an error with one being unable to resolve - in my actual case it was the Sample
class that could not be resolved. We have ant scripts that builds and that has always worked.
However, carefully examining what was required of each project, I noticed that I could move the package com.package.Sample
into Core
along with its dependencies that were in Main
. The Sample
class technically belonged inside of Main
so I did not move it. This allowed me to build successfully within Eclipse.
A couple of things I'm wondering about:
- Is this common in large projects?
- What is the best approach for this kind of situation? Prior to examining the conflict, I thought the best solution was to re-factor the
Sample
class. - If however, I could not move the package
com.package.Sample
out ofMain
. What should I do? Or is this a case of it will never build?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
分别回答你的问题:
祝你好运!
To answer your bullets, respectively:
Good luck!
包名应该全部小写,类名应该以大写开头。遵循此做法可确保永远不会发生您所描述的问题。
Package names should be all lowercase, class names should start with a capital. Following this practice makes sure that problems like you describe never occur.