不(没有)Scala自动生成了setter吗?
发布时间:2020-12-16 18:19:37 所属栏目:安全 来源:网络整理
导读:谷歌和我失败的记忆都给了我一些提示,但每一次尝试都是枯竭的. class Y { var y = 0}var m = new Y()m.y_(3)error: value y_ is not a member of Y 请告诉我,我做错了什么. (另外:请告诉我这是错的.) 编辑 我没有做错的事情,或者至少不是我唯一做错的事情,
谷歌和我失败的记忆都给了我一些提示,但每一次尝试都是枯竭的.
class Y { var y = 0 } var m = new Y() m.y_(3) error: value y_ is not a member of Y 请告诉我,我做错了什么. (另外:请告诉我这是错的.) 编辑 我没有做错的事情,或者至少不是我唯一做错的事情,就是我调用setter的方式.以下内容也会失败,所有错误消息都相同: m.y_ // should be a function valued expression m.y_ = (3) // suggested by Google and by Mchl f(m.y_) // where f takes Int => Unit as an argument f(m.y) // complains that I am passing in Int not a function 我通过SimplyScala这样做,因为我太懒了,不耐烦在我的小型家用机器上安装Scala.希望不是那个…… 最终获胜者是 … Fabian,他指出我在_和=之间不能有空格.我想出了为什么会这样,然后它发生在我身上:
注意: class Y { var y = 0 } var m = new Y() m.y_=(3) m.y res1: Int = 3 m.y_= error: missing arguments for method y_= in class Y; follow this method with `_` if you want to treat it as a partially applied function m.y_= ^ m.y_=_ res2: (Int) => Unit = def four(f : Int => Unit) = f(4) four(m.y_=) m.y res3: Int = 4 StackExchange上另一个成功的日子. 解决方法
确实如此.他们只是透明的.
m.y = 3 这实际上通过setter方法访问m.y. 你可以直接打电话给m.y _ =(3) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |