- 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.
推薦閱讀
- Moodle Administration Essentials
- Debian 7:System Administration Best Practices
- Visual Basic程序設計教程
- Lua程序設計(第4版)
- Java深入解析:透析Java本質的36個話題
- Reactive Programming With Java 9
- R Deep Learning Cookbook
- Node.js全程實例
- 軟件品質之完美管理:實戰經典
- 輕松上手2D游戲開發:Unity入門
- Visual FoxPro 6.0程序設計
- Troubleshooting Citrix XenApp?
- Android Sensor Programming By Example
- jQuery技術內幕:深入解析jQuery架構設計與實現原理
- Python硬件編程實戰