这是两个方法声明:
def fun = "x"
def fun() = "x"
由于它们都不需要参数并返回String,因此除调用方式之外还有什么区别?
请您参考如下方法:
这只是一个约定:
obj.fun //accessing like property with no side-effects
obj.fun() //ordinary method call, return value, but might also have side-effects
首选
()版本以强调这是一种与简单属性相反的方法。
请注意,这只是一种约定,一种记录代码的方式,编译器不会强制执行上述规则。




