简体   繁体   English

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

[英]How can I avoid a name conflict between a class and a package in Java?

I have two projects, Main and Core . 我有两个项目, MainCore I have a package called com.package.Sample (along with its contents) and a class called Sample in package com.package . 我有一个名为com.package.Sample的包(及其内容)和一个名为Sample的包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. 如果我要在Main包含两者,我会遇到一个无法解决的错误 - 在我的实际情况中,它是Sample类无法解析。 We have ant scripts that builds and that has always worked. 我们有构建的ant脚本,并且一直有效。

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 . 但是,仔细检查每个项目所需的内容,我注意到我可以将软件包com.package.SampleMain依赖项一起移动到Core中。 The Sample class technically belonged inside of Main so I did not move it. Sample类技术上属于Main内部所以我没有移动它。 This allowed me to build successfully within Eclipse. 这使我能够在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. 在检查冲突之前,我认为最好的解决方案是重新计算Sample类。
  • If however, I could not move the package com.package.Sample out of Main . 但是,如果我无法将包com.package.Sample移出Main What should I do? 我该怎么办? Or is this a case of it will never build? 或者这是一个永远不会建立的情况?

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). 如果代码库被设计为导致名称冲突(特别是在Java中,这使得解决这样的问题变得容易),大型项目可能永远不会变大。
  • I wouldn't refactor the Sample class if the name is appropriately descriptive. 如果名称具有适当的描述性,我不会重构Sample类。 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. 我从来没有在我的回忆制作中看到过在包名中使用任何大写字母的Java代码(商业或开源)。 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. 由于Java中的约定是使用Pascal case或camel case或者你想要调用它的任何东西,如果遵循该约定,你将永远不会有类/包名称冲突。
  • 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. 由于您使用的是Eclipse,因此您应该能够在资源管理器中右键单击该软件包,选择“重构”,将名称更改为小写,并完成大量工作。

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. 遵循这种做法可确保您描述的问题永远不会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM