首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >case类问题的Scala构造函数

case类问题的Scala构造函数
EN

Stack Overflow用户
提问于 2011-07-14 21:43:08
回答 2查看 191关注 0票数 2

我对Scala中的一些东西感到困惑。我似乎有一些对象具有相同的地址,但内容不同。我在使用Kiama时遇到了这个问题。但为了简单起见,我将所有代码归结为:

代码语言:javascript
复制
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)
  }
}

当我运行我的测试用例时,我得到以下输出:

代码语言:javascript
复制
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,以防万一。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-14 21:56:21

left1left2不是同一个对象。尝试println(left1 eq left2),它将打印false。默认的toString方法调用Integer.toHexString(hashCode),因此如果两个对象的散列代码匹配,您将得到相同的id。在这里它做到了,因为case类会自动获得一个像样的hashCodeequals实现。

票数 6
EN

Stack Overflow用户

发布于 2011-07-20 23:07:29

需要注意的是: System.identityHashCode()是一个很好的技巧,但请注意,它也不能保证返回唯一的结果--我见过它为两个不同的活动对象返回相同的数字。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6694112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档