What is a mixin, and why are they useful?

后端 未结 16 2162
無奈伤痛
無奈伤痛 2020-11-22 00:18

In \"Programming Python\", Mark Lutz mentions \"mixins\". I\'m from a C/C++/C# background and I have not heard the term before. What is a mixin?

Reading between the

16条回答
  •  旧时难觅i
    2020-11-22 01:17

    Maybe an example from ruby can help:

    You can include the mixin Comparable and define one function "<=>(other)", the mixin provides all those functions:

    <(other)
    >(other)
    ==(other)
    <=(other)
    >=(other)
    between?(other)
    

    It does this by invoking <=>(other) and giving back the right result.

    "instance <=> other" returns 0 if both objects are equal, less than 0 if instance is bigger than other and more than 0 if other is bigger.

提交回复
热议问题