我对Scala中的一些东西感到困惑。我似乎有一些对象具有相同的地址,但内容不同。我在使用Kiama时遇到了这个问题。但为了简单起见,我将所有代码归结为:
object CaseTests {
trait Attributable extends Product {
var parent: Attributable = null;
private def setChildConnections = {
var i : Int = 0
for (i <- 0 until productArity) {
productElement (i) match {
case c : Attributable => c.parent = this
case _ =>
}
}
}
setChildConnections
}
abstract class Tree extends Attributable { def id = super.toString }
case class Pair (left : Tree, right : Tree) extends Tree { println(this+" = "+super.toString + " = ("+left.id+", "+right.id+")"); }
case class Leaf (value : Int) extends Tree { println(this+" = "+super.toString); }
def main(args: Array[String]): Unit = {
val l1 = Leaf(1);
val l2 = Leaf(1);
val tree = Pair (Leaf (1), Pair (Leaf (1), Leaf (2)))
val Pair(left1: Tree, sub: Tree) = tree
val Pair(left2: Tree, right: Tree) = sub
println("left1 = "+left1.id)
println("left2 = "+left2.id)
println("left1.parent = "+left1.parent)
println("left2.parent = "+left2.parent)
}
}当我运行我的测试用例时,我得到以下输出:
Leaf(1) = org.modelica.v4.tests.full.CaseTests$Leaf@fe67d8d2
Leaf(1) = org.modelica.v4.tests.full.CaseTests$Leaf@fe67d8d2
Leaf(1) = org.modelica.v4.tests.full.CaseTests$Leaf@fe67d8d2
Leaf(1) = org.modelica.v4.tests.full.CaseTests$Leaf@fe67d8d2
Leaf(2) = org.modelica.v4.tests.full.CaseTests$Leaf@de2f8005
Pair(Leaf(1),Leaf(2)) = org.modelica.v4.tests.full.CaseTests$Pair@d8e41584 = (org.modelica.v4.tests.full.CaseTests$Leaf@fe67d8d2, org.modelica.v4.tests.full.CaseTests$Leaf@de2f8005)
Pair(Leaf(1),Pair(Leaf(1),Leaf(2))) = org.modelica.v4.tests.full.CaseTests$Pair@6a311526 = (org.modelica.v4.tests.full.CaseTests$Leaf@fe67d8d2, org.modelica.v4.tests.full.CaseTests$Pair@d8e41584)
left1 = org.modelica.v4.tests.full.CaseTests$Leaf@fe67d8d2
left2 = org.modelica.v4.tests.full.CaseTests$Leaf@fe67d8d2
left1.parent = Pair(Leaf(1),Pair(Leaf(1),Leaf(2)))
left2.parent = Pair(Leaf(1),Leaf(2))我不理解的(我怀疑这是因为我错过了Scala/Java中关于等价性的一些微妙之处,或者我只是误解了输出)是left1和left2为什么看起来有相同的地址(或者我误解了),但却是不同的父级?!
如果有人能纠正我,我会很感激的。谢谢。
附言:我正在运行Scala 2.9,以防万一。
发布于 2011-07-14 21:56:21
left1和left2不是同一个对象。尝试println(left1 eq left2),它将打印false。默认的toString方法调用Integer.toHexString(hashCode),因此如果两个对象的散列代码匹配,您将得到相同的id。在这里它做到了,因为case类会自动获得一个像样的hashCode和equals实现。
发布于 2011-07-20 23:07:29
需要注意的是: System.identityHashCode()是一个很好的技巧,但请注意,它也不能保证返回唯一的结果--我见过它为两个不同的活动对象返回相同的数字。
https://stackoverflow.com/questions/6694112
复制相似问题