Scala - extends vs with

前端 未结 3 1181
渐次进展
渐次进展 2021-01-31 16:02

I am confused. In my existing project, I am not able to find the difference between extends and with. Could you please help me?

3条回答
  •  旧时难觅i
    2021-01-31 16:10

    The first thing you inherit from can either be a trait or a class, using the extends keyword.

    trait SomeTrait
    class SomeClass
    
    class ThisIsValid extends SomeTrait
    class ThisAsWell extends SomeClass
    

    You can define further inherited traits (and only traits) using the with keyword.

    class AlsoThisWorks extends SomeClass with SomeTrait
    

    If a trait inherits from a class you cannot use it like in the above example.

    That's it regarding the extends and with keywords. If you want to learn more about classes and traits the official documentation goes in depth on the topic.

提交回复
热议问题