我想在模式匹配中知道,如下所示,我们是否说动态或静态绑定案例类?
sealed trait Expr
case class Lit(n: Int) extends Expr
case class Add(l: Expr, r: Expr) extends Expr
class Eval {
def eval(e: Expr): Int = e match {
case Lit(n) => n
case Add(l, r) => eval(l) + eval(r)
}
}
class Printo {
def printo(e: Expr): Unit = e match {
case Lit(n) => print(" " + n + " ")
case Add(l, r) => printo(l); print("+"); printo(r)
}
}
//Test
object TestExpr {
def main(args: Array[String]) {
val eval = new Eval
val printo = new Printo
val expr1 =
Add(Add(Lit(4), Add(Lit(7), Lit(10))),
Add(Lit(4), Add(Lit(7), Lit(10))))
val x = eval.eval(expr1)
p + "" + println(" = " + x)
}
}
我如何知道Lit(n)
或Add(l, r)
是静态还是动态调用/绑定的?