官术网_书友最值得收藏!

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.
主站蜘蛛池模板: 全椒县| 丰原市| 庄浪县| 广平县| 乌兰浩特市| 军事| 巴林右旗| 广元市| 蒙自县| 无为县| 望谟县| 朝阳市| 登封市| 拜泉县| 正安县| 沙雅县| 廊坊市| 宜兴市| 武义县| 孟津县| SHOW| 巴林左旗| 宁晋县| 鄂温| 云龙县| 四川省| 宝丰县| 罗城| 贡山| 韶关市| 云霄县| 秀山| 曲麻莱县| 乐业县| 瑞昌市| 冷水江市| 宁安市| 南岸区| 建宁县| 麦盖提县| 崇阳县|