斯卡拉:告诉我一堂课需要抽象

时间:2015-04-27 08:15:38

标签: scala

我跟随课程中的Scala课程并在其中一个视频中使用了以下代码:

 abstract class IntSet {
  def contains(x: Int): Boolean
  def incl(x: Int): IntSet
}

class Empty extends IntSet {
  def contais(x: Int): Boolean = false
  def incl(x: Int): IntSet = new NonEmpty(x, new Empty, new Empty)
  override def toString = "."
}

class NonEmpty(elem: Int, left: IntSet, right: IntSet) extends IntSet{
  def contains(x: Int): Boolean =
    if (x < elem) left contains x
    else if (x > elem) right contains x
    else true

  def incl(x: Int): IntSet =
    if (x < elem) new NonEmpty(elem, left incl x, right)
    else if (x > elem) new NonEmpty(elem, left, right incl x)
    else this

  override def toString = "{" + left + elem + right + "}"
}

编译器告诉我:

 "Error:(6, 8) class Empty needs to be abstract, since method contains
 in class IntSet of type (x: Int)Boolean is not defined class Empty
 extends IntSet {
       ^"

根据其他posts,问题通常与方法签名中的不匹配有关,但在这种情况下&#34;包含&#34;在中的签名与 IntSet 中的签名完全相同。

1 个答案:

答案 0 :(得分:2)

你写了一个错字:

class Empty extends IntSet {
  def contais(x: Int): Boolean = false

您键入了contais而不是contains