java 命名约定_使用Java命名约定

java 命名约定

A naming convention is a rule to follow as you decide what to name your identifiers (e.g. class, package, variable, method, etc.).

命名约定是您决定使用什么名称命名标识符 (例如,类,包,变量,方法等)时要遵循的规则。

为什么要使用命名约定? ( Why Use Naming Conventions? )

Different Java programmers can have different styles and approaches to the way they program. By using standard Java naming conventions they make their code easier to read for themselves and for other programmers. Readability of Java code is important because it means less time is spent trying to figure out what the code does, leaving more time to fix or modify it.

不同的Java程序员可以采用不同的样式和方法进行编程。 通过使用标准的Java命名约定,它们使自己和其他程序员的代码更易于阅读。 Java代码的可读性很重要,因为它意味着花更少的时间来弄清代码的作用,从而有更多的时间来修复或修改它。

To illustrate the point it's worth mentioning that most software companies will have a document that outlines the naming conventions they want their programmers to follow. A new programmer who becomes familiar with those rules will be able to understand code written by a programmer who might have left the company many years beforehand.

为了说明这一点,值得一提的是,大多数软件公司都会有一份文档,概述了他们希望程序员遵循的命名约定。 熟悉这些规则的新程序员将能够理解由可能已经多年离开公司的程序员编写的代码。

为您的标识符选择一个名称 ( Picking a Name for Your Identifier )

When choosing a name for an identifier, make sure it's meaningful. For instance, if your program deals with customer accounts then choose names that make sense for dealing with customers and their accounts (e.g., customerName, accountDetails). Don't worry about the length of the name. A longer name that sums up the identifier perfectly is preferable to a shorter name that might be quick to type but ambiguous.

为标识符选择名称时,请确保它有意义。 例如,如果您的程序处理客户帐户,则选择对客户及其客户帐户有意义的名称(例如,customerName,accountDetails)。 不用担心名称的长度。 较长的名称可以完美地概括标识符,而不是较短的名称,后者可以快速键入但不明确。

关于案件的几句话 ( A Few Words About Cases )

Using the right letter case is the key to following a naming convention:

使用正确的字母大小写是遵循命名约定的关键:

  • Lowercase is where all the letters in a word are written without any capitalization (e.g., while, if, mypackage).

    小写字母是指单词中的所有字母都没有大写(例如,如果是,则为mypackage)。

  • Uppercase is where all the letters in a word are written in capitals. When there are more than two words in the name use underscores to separate them (e.g., MAX_HOURS, FIRST_DAY_OF_WEEK).

    大写是一个单词中的所有字母都用大写字母表示的地方。 如果名称中有两个以上的单词,请使用下划线将其分隔(例如MAX_HOURS,FIRST_DAY_OF_WEEK)。

  • CamelCase (also known as Upper CamelCase) is where each new word begins with a capital letter (e.g., CamelCase, CustomerAccount, PlayingCard).

    在CamelCase (也称为Upper CamelCase)中,每个新单词都以大写字母开头(例如CamelCase,CustomerAccount,PlayingCard)。

  • Mixed case (also known as Lower CamelCase) is the same as CamelCase except the first letter of the name is in lowercase (e.g., hasChildren, customerFirstName, customerLastName).

    混合大小写 (也称为小写CamelCase)与CamelCase相同,只是名称的首字母小写(例如,hasChildren,customerFirstName,customerLastName)。

标准Java命名约定 ( Standard Java Naming Conventions )

The below list outlines the standard Java naming conventions for each identifier type:

下表概述了每种标识符类型的标准Java命名约定:

  • Packages: Names should be in lowercase. With small projects that only have a few packages it's okay to just give them simple (but meaningful!) names:

    包:名称应小写。 对于只有几个软件包的小型项目,可以给它们简单的名称(但有意义!)是可以的:

     package pokeranalyzer package mycalculator  In software companies and large projects where the packages might be imported into other classes, the names will normally be subdivided. Typically this will start with the company domain before being split into layers or features: 
     package com.mycompany.utilities package org.bobscompany.application.userinterface 
  • Classes: Names should be in CamelCase. Try to use nouns because a class is normally representing something in the real world:

    班级:名字应该在CamelCase中。 尝试使用名词,因为类通常代表现实世界中的某些事物:

     class Customer class Account 
  • Interfaces: Names should be in CamelCase. They tend to have a name that describes an operation that a class can do:

    界面:名称应在CamelCase中。 它们通常具有一个描述类可以执行的操作的名称:

     interface Comparable interface Enumerable  Note that some programmers like to distinguish interfaces by beginning the name with an "I": 
     interface IComparable interface IEnumerable 
  • Methods: Names should be in mixed case. Use verbs to describe what the method does:

    方法:名称应大小写混合。 使用动词来描述该方法的作用:

     void calculateTax() string getSurname() 
  • Variables: Names should be in mixed case. The names should represent what the value of the variable represents:

    变量:名称应大小写混合。 名称应代表变量值代表的含义:

     string firstName int orderNumber  Only use very short names when the variables are short-lived, such as in for loops: 
     for (int i=0; i<20;i++) {   //i only lives in here } 
  • Constants: Names should be in uppercase.

    常量:名称应为大写。

     static final int DEFAULT_WIDTH static final int MAX_HEIGHT 

翻译自: https://www.thoughtco.com/using-java-naming-conventions-2034199

java 命名约定

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值