Java:使用包的约定?

发布于 2024-10-09 13:56:30 字数 255 浏览 0 评论 0原文

我有 PHP 背景,由于过去缺乏命名空间,我使用了 Zend Framework 风格的“包”。例如,如果我有一个抽象类 Player 和子类 Player_Human、Player_DumbComputer、Player_Minimax 等。我会将 Player 放在主目录中,并将其子类放在目录 /Player/ 中。它试图在 Java 中做类似的事情,但我遇到了名称冲突——我有一个包 blah.Player 和一个类 blah.Player。我该如何避免这种情况?这方面的常见做法是什么?

I am coming from a PHP background, and due to the lack of namespaces in the past I have used Zend Framework style "packages". For example, if I have an abstract class Player and children Player_Human, Player_DumbComputer, Player_Minimax, etc. I would put Player in the main directory and put it's children in a directory /Player/. It tried to do something similar in Java, but I got a name-clash -- I have both a package blah.Player and a class blah.Player. How do I avoid this? What is the common practice regarding this?

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

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

发布评论

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

评论(3

聚集的泪 2024-10-16 13:56:30

来自JavaTM编程语言的代码约定

包命名约定:

唯一包名称的前缀始终以全小写 ASCII 字母书写,并且应该是顶级域名之一,目前为 com、edu、gov、mil、net、org 或英文两者之一-ISO 标准 3166, 1981 中指定的用于标识国家/地区的字母代码。

包名称的后续组成部分根据组织自己的内部命名约定而有所不同。此类约定可能指定某些目录名称组件是部门、部门、项目、计算机或登录名称。

类命名约定:

类名应该是名词,混合大小写,每个内部单词的第一个字母大写。尽量保持类名简单且具有描述性。使用整个单词 - 避免使用首字母缩略词和缩写词(除非缩写词比长形式使用更广泛,例如 URL 或 HTML)。

From Code Conventions for the JavaTM Programming Language:

Package naming conventions:

The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.

Subsequent components of the package name vary according to an organization's own internal naming conventions. Such conventions might specify that certain directory name components be division, department, project, machine, or login names.

Class naming conventions:

Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

生活了然无味 2024-10-16 13:56:30

Java 包名称始终为小写字母,而类名称始终以大写字母开头,并且是驼峰式名称(无下划线)。所以你的结构应该是:

- blah
|- Player.java
|- player
  |- HumanPlayer.java
  |- DumbComputerPlayer.java
  |- MinimaxPlayer.java

Java Package names are always in lower case while the Class names always start with an upper case letter and are camel case names (no underscores). So your structure should be:

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