- Scala Design Patterns
- Ivan Nikolov
- 131字
- 2021-07-16 12:57:27
Traits versus classes
Traits could be similar, but also very different to classes. It could be hard for a developer to choose which one to use in various cases but here we will try to provide some general guidelines that should help.
Use classes:
- When a behavior is not going to be reused at all or in multiple places
- When you plan to use your Scala code from another language, for example, if you are building a library that could be used in Java
Use traits:
- When a behavior is going to be reused in multiple unrelated classes.
- When you want to define interfaces and want to use them outside Scala, for example Java. The reason is that the traits that do not have any implementations are compiled similar to interfaces.
推薦閱讀
- Unity 2020 By Example
- TypeScript Essentials
- C++程序設(shè)計(jì)(第3版)
- 秒懂設(shè)計(jì)模式
- 你不知道的JavaScript(中卷)
- Learning Salesforce Einstein
- Python 3.7從入門(mén)到精通(視頻教學(xué)版)
- Struts 2.x權(quán)威指南
- Android編程權(quán)威指南(第4版)
- Raspberry Pi Blueprints
- SFML Game Development
- Java程序設(shè)計(jì)
- Koa與Node.js開(kāi)發(fā)實(shí)戰(zhàn)
- Python數(shù)據(jù)可視化之matplotlib實(shí)踐
- Web程序設(shè)計(jì)與架構(gòu)