zoukankan      html  css  js  c++  java
  • 混入和继承的区别

    Mixin vs inheritance
    Ask Question
    up vote
    76
    down vote
    favorite
    19
    What is the difference between a mixin and inheritance?

    oop inheritance mixins
    shareimprove this question
    edited Jan 7 '12 at 21:18
    user142019
    asked May 13 '09 at 20:31

    Johnd
    1,95862022
    add a comment
    8 Answers
    active oldest votes
    up vote
    47
    down vote
    A Mix in is typically used with multiple inheritance. So, in that sense, there's "no difference".

    The detail is that a Mix in is rarely useful as a standalone object.

    For example, say you have a Mix In name "ColorAndDimension", which adds a color property and width and height.

    Now, you could add ColorAndDimension to a, say, Shape Class, a Sprite Class, a Car Class, etc. And they will all have the same interface (say get/setColor, get/setHeight/Width, etc.)

    So, in the generic case a Mix in IS inheritance. But you can argue it's a matter of the role of the class in the overall domain as to whether a Mix in is a "primary" class or simply a mix in.

    Edit -- just to clarify.

    Yes, a Mix In can be considered, in todays modern lingo, an Interface with an associated Implementation. It really is just plain, old, everyday multiple inheritance using a plain, old, everyday class. It just happens to be a specific application of MI. Most languages don't give a Mix In any special status, it's just a class that was designed to be "mixed in", rather than used stand alone.

    shareimprove this answer
    edited May 14 '09 at 4:18
    answered May 13 '09 at 20:42

    Will Hartung
    92k17106187
    add a comment
    up vote
    18
    down vote
    What is the difference between a mixin and inheritance?
    A mix-in is a base class you can inherit from to provide additional functionality. The name "mix-in" indicates it is intended to be mixed in with other code. As such, the inference is that you would not instantiate the mix-in class on its own. Frequently the mix-in is used with other base classes. Therefore mixins are a subset, or special case, of inheritance.

    The advantages of using a mix-in over single inheritance are that you can write code for the functionality one time, and then use the same functionality in multiple different classes. The disadvantage is that you may need to look for that functionality in other places than where it is used, so it is good to mitigate that disadvantage by keeping it close by.

    I have personally found a mix-in necessary to use over single inheritance where we are unittesting a lot of similar code, but the test-cases are instantiated based on their inheritance of a base case, and the only way to keep the code close at hand (and in the same module), without messing with coverage numbers, is to inherit from object, and have the child cases inherit from both the universal test-case base and the custom base that only applies to them.

    Mixins in Comparison and Contrast with Abstract Base Classes
    Both are a form of parent class that is not intended to be instantiated.

    A mixin provides functionality, but is unable to directly use it. A user is intended to use it through a (sub)class.

    An abstract base class provides an interface, but without usable functionality. A user is intended to create the functionality called by the interface.

    In Python, some classes in the abc module are examples of parent classes that both provide functionality through inheritance and abstract interfaces that must be implemented by the subclass. These ideas are not mutually exclusive.

  • 相关阅读:
    两个容器盛水法。
    Windows程序设计 读书笔记 位图和BitBlt。
    VC DrawText显示多行,包括设置行距。
    内存管理函数
    GDI+
    二进制法。
    Windows程序设计 读书笔记 打印机。
    逆推法
    DDB和DIB概念区别 及 程序示例。
    Windows程序设计 读书笔记 剪贴板。
  • 原文地址:https://www.cnblogs.com/wdmx/p/9940258.html
Copyright © 2011-2022 走看看