Last active
December 20, 2015 16:59
-
-
Save lanceon/6165350 to your computer and use it in GitHub Desktop.
Scala traits composition and method overriding
This file contains 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
object TraitsTest { | |
trait Base { | |
def func() = println("Base.func") | |
} | |
trait A extends Base { | |
override def func() = { super.func(); println("A.func") } | |
} | |
trait B extends Base { | |
override def func() = { super.func(); println("B.func") } | |
} | |
class MyClass1 extends A with B { println("Constructing MyClass1") } | |
class MyClass2 extends B with A { println("Constructing MyClass2") } | |
def main(args: Array[String]) { | |
(new MyClass1).func() // --> Constructing MyClass1, Base.func, A.func, B.func | |
(new MyClass2).func() // --> Constructing MyClass2, Base.func, B.func, A.func | |
} | |
} |
Scala's Stackable Trait Pattern
http://www.artima.com/scalazine/articles/stackable_trait_pattern.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://stackoverflow.com/questions/1836060/cant-extend-two-traits-that-have-a-method-with-the-same-signature