scala - 为什么 Scala Trait 可以扩展类?

标签 scala traits

我发现 Scala 中的特征与 Java 中的接口(interface)类似(但是 Java 中的接口(interface)扩展了其他接口(interface),它们不扩展类)。我看到an example on SO about traits usage其中特征扩展了类。

这样做的目的是什么?为什么特征可以扩展类?

最佳答案

是的,可以,扩展特征对哪些可以扩展该特征施加了限制> - 也就是说,所有混合该特征必须扩展该

scala> class Foo
defined class Foo

scala> trait FooTrait extends Foo
defined trait FooTrait

scala> val good = new Foo with FooTrait
good: Foo with FooTrait = $anon$1@773d3f62

scala> class Bar
defined class Bar

scala> val bad = new Bar with FooTrait
<console>:10: error: illegal inheritance; superclass Bar
 is not a subclass of the superclass Foo
 of the mixin trait FooTrait
       val bad = new Bar with FooTrait
                              ^

关于scala - 为什么 Scala Trait 可以扩展类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12854941/

相关文章:

java - 支持或反对使用 JVM 的主要论点

module - 使用与结构同名的子模块的特征

rust - 当根据特征定义提取函数的返回类型时,如何提取 Rust 中的一部分函数?

java - Scala - 如何将 LocalDateTime 转换为没有 GMT 后缀格式的 ZonedDateTime?

scala - 使 Scala Remote Actors 更加稳定

scala - GADT Type as Shapeless Coproduct——如何用任意数量的代数构建解释器

c++ - 如何将特征功能委托(delegate)给类(class)成员?

rust - 如何实现通用函数的专用版本?

rust - 将范围作为结构域

scala - 如何在 Mesos 上的 Spark 中修复来自 Google Protobuf 的 "A protocol message was rejected because it was too big"?