有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (3) 个答案

  1. # 1 楼答案

    就我个人而言,我会把它作为iOSCompatProxy。我认为世界上的每一条规则都有例外。:)

  2. # 2 楼答案

    对于这种类型的类名,没有普遍遵循的约定。即使是像“XML”这样的常规大写缩写也会导致不一致;查看the official JDK class list,我看到名为XMLSomething的类和XmlSomething的类一样多

    谷歌在Google Java Style Guide中处理这个话题,他们给出了一个将文本转换为类名的方案:

    Sometimes there is more than one reasonable way to convert an English phrase into camel case, such as when acronyms or unusual constructs like "IPv6" or "iOS" are present. To improve predictability, Google Style specifies the following (nearly) deterministic scheme.

    Beginning with the prose form of the name:

    1. Convert the phrase to plain ASCII and remove any apostrophes. For example, "Müller's algorithm" might become "Muellers algorithm".

    2. Divide this result into words, splitting on spaces and any remaining punctuation (typically hyphens).

      • Recommended: if any word already has a conventional camel-case appearance in common usage, split this into its constituent parts (e.g., "AdWords" becomes "ad words"). Note that a word such as "iOS" is not really in camel case per se; it defies any convention, so this recommendation does not apply.
    3. Now lowercase everything (including acronyms), then uppercase only the first character of:

      • ... each word, to yield upper camel case, or
      • ... each word except the first, to yield lower camel case
    4. Finally, join all the words into a single identifier.

    他们的方案将IosCompatProxy作为类名,将iosCompatProxy作为变量名。他们故意忽略了最初的大写字母,而采用了更基于规则的驼峰案例形式

    也许这不是最漂亮的表格,但如果你想找一个规则来遵循,并且在你工作的地方没有一个规则,谷歌的风格指南就是一个很好的规则,因为它特别提到了“iOS”。我倾向于高度重视他们的约定,因为他们使用这些约定维护了大量Java(截至2018年[1]有3亿行)

  3. # 3 楼答案

    一般来说,在Java类名中,新单词开头的第一个字母和所有后续字母都是大写的,例如ThisIsAClass。这是为了将它们与变量名和方法名区分开来。因此,根据我对Oracle风格指南的理解,我认为IOSCompatProxy将是合适的类名,这是这里看到的通用Java风格指南(我不认为自上次更新以来有任何重大的风格变化):

    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).

    此外,由于iOS是一个比“互联网和操作系统”更广泛使用的首字母缩略词,我想说的是保持整个首字母缩略词大写。如果你有不同的解释,比如与@Boann所写内容相匹配的解释,请使用它

    然而,我不鼓励你遵循谷歌的风格指南,除非你为谷歌工作(假设谷歌的风格指南基于甲骨文的风格指南,一般语法遵循可能是可以的),只是因为他们的风格可能与你未来工作的公司不同。相反,我建议您遵循以下Oracle风格指南:https://www.oracle.com/technetwork/java/codeconvtoc-136057.html