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

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.
主站蜘蛛池模板: 唐河县| 黄陵县| 邯郸县| 河北区| 双江| 嘉义市| 吐鲁番市| 灵石县| 昌平区| 桦南县| 和龙市| 商水县| 莲花县| 新密市| 郧西县| 沁阳市| 延吉市| 岗巴县| 上饶市| 哈尔滨市| 铁岭市| 定日县| 丰顺县| 和平区| 桦川县| 昌邑市| 石屏县| 岳普湖县| 睢宁县| 宁晋县| 哈尔滨市| 远安县| 枣庄市| 罗田县| 雅安市| 攀枝花市| 高碑店市| 新宁县| 屯昌县| 久治县| 墨脱县|