Mixing multiple traits in Scala

后端 未结 2 1995
孤街浪徒
孤街浪徒 2021-01-31 07:12

Quick note: Examples from the tutorial Scala for Java Refugees Part 5: Traits and Types.

Suppose I have the traits Student, Worker, Underpaid, and Youn

2条回答
  •  感情败类
    2021-01-31 07:38

    It is easy, when declaring a class you just use the "with" keyword as often as you want

    class CollegeStudent extends Student with Worker with Underpaid with Young
    

    the order of the traits can be important if a trait is changing the behavior of the class, it all depends on traits you are using.

    Also if you don't want to have a class which always uses the same traits you can use them later:

    class CollegeStudent extends Student
    new CollegeStudent with Worker with Underpaid with NotSoYoungAnymore
    

提交回复
热议问题