Naming conventions are used to make Java programs more read‐ able. It is important to use meaningful and unambiguous names comprised of Java letters.
使用命名约定可以使java程序变得更具可读性。使用有意义且不模棱两可的名字是很重要的。
Class names should be nouns, as they represent “things” or “objects.” They should be mixed case (camel case) with only the first letter of each word capitalized, as in the following:
类的名字应该是名词形式的,因为它代表着事物或对象。使用混合命名(驼峰式大小写),也就是说每个单词的首字母应该大写。如下所示:
public class Fish {...}
Interface names should be adjectives. They should end with “able” or “ible” whenever the interface provides a capability; otherwise, they should be nouns. Interface names follow the same capitali‐ zation convention as class names:
接口,当接口提供了某个功能时,接口应该命名为形容词形式,并以able或ible结尾。否则则命名为名词形式,且与名词的命名规则相同。
public interface Serializable {...} public interface SystemPanel {...}
Method names should contain a verb, as they are used to make an object take action. They should be mixed case, beginning with a lowercase letter, and the first letter of each subsequent word should be capitalized. Adjectives and nouns may be included in method names:
方法名应该包含一个动词,毕竟他们会对一个对象发生一个动作。它是混合命名的,以小写字母开头,之后的每个词的首字母都应该是大写的。方法的名字中也会包含形容词和名词:
public void locate() {...} // verb public String getWayPoint() {...} // verb and noun
Instance variable names should be nouns and should follow the same capitalization convention as method names:
实例变量的名字是名词形式的,和方法的命名规范相同:
private String wayPoint;
Parameter and local variable names should be descriptive lowercase single words, acronyms, or abbreviations. If multiple words are necessary, they should follow the same capitalization convention as method names:
参数和本地变量的名字应该是具有叙述性的、小写的且是单独的一个词,首字母缩略词或缩略词。如果想要写成混合单词的话,可以参考方法的大写命名方式:
public void printHotSpots(ArrayList spotList) { int counter = 0; for (String hotSpot : spotList) { System.out.println("Hot Spot #" + ++counter + ": " + hotSpot); } }
Temporary variable names may be single letters such as i, j, k, m, and n for integers and c, d, and e for characters.
临时变量可以是单独的字母,像可以使用i,j,k,m和n来命名整数,用c,d和e之类的字母来表示字符。
Generic type parameter names should be uppercase single letters. The letter T for type is typically recommended.
泛型参数应该是大写的单独字母,强烈建议使用T来表示类型。
The Collections Framework makes extensive use of generics. E is used for collection elements, S is used for service loaders, and K and V are used for map keys and values:
集合框架大量使用了泛型,E用来表示集合元素,S用来表示service loaders,K和V用来表示map的键和值。
public interface Map { V put(K key, V value); }
Constant names should be all uppercase letters, and multiple words should be separated by underscores:
常量全部是大写字母,不同的单词之间通过下划线分隔开:
public static final int MAX_DEPTH = 200;
Enumeration names should follow the conventions of class names. The enumeration set of objects (choices) should be all uppercase letters:
枚举的命名应该遵循类命名的规范。而枚举集合里的对象应该是全部大写的:
enum Battery {CRITICAL, LOW, CHARGED, FULL}
Package names should be unique and consist of lowercase letters. Underscores may be used if necessary:
包名应该是独一无二的且由小写字母构成,必要的话也可使用下划线:
package com.oreilly.fish_finder;
Publicly available packages should be the reversed Internet do‐ main name of the organization, beginning with a single-word top-level domain name (e.g., com, net, org, or edu), followed by the name of the organization and the project or division. (Internal packages are typically named according to the project.)
Package names that begin with java and javax are restricted and can be used only to provide conforming implementations to the Java class libraries.
以java和javax开头的包名是被限制使用的,而且只能
When using acronyms in names, only the first letter of the acronym should be uppercase and only when uppercase is appropriate:
当在名字中使用首字母缩略词时,首字母缩略词只有首字母是大写,且要确保大写是合适的:
public String getGpsVersion() {...}