包/命名空间和类具有相同的名称是一个好习惯吗?

发布于 2024-10-18 05:18:33 字数 593 浏览 1 评论 0原文

我正在创建一个新的命名空间,其中一个类的最合适名称似乎与命名空间同名。这是一个好的做法吗?如果不是,还有什么替代方案?

例如:

com.person
|--- Person.(java/cs)
|--- PersonDetailChecker.(java/cs)
|--- PersonNameGenerator.(java/cs)

讨论同一问题的相关问题:

I am creating a new namespace and the most apt name for one of the class seems to be the same name as the namespace. Is this a good practice? If not, what is the alternative?

For example:

com.person
|--- Person.(java/cs)
|--- PersonDetailChecker.(java/cs)
|--- PersonNameGenerator.(java/cs)

Related questions discussing the same issue:

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

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

发布评论

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

评论(5

盛夏尉蓝 2024-10-25 05:18:33

我会尽量避免这种情况,因为它会让你的代码更难阅读
Eric Lippert 写了一篇关于此问题的文章,您可以在这里找到:

我自己也犯过几次这样的错误,这确实让我的一些代码变得更难阅读。

I would try to avoid that, since it can make your code harder to read
Eric Lippert has written an article about this, which you can find here:

I've made the mistake myself a few times, and it certainly made it harder to read some of my code.

感情旳空白 2024-10-25 05:18:33

在Java中做到这一点是没有问题的。

但这不是一个好的实践:因为好的实践意味着你应该这样做。 (这显然不是事实。)。

顺便提一句。在 Java 中,类名应该以大写字母开头,包应该只包含小写字母,因为它们永远不会真正相同——这是一个很好的做法。

已添加
经过一段时间的重新细化,我认为如果包和类具有相同的名称,则表明架构失败。理由是:任何事物都应该有其独特的存在理由,并且其名称应该表明理由。因此,如果你有一个包和一个同名的类,那么它存在的原因不是唯一的,或者它的名称是错误的。

It is no problem to do this in Java.

But it is NOT a good practice: because a good practice would mean, that you should do it. (And that is clearly not the fact.).

BTW. In Java, the class name should start with an upper case letter, and packages should only contain lower case, to they are never realy the same -- this is good practice.

Added
After rethining the for a while I think it is an indication of an Architectural failure if the package and class have the same name. The reason is: that everything should have its own unique reason to exist, and its name should indicate the reasons. So if you have a package and a class with the same name, then its reason of exisiting is not unique, or its names are bad.

要走就滚别墨迹 2024-10-25 05:18:33

在 Java 中,一个简单的事实是,你的包名应该全部小写,类名应该以大写字母开头,这样可以保证它们不会发生冲突。如果它们相同,那么您一定违反了这些准则之一,这是一个比您的特定准则更基本的风格问题。

In Java the simple fact that your package name should be all lowercase and your class name should begin with an uppercase letter guarantee that they won't clash. If they are the same, you must have violated one of these guidelines, which is a more fundamental stylistic problem than your specific one.

一袭水袖舞倾城 2024-10-25 05:18:33

只要情况不同,就没有问题。然而,一旦您对命名空间和对象使用相同的标识符,您最终就必须完全限定对该对象的引用(这使得代码相当冗长且丑陋)。

As long as the case is different, no problem. However, as soon as you use the same exact identifier for a namespace and an object, you end up having to fully qualify your references to that object (which makes the code rather verbose and ugly IMHO).

隔岸观火 2024-10-25 05:18:33

我会不遗余力地使类名在合理的范围内唯一且不言自明,无论包如何。一个只有一个单词的类名是相当令人困惑的,除非它是一个众所周知的、普遍接受的名称,对于一个共同的概念来说。

嵌套类有点位于封闭类的名称空间中。人们可能会这样做

class Http
    class Request
        enum Method
    class Response
        class Code

Http.Request.Method.GET
new Http.Response.Code(123); 

虽然我发现这很容易阅读,但我仍然坚持旧的风格

class Http
class HttpRequest
enum  HttpRequestMethod
class HttpResponse
class HttpResponseCode

HttpRequestMethod.GET
new HttpResponseCode(123); 

I would go out of my way to make class names unique, and self explanatory, within a reasonable scope, regardless of package. A class name with one word is quite confusing, unless it's a well known, generally accepted name, for a common concept.

A nested class is kind of in the name space of the enclosing class. People may do this

class Http
    class Request
        enum Method
    class Response
        class Code

Http.Request.Method.GET
new Http.Response.Code(123); 

While I find this quite readable, I'd still stick to the old style

class Http
class HttpRequest
enum  HttpRequestMethod
class HttpResponse
class HttpResponseCode

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