简体   繁体   中英

Scala trait or concrete class

So I get what we can do with traits but in what cases should we prefer their use to a concrete class ? I was told to use traits everywhere I can and when I need to instanciate something use a regular class.

Edit : I am actually asking about when to use a trait insted of a concrete class not an abstract one

As it's often the case, there is no single rule, but I personally found useful a general suggestion give in Odersky/Spoon/Venners book "Programming in Scala".

It says if the behaviour will not be reused in other unrelated classes, use a concrete class. On the other hand, if you think you'll reuse that behaviour somewhere else in unrelated places, make it a trait.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM