gpt4 book ai didi

scala - Scala中的fun和fun()有什么区别?

转载 作者:行者123 更新时间:2023-12-04 16:29:49 25 4
gpt4 key购买 nike

这是两个方法声明:

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

首选 ()版本以强调这是一种与简单属性相反的方法。

请注意,这只是一种约定,一种记录代码的方式,编译器不会强制执行上述规则。

关于scala - Scala中的fun和fun()有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8228025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com