Mixin vs inheritance

前端 未结 9 1008
梦毁少年i
梦毁少年i 2020-12-02 07:22

What is the difference between a mixin and inheritance?

9条回答
  •  青春惊慌失措
    2020-12-02 07:59

    With multiple inheritance, new class may be composed from multiple superclasses. You can call only methods defined in any of superclasses.

    On the other hand, mixin is an abstract subclass that may be used to specialize the beavior of a variety of parent classes. Mixins may call a method (for example sayHello(): String) even though they do not define such a method.

    mixin M {
        name: String
        defmethod greetings() { print sayHello() + " " + name}
    }
    

    As you see, you can call sayHello() even though it is not defined anywhere. If you add the mixin M to class C, the C should provide the sayHello() method.

提交回复
热议问题