首頁 > Java > java教程 > 主體

掌握Java標識符命名規則,隨規範游刃有餘

WBOY
發布: 2024-02-01 08:47:06
原創
457 人瀏覽過

掌握Java標識符命名規則,隨規範游刃有餘

Java標識符命名規則:清晰易讀,規範統一

在Java程式設計中,識別碼是用來命名變數、方法、類別和包的符號。標識符的選擇對於程式碼的可讀性和可維護性至關重要。遵循Java標識符命名規則可以幫助您編寫出清晰易讀、規範統一的程式碼。

1. 識別碼由字母、數字、底線和美元符號組成

#Java標識符可以由字母、數字、底線和美元符號組成。其中,字母可以是大寫或小寫,數字不能作為標識符的第一個字元。例如,以下都是有效的識別碼:

name
age
_age
$age
登入後複製

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
登入後複製

3. 識別碼不能以數字開頭

標識符不能以數字開頭。例如,下列標識符都是無效的:

1age
2name
3_age
登入後複製

4. 識別碼不能包含空格

標識符不能包含空格。例如,以下識別碼都是無效的:

name age
age_name
登入後複製

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
登入後複製

6. 識別碼應具有描述性

標識符應具有描述性,以便其他程式設計師可以輕鬆理解其意義。例如,以下標識符都是描述性的:

customerName
productName
orderDate
登入後複製
登入後複製

7. 識別碼應避免使用縮寫

標識符應避免使用縮寫,因為縮寫可能會導致程式碼難以理解。例如,以下標識符都是難以理解的:

custName
prodName
ordDate
登入後複製

8. 標識符應避免使用下劃線

標識符應避免使用下劃線,因為下劃線可能會導致程式碼難以閱讀。例如,以下標識符都是難以閱讀的:

_customer_name
_product_name
_order_date
登入後複製

9. 標識符應遵循駝峰命名法

標識符應遵循駝峰命名法,即每個單字的首字母大寫,其餘字母小寫。例如,以下標識符都是遵循駝峰命名法的:

customerName
productName
orderDate
登入後複製
登入後複製

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;
    }

}
登入後複製
在這個程式碼範例中,我們使用駝峰命名法來命名變數和方法。這種命名風格使程式碼更易於閱讀和理解。

以上是掌握Java標識符命名規則,隨規範游刃有餘的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!