gpt4 book ai didi

Java包命名。下划线 : A special case

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:12:28 26 4
gpt4 key购买 nike

今天我在项目中命名一个包,其中包含与称为“访问结构”的概念相关的代码。

现在,将这个包命名为“com.myemployer.project.component.accessstructures”似乎没有吸引力并且因为三个“S”而难以阅读。 (更高级别的包实际上未命名为“项目”和“组件”)。

我很想使用“...component.access_structures”

我在 Oracle's site 上找不到 Java 约定中提到的任何内容| .简短的网络搜索一无所获。

这样的名称的官方惯例是什么?

最佳答案

来自 Oracle Docs

Package names are written in all lower case to avoid conflict with the names of classes or interfaces.

Companies use their reversed Internet domain name to begin their package names—for example, com.example.mypackage for a package named mypackage created by a programmer at example.com.

Name collisions that occur within a single company need to be handled by convention within that company, perhaps by including the region or the project name after the company name (for example, com.example.region.mypackage).

In some cases, the internet domain name may not be a valid package name. This can occur if the domain name contains a hyphen or other special character, if the package name begins with a digit or other character that is illegal to use as the beginning of a Java name, or if the package name contains a reserved Java keyword, such as "int". In this event, the suggested convention is to add an underscore

虽然这篇文章没有具体说明你的具体情况,但它确实说对于无效的包名称我们应该使用下划线。有人可能会争辩说 accessStructures 是我们在 Java 中定义方法的方式,因此命名这样的包可能会造成混淆。总的来说,这完全取决于您。

如果你想遵守这个约定,我相信你应该给你的包命名:

com.myemployer.project.component.access_structures

您还可以查找同义词并找到可以减少混淆的替代词。我很快找到了一些:

  • accessframework
  • accessfactory
  • accessarch (accessarchitecture)
  • accessconstructs

关于Java包命名。下划线 : A special case,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25751044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com