一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Java注释与标识符的介绍

时间:2016-04-20 编辑:简简单单 来源:一聚教程网

注释:

  单行注释://

  多行注释:/*   */

  文档注释:/**  */

  其中文档注释可以Export导出dox文档,常用Javadox标记如下:

    @author:指定Java程序的作者

    @version:指定源文件的版本

    @deprecated:不推荐使用的代码

    @param:方法的参数说明信息

    @return:方法的返回值说明信息

    @see:“参见”,用于指定较差参考的内容

    @throws:抛出的异常,与@exception同意

参考举例:

1.   类(接口)注释

例如:

/**

* 类的描述

* @author Administrator

* @Time 2012-11-2014:49:01

*

*/

public classTest extends Button {

  ……

}

2.   构造方法注释

例如:

public class Test extends Button {

  /**

   * 构造方法 的描述

   * @param name

   *       按钮的上显示的文字

   */

  public Test(String name){

     ……

  }

}

3.   方法注释

例如

public class Test extends Button {

  /**

   * 为按钮添加颜色

   *@param color

         按钮的颜色

*@return

*@exception  (方法有异常的话加)

* @author Administrator

* @Time2012-11-20 15:02:29


   */

  public voidaddColor(String color){

     ……

  }

}

4.   全局变量注释

例如:

public final class String

   implements java.io.Serializable, Comparable,CharSequence

{

   /** The value is used for characterstorage. */

   private final char value[];

   /** The offset is the first index of thestorage that is used. */

   private final int offset;

   /** The count is the number of charactersin the String. */

   private final int count;

   /** Cache the hash code for the string */

private int hash; // Default to 0

……

}

5.   字段/属性注释

例如:

public class EmailBody implements Serializable{

   private String id;

   private String senderName;//发送人姓名

   private String title;//不能超过120个中文字符

   private String content;//邮件正文

   private String attach;//附件,如果有的话

   private String totalCount;//总发送人数

   private String successCount;//成功发送的人数

   private Integer isDelete;//0不删除 1删除

   private Date createTime;//目前不支持定时 所以创建后即刻发送

   privateSet EmailList;

……

}

其实规范是自己订的,只要团队中大家都统一遵守,统一规范,就会取得好的效果,希望对平时不加注释的朋友有点帮助。

标识符:

  分号(;):每个java语句必须使用分号作为结尾

  花括号({}):定义一个代码块,代码块在逻辑上是一个整体

  方括号([]):紧跟在数组变量名之后,用于访问数组元素,括号中为希望访问的数组索引

  圆括号(()):1.定义方法时圆括号申明形参,调用方法时圆括号传入实参

         2.将表达式的某个部分括为整体,优先运算

         3.强制类型转化的运算符

  空格( ):空格可出现在java的任何地方,注意不可用空格分开变量名

  圆点(.):用在类/对象和它的成员(成员变量、方法、内部类)之间,表明调用某个类/实例的指定成员

总结起来:
[1]Java标识符只能由数字、字母、下划线“_”或“$”符号以及Unicode字符集组成
[2]Java标识符必须以字母、下划线“_”或“$”符号以及Unicode字符集开头
[3]Java标识符不可以是Java关键字、保留字(const、goto)和字面量(true、false、null)
[4]Java标识符区分大小写,是大小写敏感的
合法标识符举例:
_哈哈、哈哈、_name、name、$dollor、$123、哈1哈
非法标识符举例:
1name、1Hello、 H%llo
——[$]标识符举例——
import java.lang.*;
public class IdentityTester {
    public static void main(String args[]){
        // 因为中文是Unicode字符集,所以是可以作为标识符的
        String _哈哈 = "Hello";
        String 哈哈 = "You are very good";
        int _name = 0;
        int name = 0;
        float $dollor = 0.0f;
        String 哈1哈 = "Hello One Hello";
        String 1Hello = "";
        String H%llo = "";
    }
}
    上边的代码中包含非合法的标识符 1Hello , H%llo,因此程序的运行结果会出现error: Java 标识符
    编译器不能识别。
   当程序改成:

import java.lang.*;
public class IdentityTester{
   public static void main(String args[]){
       String _哈哈="Hello";
       String 哈哈="You are very good";
       int _name=0;
       int name=0;
       float $dollor=0.0f;
       String 哈1哈="Hello One Hello";
       //String 1Hello="";
       //String H%llo="";
       System.out.println(_哈哈);
       System.out.println(哈哈);
       System.out.println(_name);
       System.out.println(name);
       System.out.println($dollor);
       System.out.println(哈1哈);
 
     }
}
 
运行结果为:Java 标识符
 
改进多行输出:
import java.lang.*;
public class IdentityTester{
   public static void main(String args[]){
       String _哈哈="Hello";
       String 哈哈="You are very good";
       int _name=0;
       int name=0;
       float $dollor=0.0f;
       String 哈1哈="Hello One Hello";
       //String 1Hello="";
       //String H%llo="";
       System.out.print(_哈哈+哈哈+_name+name);
      // System.out.println(哈哈);
       //System.out.println(_name);
       //System.out.println(name);
       //System.out.println($dollor);
       //System.out.println(哈1哈);
   }
}

热门栏目