Which among importing companion object or extending trait is better

前端 未结 3 1502
名媛妹妹
名媛妹妹 2021-01-23 05:36

I have a JSON protocol written in spray

trait MyJsonProtocol {
   //some logic
}

object MyJsonProtocol extends MyJsonProtocol {

}

Now which i

3条回答
  •  旧时难觅i
    2021-01-23 05:48

    It depends on the scenario, Because a companion class or object can access the private members of its companion. Use a companion object for methods and values which are not specific to instances of the companion class. If you just want multiple inheritance but allow code reusability then trait is fine.

    Hope it helps.

提交回复
热议问题