抽象类和特征之间的区别 [英] Difference between Abstract Class and Trait
问题描述
可能的重复:
Scala 特征与抽象类
抽象类和特征之间的概念区别是什么?
What is the conceptual difference between abstract classes and traits?
推荐答案
一个类只能扩展一个超类,因此只有一个抽象类.如果你想组合几个类,Scala 的方法是使用 mixin class composition:你组合一个(可选)超类、您自己的成员定义和一个或多个特征.与类相比,trait 受到限制,因为它不能有构造函数参数(比较 scala 参考手册一>).
A class can only extend one superclass, and thus, only one abstract class. If you want to compose several classes the Scala way is to use mixin class composition: you combine an (optional) superclass, your own member definitions and one or more traits. A trait is restricted in comparison to classes in that it cannot have constructor parameters (compare the scala reference manual).
引入了与类相比的特性限制,以避免多重继承的典型问题.关于继承层次,有或多或少复杂的规则;最好避免在这实际上很重要的层次结构.;-) 据我所知,只有从两个不同的特征继承两个具有相同签名的方法/两个具有相同名称的变量才重要.
The restrictions of traits in comparison to classes are introduced to avoid typical problems with multiple inheritance. There are more or less complicated rules with respect to the inheritance hierarchy; it might be best to avoid a hierarchy where this actually matters. ;-) As far as I understand, it can only matter if you inherit two methods with the same signature / two variables with the same name from two different traits.
这篇关于抽象类和特征之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!