Scala:==默认等于吗? [英] Scala: Does == default to equals?

查看:104
本文介绍了Scala:==默认等于吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读《 Scala编程》。它说:

I'm reading through Programming in Scala. It says:


您可以通过以下方式重新定义 == 的行为:覆盖等于方法,该方法始终从类 Any 继承。继承的 equals 除非对象被覆盖,否则将生效,就像对象在Java中一样。因此,等于(并带有 == )默认情况下与 eq ,但是您可以通过覆盖定义的类中的 equals 方法来更改其行为。无法直接重写 == ,因为它被定义为类 Any 中的最终方法。也就是说,Scala对待 == 就像在 Any 类中定义如下:

You can redefine the behavior of == for new types by overriding the equals method, which is always inherited from class Any. The inherited equals, which takes effect unless overridden, is object identity, as is the case in Java. So equals (and with it, ==) is by default the same as eq, but you can change its behavior by overriding the equals method in the classes you define. It is not possible to override == directly, as it is defined as a final method in class Any. That is, Scala treats == as if was defined as follows in class Any:

final def == (that: Any): Boolean = 
  if (null eq this) (null eq that) else (this equals that)


但这与我所看到的并不相称Scala 2.9.1,看起来像这样:

But this isn't jibing with what I'm seeing in scala 2.9.1, where it seems like:


  • == 似乎默认为等于

  • 我可以直接覆盖 ==

  • == doesn't seem to default to equals
  • I can override == directly (without complaint from the compiler, no override needed).

因此,似乎是编译器的投诉,不需要 override )。我喜欢以下任何一种方式:

So it seems to me like either:

  • I'm doing it wrong - this definition of Rational gives

% scala                                                                   
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).
Type in expressions to have them evaluated.
Type :help for more information.

scala> Rational(1) == Rational(1)
res0: Boolean = false

scala> Rational(1) equals Rational(1)
res1: Boolean = true


  • 或我正在阅读这本书的过时版本,并且情况有所改变。

  • or I'm reading an out of date version of the book, and things have changed.

    这是怎么回事?

    推荐答案

    您正在犯一个非常容易理解的错误-您试图编写一个类型安全的equals(即 def equals(r:Rational))而不是通用的equals(即 over def equals(a:Any))。

    You are making a very understandable mistake--you are trying to write a type-safe equals (i.e. def equals(r: Rational)) instead of a generic equals (i.e. override def equals(a: Any)).

    因此,不要覆盖等于 -请注意,您不需要覆盖关键字!-您正在通过重载类型参数创建另一个方法,然后使用两个 equals方法,其中一个方法使用 Rational 还有一个需要 Any == 也是如此;只有 Any 参数化的方法不能被覆盖。

    So instead of overriding equals--note that you don't need the override keyword!--you are creating another method by overloading the type parameters, and then having two equals methods, one which takes Rational and one which takes Any. Same thing with ==; only the Any-parameterized method cannot be overridden.

    要获得与Java一致的行为(以及Scala库),则需要将equals重写为类似的内容

    To get the behavior consistent with Java (and the Scala library), you'd need to rewrite equals as something like

    override def equals(a: Any) = a match {
      case r: Rational => numer == r.numer && denom == r.demon
      case _ => false
    }
    

    这篇关于Scala:==默认等于吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆