[code="java"]public class ClassName extends ParentClassName
{
public ClassName () {;}
}[/code]
首先我们来看看构造函数的词法定义:
红色的opt表示可有可无。
[b]ConstructorBody:[/b]
{ ExplicitConstructorInvocation[color=red][size=xx-small]opt[/size][/color] BlockStatements[color=red][size=xx-small]opt[/size][/color]}
接着下面是BlockStatements的词法定义:
[b]BlockStatements:[/b]
BlockStatement
BlockStatements BlockStatement
[b]BlockStatement:[/b]
LocalVariableDeclarationStatement
ClassDeclaration
接下来,看看Statement的词法定义:
[b]Statement:[/b]
StatementWithoutTrailingSubstatement
LabeledStatement
IfThenStatement
IfThenElseStatement
WhileStatement
ForStatement
接下来看看statement中第一个StatementWithoutTrailingSubstatement词法定义:
[b]StatementWithoutTrailingSubstatement:[/b]
Block
EmptyStatement
ExpressionStatement
AssertStatement
SwitchStatement
DoStatement
BreakStatement
ContinueStatement
ReturnStatement
SynchronizedStatement
ThrowStatement
TryStatement
最后看看statement的词法定义。
[b]EmptyStatement:[/b]
;
java规范中对此如下说:[quote]
An empty statement does nothing.
Execution of an empty statement always completes normally.[/quote]=================================
一路走下来,有点费劲,但是总算把问题弄清楚了,为什么构造函数、方法等中可以只有分号(;),其实跟“{ }”这样没区别。
所以甚至可以这样写代码:
[code="java"]if(true){;}[/code]
[color=gray]详细分析参考:The Java™ Language Specification Third Edition[/color]
没有什么特别的含义,分号是java语句的逻辑分割标识,单个分号就是个空语句,任何地方都可以放
就是一个空的构造方法。
PS: 读源码,不要拘泥于古怪的语法,要着眼于内容。写代码时,也不要用少有的用法,尽量使用通常的写法。
The Java™ Language Specification Third Edition
///////////////////
这本java规范讲的很清楚,所有的java书籍都是以这个为标准的。