熟练使用注释是一个程序员的基本素养
作用:使部分内容只为程序员可见,不为编译器所编译、虚拟机所执行
位置:类声明前后、方法声明前后、属性声明前后、方法体中。几乎可以在一个源文件任意位置,但不能在一个关键字字符中插入注释
类型:1.单行注释://text ——从 // 到本行结束的所有字符均作为注释而被编译器忽略
2.多行注释:/*text*/ ——从 /* 到 */ 间的所有字符会被编译器忽略
3.文档注释:/**test*/ ——从"/**"到"*/"间的所有字符会被编译器忽略。当这类注释出现在任何声明(如类的声明、类的成员变量的声明或者类的成员方法的声明)之前时,会作为JavaDoc文档的内容;
@author 类的作者
@version 类的版本
@since 从什么时候开始使用的
@see 另外参照...
@param 方法的参数
@return 方法的返回类型
@exception 方法抛出的异常
文档注释举例:
package com.wsj.java.ch02;
/**
* this is my CommentTest Class
*@author wangshuaijie
*@version 1.0
*@since JDK1.7
* */
public class CommentTest{
//表示这个人的名字
private String name;
/**
*this is a age of person
*
* */
private int age;
/**
*@param name :a name of person
*@exception null
* */
public void setName(String name){
this.name = name;
}
/**
*@return 没有返回类型
* */
public String getName(){
return this.name;
}
public static void main(String[] args){
/*
CommentTest ct = new CommentTest();
ct.setName("tom");
String temp = ct.getName();
System.out.println(temp);
*/
}
}