Java中如何避免类和包之间的名称冲突?

发布于 2024-12-02 02:53:30 字数 675 浏览 0 评论 0原文

我有两个项目,MainCore。我有一个名为 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 of Main. What should I do? Or is this a case of it will never build?

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

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

发布评论

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

评论(2

风吹过旳痕迹 2024-12-09 02:53:30

分别回答你的问题:

  • 我不知道这在大型项目中是否常见,我认为不会。如果代码库被设计为导致名称冲突(特别是在 Java 中,这使得解决这样的问题变得很容易),那么大型项目可能永远不会变得很大。
  • 如果名称具有适当的描述性,我不会重构 Sample 类。然而,我会用全部小写字母来命名我的包(和子包)。您暗示这是一个小项目,所以我猜您可以控制命名。在我的记忆中,我从未见过在包名称中使用任何大写字母的 Java 代码(商业或开源)。由于 Java 中的约定是使用 Pascal 大小写或驼峰大小写或任何您想要的名称,因此如果遵循该约定,您将永远不会遇到类/包名称冲突。
  • 当然会建立。很容易修复。由于您使用的是 Eclipse,因此您应该能够在资源管理器中右键单击该包,选择“重构”,将名称更改为小写,然后就基本完成了。

祝你好运!

To answer your bullets, respectively:

  • I don't know if this is common in large projects, I would assume not. Large projects would probably never become large if the codebase was designed to cause name conflicts (especially in Java, which makes solving problems like this easy).
  • I wouldn't refactor the Sample class if the name is appropriately descriptive. I would however, name my packages (and subpackages) with all lowercase letters. You implied that this is a smallish project, so I'm guessing you have control over naming. I have never seen in my recollection production Java code (commercial or open source) that used any capitals in a package name. Since the convention in Java is to use Pascal case or camel case or whatever you want to call it, if you follow that convention you will never have a class/package name conflict.
  • Of course it will build. Pretty easy fix. Since you are using Eclipse, you should be able to right-click the package in the explorer, choose "refactor", change the name to lowercase, and be pretty much done.

Good luck!

难如初 2024-12-09 02:53:30

包名应该全部小写,类名应该以大写开头。遵循此做法可确保永远不会发生您所描述的问题。

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.

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