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

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.
主站蜘蛛池模板: 共和县| 义乌市| 胶州市| 肇源县| 碌曲县| 清涧县| 安仁县| 城固县| 历史| 长海县| 南充市| 上栗县| 嘉善县| 江津市| 康乐县| 罗平县| 大英县| 德化县| 富顺县| 屏东县| 广州市| 江北区| 竹山县| 堆龙德庆县| 丰宁| 云林县| 宣汉县| 子长县| 扶风县| 兰溪市| 牡丹江市| 林周县| 长汀县| 静宁县| 攀枝花市| 资溪县| 永仁县| 金堂县| 平阳县| 三河市| 会同县|