【发布时间】:2020-02-18 19:34:49
【问题描述】:
您好,我是 Scala 新手,我很难让我的班级打印出一些值。
class TreeDemo[T](implicit o : T => Ordered[T]) {
sealed trait BinaryTree
case object Empty extends BinaryTree
case class Node(left:BinaryTree, d:T, right:BinaryTree) extends BinaryTree
// construct a "leaf" node
def Leaf(d : T) : BinaryTree = Node(Empty,d,Empty)
// remove all nodes equal to x from tree t
def remove(t : BinaryTree, x : T) : BinaryTree = {
replace(t, x, Empty)
}
val myTree = Node(Node(Leaf(1),2,Leaf(3)),4,Leaf(5))
val x = remove(myTree, 2)
def main (args: Array[String]) {
println(x)
}
}
从上面的代码中,我尝试打印删除 def,但出现错误。
【问题讨论】:
-
请定义
but I am getting errors。 -
remove()尚未在发布的代码中定义。而TreeDemo类接收类型参数T,所以它是在类外定义的,所以不能在里面创建Leaf(1)(其中T是Int)类定义。
标签: scala