scala - `super` 是否静态绑定(bind)在类中?

标签 scala class traits

我正在阅读 Martin Odersky 等人(第 2 版)在“Scala 编程”中关于特征的章节,我对 super 在静态绑定(bind)类中的声明感到困惑,与特征不同,动态绑定(bind)的位置(第 220 页)。

我理解这句话,但是当涉及到像这样的例子时:

val queue = (new BasicIntQueue with Incrementing with Filtering)

p.229 或线性化的完整解释(p.234)在我看来,super 不能静态绑定(bind),因为否则堆叠特征是不可能的 - 即当类“开始”已经解析了 super 的堆栈方法调用链,无论用户将什么添加到特征堆栈,它都会命中直接类父类。

我错过了什么? :-) super 真的静态绑定(bind)到它的父级吗?

最佳答案

val queue = (new BasicIntQueue with Incrementing with Filtering)

on p.229 or entire explanation of linearization (p.234) it seems to me, that super cannot be statically bound, because otherwise stacking traits would not be possible -- i.e. when class "starts" the chain of calling of stacked method with super already resolved, it would hit direct class parent no matter what user added to the stack of traits

但在这种情况下,链不是从 BasicIntQueue 的方法开始,而是从 Filtering 的方法开始。如果没有混合特征覆盖方法,它只会从类开始。如果您改为定义

class MyQueue extends BasicIntQueue with Incrementing with Filtering {
  .. // some super calls
}

// an anonymous class
val queue = new BasicIntQueue with Incrementing with Filtering {
  .. // some super calls
}

super 将引用 BasicIntQueue with Incrementing with Filtering,这是静态解析的。

但是,Incrementing 中的 super 调用是动态解析的:在 BasicIntQueue with Incrementing 中,它们将引用 BasicIntQueue ,而在 BasicIntQueue with Filtering with Incrementing 中,它们将引用 BasicIntQueue with FilteringIncrementing 没有任何变化。

关于scala - `super` 是否静态绑定(bind)在类中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30342048/

相关文章:

c++ - 在 C++0x 标准中使用/定义了什么样的 "Traits"

rust - 如何为我不拥有的类型实现我不拥有的特征?

scala - 您如何编写对文件更改使用react的 Scala 脚本

Java:如何从 String 创建类列表

c++ - 如何用 1000000 个元素声明类 C++

css - Aptana:在 .css 文件中显示未使用的 css 类

scala - 重写 Scala 中的密封特征

scala - 在 Spark Streaming 中,有没有办法检测批处理何时完成?

Scala Lift 几个 https 问题

scala - Concrete Map#empty 在创建自定义 map 扩展时导致设计气味