trait Logger {
def log(msg: String) {}
}
trait PrinterLogger extends Logger {
val v = 24
override def log(msg: String): Unit = {
println(msg)
}
}
trait CryptoLogger extends Logger {
val key = 3
override def log(msg: String): Unit = {
val ceasarMsg = for (c <- msg) yield c match {
case a if a >= 'A' && a <= 'Z' => applyKey(a, 'A', 26)
case a if a >= '0' && a <= '9' => applyKey(a, '0', 10)
case _ => c
}
super.log(ceasarMsg)
}
private def applyKey(c: Char, r: Char, l: Int): Char = {
((c - r + key) % l + r).toChar
}
}
class Person(private val _name: String) extends {
override val key = 9
override val v = 36
} with PrinterLogger with CryptoLogger {
def name = {
log(_name)
_name
}
}
val JohnSmith = new Person("John Smith")
println(JohnSmith.key)
println(JohnSmith.v)
JohnSmith.name