Maison > Java > javaDidacticiel > le corps du texte

Maîtrisez les règles de dénomination des identifiants Java et suivez facilement les spécifications

WBOY
Libérer: 2024-02-01 08:47:06
original
457 Les gens l'ont consulté

Maîtrisez les règles de dénomination des identifiants Java et suivez facilement les spécifications

Java标识符命名规则:清晰易读,规范统一

在Java编程中,标识符是用来命名变量、方法、类和包的符号。标识符的选择对于代码的可读性和可维护性至关重要。遵循Java标识符命名规则可以帮助您编写出清晰易读、规范统一的代码。

1. 标识符由字母、数字、下划线和美元符号组成

Java标识符可以由字母、数字、下划线和美元符号组成。其中,字母可以是大写或小写,数字不能作为标识符的第一个字符。例如,以下都是有效的标识符:

name
age
_age
$age
Copier après la connexion

2. 标识符不能是Java关键字

Java关键字是Java语言中具有特殊含义的单词,不能作为标识符使用。例如,以下都是Java关键字:

abstract
boolean
byte
case
catch
char
class
const
continue
default
do
double
else
extends
final
finally
float
for
if
implements
import
instanceof
int
interface
long
native
new
null
package
private
protected
public
return
short
static
super
switch
synchronized
this
throw
throws
transient
try
void
volatile
while
Copier après la connexion

3. 标识符不能以数字开头

标识符不能以数字开头。例如,以下标识符都是无效的:

1age
2name
3_age
Copier après la connexion

4. 标识符不能包含空格

标识符不能包含空格。例如,以下标识符都是无效的:

name age
age_name
Copier après la connexion

5. 标识符不能与Java保留字相同

Java保留字是Java语言中预留的单词,不能作为标识符使用。例如,以下都是Java保留字:

abstract
assert
boolean
break
byte
case
catch
char
class
const
continue
default
do
double
else
enum
extends
final
finally
float
for
goto
if
implements
import
instanceof
int
interface
long
native
new
package
private
protected
public
return
short
static
strictfp
super
switch
synchronized
this
throw
throws
transient
try
void
volatile
while
Copier après la connexion

6. 标识符应具有描述性

标识符应具有描述性,以便其他程序员能够轻松理解其含义。例如,以下标识符都是具有描述性的:

customerName
productName
orderDate
Copier après la connexion
Copier après la connexion

7. 标识符应避免使用缩写

标识符应避免使用缩写,因为缩写可能会导致代码难以理解。例如,以下标识符都是难以理解的:

custName
prodName
ordDate
Copier après la connexion

8. 标识符应避免使用下划线

标识符应避免使用下划线,因为下划线可能会导致代码难以阅读。例如,以下标识符都是难以阅读的:

_customer_name
_product_name
_order_date
Copier après la connexion

9. 标识符应遵循驼峰命名法

标识符应遵循驼峰命名法,即每个单词的首字母大写,其余字母小写。例如,以下标识符都是遵循驼峰命名法的:

customerName
productName
orderDate
Copier après la connexion
Copier après la connexion

10. 标识符应遵循一致的命名风格

标识符应遵循一致的命名风格,以便其他程序员能够轻松理解代码。例如,您可以使用以下命名风格:

  • 匈牙利命名法: 变量名前缀表示变量的类型,例如:

    • iAge 表示整型变量 age
    • sName 表示字符串变量 name
  • 帕斯卡命名法: 变量名由单词组成,每个单词的首字母大写,例如:

    • CustomerName 表示变量 customerName
    • ProductName 表示变量 productName
  • 小驼峰命名法: 变量名由单词组成,第一个单词的首字母小写,其余单词的首字母大写,例如:

    • customerName 表示变量 customerName
    • productName 表示变量 productName

代码示例:

public class Customer {

    private String customerName;
    private String customerAddress;
    private String customerPhoneNumber;

    public Customer(String customerName, String customerAddress, String customerPhoneNumber) {
        this.customerName = customerName;
        this.customerAddress = customerAddress;
        this.customerPhoneNumber = customerPhoneNumber;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getCustomerAddress() {
        return customerAddress;
    }

    public void setCustomerAddress(String customerAddress) {
        this.customerAddress = customerAddress;
    }

    public String getCustomerPhoneNumber() {
        return customerPhoneNumber;
    }

    public void setCustomerPhoneNumber(String customerPhoneNumber) {
        this.customerPhoneNumber = customerPhoneNumber;
    }

}
Copier après la connexion

在这个代码示例中,我们使用驼峰命名法来命名变量和方法。这种命名风格使代码更易于阅读和理解。

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!