且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Scala中混合多个特征

更新时间:2023-09-13 23:52:34

很简单,在声明一个类时,您可以根据需要随意使用"with"关键字

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