- 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.
推薦閱讀
- Mastering Natural Language Processing with Python
- Python高級(jí)編程
- Java深入解析:透析Java本質(zhì)的36個(gè)話題
- SSM輕量級(jí)框架應(yīng)用實(shí)戰(zhàn)
- Learning Probabilistic Graphical Models in R
- Vue.js 2 Web Development Projects
- ExtJS Web應(yīng)用程序開發(fā)指南第2版
- OpenMP核心技術(shù)指南
- 玩轉(zhuǎn).NET Micro Framework移植:基于STM32F10x處理器
- 大學(xué)計(jì)算機(jī)基礎(chǔ)實(shí)訓(xùn)教程
- 大數(shù)據(jù)時(shí)代的企業(yè)升級(jí)之道(全3冊(cè))
- Instant GLEW
- 深入理解Zabbix監(jiān)控系統(tǒng)
- Ionic Cookbook
- 精通Django 3 Web開發(fā)