java 命名约定_Java命名约定

java 命名约定

While writing code in Java, it is advisable to follow certain naming conventions. Java naming conventions bring some form of uniformity across your code. It makes your code easy to read for other developers.

在用Java编写代码时,建议遵循某些命名约定。 Java命名约定为您的代码带来了某种形式的统一性。 它使您的代码易于其他开发人员阅读。

Although these are not hardcore rules, it is best to follow these practices while writing code.

尽管这些不是硬性规则,但是在编写代码时最好遵循这些做法。

1. Java包命名约定 (1. Java Packages Naming Conventions)

  • The package name should be in a small case.

    软件包名称应为小写。
  • In case there are multiple words, separate them by a dot.

    如果有多个单词,请用圆点将其分开。
  • The prefix should be one of the top-level domain names like com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries. (In, Us, Uk)

    前缀应该是com,edu,gov,mil,net,org等顶级域名之一,或者是用来标识国家/地区的英语两个字母的代码之一。 (在我们,英国)

Example :

范例:


package com.journaldev.util;

2.类和接口的命名约定 (2. Classes and Interfaces Naming Conventions)

  • Class and Interface names should be nouns.

    类和接口名称应为名词。
  • They can be in mixed case but the first letter of each internal word should be capitalized. That means the first letter of the class and interface name should be capital too.

    它们可以是大小写混合的,但每个内部单词的首字母应大写。 这意味着类和接口名称的首字母也应为大写。
  • Avoid acronyms and abbreviations.

    避免使用首字母缩写词和缩写。

Example :

范例:


public class Vehicle  
{  
//code  
}  

class CarCleaningShop {

}

3. Java方法命名约定 (3. Java Methods Naming Convention)

  • Methods should be verbs indicative of the functionality of that particular method.

    方法应该是指示该特定方法功能的动词。
  • They can be in mixed case.

    它们可以混合使用。
  • The first letter should be in lowercase with every subsequent word having the first letter in uppercase.

    首字母应小写,随后的每个单词的首字母均应大写。

Example :

范例:


void slowDown()
{
//code 
} 

void getCustomerAddress() {
}

4. Java变量命名约定 (4. Java Variables Naming Conventions)

  • Variable names should not start with an underscore (_) or dollar sign ($) characters.

    变量名称不应以下划线(_)或美元符号($)字符开头。
  • Start variable names with a lowercase letter with every subsequent word having the first letter in uppercase.

    变量名以小写字母开头,随后的每个单词的首字母均为大写。
  • Avoid using One-character (i,j,k) variable names except in case of temporary throwaway variables.

    避免使用单字符(i,j,k)变量名,除非是临时丢弃的变量。
  • Variable name should be indicative of the variable’s use.

    变量名称应指示变量的用途。

public class Vehicle  
{  
public void slowDown()
{
int speed;
int timeToStop; 
} 
}  

5.常量命名约定 (5. Constants Naming Conventions)

  • A constant name should be in all uppercase.

    常量名称应全部大写。
  • In case there are multiple words, separate them by underscores (“_”).

    如果有多个单词,请用下划线(_)隔开。

Example :

范例:


public class Vehicle { 

static final int MAX_SPEED = 120;

}

结论 (Conclusion )

These are the naming conventions that will make your Java code easy to read. It is not necessary to use these, however, when writing code for production that is going to be read by other people it is best to use Java naming conventions.

这些是使您的Java代码易于阅读的命名约定。 不必使用这些,但是,当编写将要由其他人读取的生产代码时,最好使用Java命名约定。

翻译自: https://www.journaldev.com/42341/java-naming-conventions

java 命名约定

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值