日期:2014-05-20  浏览次数:20641 次

Java 小问题
最近看 Java 编程思想,看到一种注释的用法:

//! some_statement;

就是“两个斜线加上一个叹号”这种用法

书上只提到一句(这种注释语法使得注释能够被自动移除以方便测试)

没明白什么意思。。

希望哪位能给详细说一下这种注释的作用及用法。。

谢谢!
------解决方案--------------------
你的理解错误吧,要么就是翻译的不准确。
我一般都喜欢用这种注释

/*
func(){
}
//*/

因为我只需要在注释的第一行前面加一个/,则可以取消注释,再把/去掉,就可以注释很多行。
------解决方案--------------------
引用:
Java 编程思想 中文版 第四版 46页,第二行就是。。
章节是:3.8 逻辑操作符

从这一页开始,后面很多的示例代码都使用了这种注释方式。。

Quote: 引用:

没见过 这种写法 ,你可以写上多少也多少行 大家一块看看,java编程思想这本书大多数人都有


看了一下确实如此,以前对thinking java 这写前几章没认真看过,没注意到这个写法,查了些
java语言规范地址,java7中3.7是注释的描述如下,也没有这种写法。

3.7. Comments

There are two kinds of comments.

/* text */

A traditional comment: all the text from the ASCII characters /* to the ASCII characters */ is ignored (as in C and C++).

// text

An end-of-line comment: all the text from the ASCII characters // to the end of the line is ignored (as in C++).


Comment:
    TraditionalComment
    EndOfLineComment

TraditionalComment:
    / * CommentTail

EndOfLineComment:
    / / CharactersInLineopt

CommentTail:
    * CommentTailStar
    NotStar CommentTail

CommentTailStar:
    /
    * CommentTailStar
    NotStarNotSlash CommentTail

NotStar:
    InputCharacter but not *
    LineTerminator

NotStarNotSlash:
    InputCharacter but not * or /
    LineTerminator

CharactersInLine:
    InputCharacter
    CharactersInLine InputCharacter
These productions imply all of the following properties:

Comments do not nest.

/* and */ have no special meaning in comments that begin with //.

// has no special meaning in comments that begin with /* or /**.

As a result, the text:

/* this comment /* // /** ends here: */
is a single complete comment.

The lexical grammar implies that comments do not occur within character literals (§3.10.4) or string literals (§3.10.5).

也无从查处,个人猜测跟当时作者的编辑器有关,如果用eclipse的话就不会这么麻烦,直接ctrl+/就可以了。
------解决方案--------------------
我来给个上下文吧:

逻辑操作符
int i = rand.nextInt(100);
int j = rand.nextInt(100);
...
print("i != j is " + (i != j));
// Treating an int as a boolean is not legal Java:
//! print("i && j is " + (i && j));
//! print("i 
------解决方案--------------------
 j is " + (i 
------解决方案--------------------
 j));
//! print("!i is " + !i);
print("(i < 10) && (j < 10) is" + ((i < 10) && (j < 10)));
...
“与”、“或”、“非”操作只可应用于布尔值。与在C及C++中不同的是:不可将一个非布尔值当作布尔值在逻辑表达式中使用。在前面的代码中用//!注释掉的语句,就是错误的用法(这种注释语句使得注释能够被自动移除以方便测试)。

所以我的理解就是:
1、作者用//!来提醒读者,这语句是错误的;
2、在代码中“注释代码”/“移除注释”,可以很方便的测试代码正确与否。