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是比较两个对象的引用是否相等的!