Created
December 21, 2011 08:31
-
-
Save lshoo/1505237 to your computer and use it in GitHub Desktop.
书上的例子(Programming in scala2),提示enqueue方法没有实现.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Queue[T] { | |
def head: T | |
def tail: Queue[T] | |
def enqueue(x: T): Queue[T] | |
} | |
object Queue { | |
private class QueueImpl[T] ( | |
private val leading: List[T], | |
private val trailing: List[T] | |
) extends Queue[T] { | |
def mirror = | |
if (leading.isEmpty) | |
new QueueImpl(trailing.reverse, Nil) | |
else | |
this | |
def head: T = mirror.leading.head | |
def tail: QueueImpl[T] = { | |
val q = mirror | |
new QueueImpl(q.leading.tail, q.trailing) | |
} | |
def enqueue(x: T) = new QueueImpl(leading, x :: trailing) | |
} | |
} | |
class StrangeIntQueue extends Queue[Int] { | |
override def enqueue(x: Int) = { | |
println(math.sqrt(x)) | |
super.enqueue(x) | |
} | |
} | |
会提示<console>:11: error: method enqueue in trait Queue is accessed from super. It | |
y not be abstract unless it is overridden by a member declared `abstract' and | |
verride' | |
super.enqueue(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment