Scala 中的赋值方法

发布于 2024-09-01 06:45:55 字数 240 浏览 6 评论 0原文

当执行此代码时:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨轻弹 2024-09-08 06:45:55

有了这个文件:

//assignmethod.scala
object Main {
  def main(args: Array[String]) {
    var a = 24
    var b = Array (1, 2, 3)
    a = 42
    b = Array (3, 4, 5)
    b (1) = 42
  }
}

运行 scalac -print allocatemethod.scala 给我们带来:

[[syntax trees at end of cleanup]]// Scala source: assignmethod.scala
package <empty> {
  final class Main extends java.lang.Object with ScalaObject {
    def main(args: Array[java.lang.String]): Unit = {
      var a: Int = 24;
      var b: Array[Int] = scala.Array.apply(1, scala.this.Predef.wrapIntArray(Array[Int]{2, 3}));
      a = 42;
      b = scala.Array.apply(3, scala.this.Predef.wrapIntArray(Array[Int]{4, 5}));
      b.update(1, 42)
    };
    def this(): object Main = {
      Main.super.this();
      ()
    }
  }
}

如您所见,编译器只是将最后一个 (b (1) = 42) 更改为方法称呼:

b.update(1, 42)

Having this file:

//assignmethod.scala
object Main {
  def main(args: Array[String]) {
    var a = 24
    var b = Array (1, 2, 3)
    a = 42
    b = Array (3, 4, 5)
    b (1) = 42
  }
}

running scalac -print assignmethod.scala gives us:

[[syntax trees at end of cleanup]]// Scala source: assignmethod.scala
package <empty> {
  final class Main extends java.lang.Object with ScalaObject {
    def main(args: Array[java.lang.String]): Unit = {
      var a: Int = 24;
      var b: Array[Int] = scala.Array.apply(1, scala.this.Predef.wrapIntArray(Array[Int]{2, 3}));
      a = 42;
      b = scala.Array.apply(3, scala.this.Predef.wrapIntArray(Array[Int]{4, 5}));
      b.update(1, 42)
    };
    def this(): object Main = {
      Main.super.this();
      ()
    }
  }
}

As you can see the compiler just changes the last one (b (1) = 42) to the method call:

b.update(1, 42)
萌能量女王 2024-09-08 06:45:55

补充 Michael 的 答案,在 Scala 中赋值不能被覆盖,尽管您可以创建类似赋值的运算符,例如 :=

可以覆盖的“分配”是:

// method update
a(x) = y 
// method x_=, assuming method x exists and is also visible 
a.x = y
// method +=, though it will be converted to x = x + y if method += doesn't exist
a += y

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:

// method update
a(x) = y 
// method x_=, assuming method x exists and is also visible 
a.x = y
// method +=, though it will be converted to x = x + y if method += doesn't exist
a += y
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文