问题描述
mixin和inheritance有什么区别?
What is the difference between a mixin and inheritance?
推荐答案
混合输入通常与多重继承一起使用。因此,从这个意义上说,没有区别。
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.
例如,假设您有一个名为ColorAndDimension的混合名称,它会添加颜色属性以及宽度和高度。
For example, say you have a Mix In name "ColorAndDimension", which adds a color property and width and height.
现在,您可以将ColorAndDimension添加到,例如,Shape类,Sprite类,Car Class等。它们都将具有相同的接口(例如get / setColor,get / setHeight / Width等)
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.)
因此,在通用情况下,混合IS继承。但是你可以认为这是整个领域中类的作用,即混合输入是主要类还是仅仅是混合输入。
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.
是的,在今天的现代术语中,可以考虑将Mix In作为具有相关实现的接口。它实际上只是简单,陈旧,日常的多重继承,使用普通的,旧的,日常的课程。它恰好是MI的特定应用。大多数语言都没有混合在任何特殊状态下,它只是一个被设计为混入的类,而不是单独使用。
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.
这篇关于Mixin vs继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!