抽象类和Mixin有什么区别? [英] What is the difference between an Abstract Class and a Mixin?

查看:93
本文介绍了抽象类和Mixin有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在Java框架上找到了文章,该文章显然允许它支持 Mixin 和称为复合面向程序的东西(据我所知,它甚至可能是同样的事情...)我也听说过AOP,也曾经使用过AOP,但我不确定它与AOP有何不同...

解决方案

在与语言无关的级别上,mixin只是为类添加了功能,而更多是为了给程序员提供便利并避免代码重复.抽象(基)类形成一个is-a关系,并允许多态.继承被过度使用的一个原因是,这是一种实现mixins的简单方法,而无需使用不真正支持它们的语言编写任何样板文件.问题是您将多态is-关系声明为副作用,这使您的API更加混乱,并可能增加歧义.因此,诸如D和Ruby之类的较新语言都支持mixins作为本机功能,从而提供了一种便捷的方法,可以在不声明多态is-a关系的情况下向一类添加一堆功能.

I just found an article on a framework in Java that apparently allows it to support Mixins and something called Composite Oriented Programming (which for all I know might even be the same thing...) I've also heard of/worked with AOP, and I'm not sure how it differs from this either...

解决方案

At a language-agnostic level, a mixin just adds functionality to a class, and is more for programmer convenience and to avoid code duplication. An abstract (base) class forms an is-a relationship and allows for polymorphism. One reason why inheritance is overused is that it's an easy way to implement mixins without writing any boilerplate in languages that don't really support them. The problem is that you're declaring a polymorphic is-a relationship as a side effect, making your API more confusing and possibly adding ambiguity. Hence, newer languages like D and Ruby support mixins as native features, allowing a convenient way to add a bunch of functionality to a class without declaring a polymorphic is-a relationship.

这篇关于抽象类和Mixin有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆