table of Contents
Trait Trait
- Traits packaging methods and variables, Interface and compared to its method can be implemented, which is a bit similar to the definitions and abstract classes;
- Scala classes inherit a single inheritance, but can be mixed and multiple Trait , Trait these member variables and methods defined in the class will become a member variables and methods;
- Create a class when you can use extends with or to mix a trait;
- Traits can be considered the parent class inheritance system, reference can be used to receive Traits subclass object (polymorphism)
- Trait can not have any "class" parameter
package test
object MyTest extends Super {
override def main(args: Array[String]): Unit = {
val t: Test = new MyTest()
t.hello()
}
}
trait Test {
def hello(): Unit = println("hello scala")
}
class MyTest extends AnyRef with Test {
}