Traits let you modify the methods of a class, and they do so in a way that allows you to stack those modifications with each other.
abstract class IntQueue {
def get(): Int
def put(x: Int)
}
trait Doubling extends IntQueue {
abstract override def put(x: Int){super.put(2*x)}
}
1. The trait can only be mixed into a class that also extends IntQueue.
2. Since super calls in a trait are dynamically bound, the super call in trait
Doubling will work so long as the trait is mixed in after another trait or class that gives a concrete definition to the method.
Usage:
scala> class MyQueue extends BasicIntQueue with Doubling
No comments:
Post a Comment