Scala 中的赋值方法
当执行此代码时:
var a = 24
var b = Array (1, 2, 3)
a = 42
b = Array (3, 4, 5)
b (1) = 42
我在这里看到三个(五个?)分配。在这种情况下调用的方法调用的名称是什么? 是运算符重载吗?
更新:
我可以创建一个类并重载作业吗? ( x = y 不是 x(1) = y )
When this code is executed:
var a = 24
var b = Array (1, 2, 3)
a = 42
b = Array (3, 4, 5)
b (1) = 42
I see three (five?) assignments here. What is the name of the method call that is called in such circumstances?
Is it operator overloading?
Update:
Can I create a class and overload assignment? ( x = y not x(1) = y )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有了这个文件:
运行 scalac -print allocatemethod.scala 给我们带来:
如您所见,编译器只是将最后一个 (
b (1) = 42
) 更改为方法称呼:Having this file:
running
scalac -print assignmethod.scala
gives us:As you can see the compiler just changes the last one (
b (1) = 42
) to the method call:补充 Michael 的 答案,在 Scala 中赋值不能被覆盖,尽管您可以创建类似赋值的运算符,例如
:=
。可以覆盖的“分配”是:
Complementing Michael's answer, assignment can't be overridden in Scala, though you can create an assignment-like operator, like
:=
, for example.The "assignments" that can be overridden are: