Scala的eq,ne,equals,==方法与Java异同

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-29 13:37   11   0

Any是Scala继承关系中的根类,继承关系图如下:


Any根类的代码:

abstract class Any {

  def equals(that: Any): Boolean

  def hashCode(): Int

  def toString(): String

  final def getClass(): Class[_] = sys.error("getClass")

  final def ==(that: Any): Boolean = this equals that //可以看出==底层实现调用的是equals方法

  final def != (that: Any): Boolean = !(this == that) //!=底层实现也是调用的equals方法,因此可以有一个很好的判断

  final def ##(): Int = sys.error("##")

  final def isInstanceOf[T0]: Boolean = sys.error("isInstanceOf")

  final def asInstanceOf[T0]: T0 = sys.error("asInstanceOf")
  
}
从上面实现可以看出==与!=与equlas的关系了。


eq和ne是比较两个对象的引用是否相等的!



分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP