为什么抽象方法有时需要覆盖? [英] Why is override sometimes required for an abstract method?

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

问题描述

基于上一个问题,以下代码编译OK

Based on a previous question, the following code compiles OK

trait Logger {
  def log(msg: String): Unit
}

trait LoggerA extends Logger {
  def log(msg: String) = ???
}

trait LoggerB extends Logger {
  override def log(msg: String) = ???
}

class Logged1 extends LoggerA 
class Logged2 extends LoggerB
class Logged3 extends LoggerA with LoggerB

LoggerA 中不需要override,因为Logger 中没有log 的具体实现.

The override is not required in LoggerA because there is no concrete implementation of log in Logger.

但是,如果我从 LoggerB 中删除 override 它不再编译:

However if I remove the override from LoggerB it no longer compiles:

class Logged3 inherits conflicting members:
  def log(msg: String): Nothing (defined in trait LoggerA) and
  def log(msg: String): Nothing (defined in trait LoggerB)
  (note: this can be resolved by declaring an `override` in class Logged3.)

为什么在这个特定情况中需要override?指定 override 是否会以某种方式改变方法或类?

Why is override required in this specific case? Does specifying override change the method or the class in some way?

推荐答案

我猜它是由 SLS 5.1.4 覆盖

如果 M′ 不是抽象成员,则 M 必须标记为 override.... 或者 M 和 M' 都覆盖在 a 中定义的第三个成员 M''包含 M 和 M' 的类的基类.

If M′ is not an abstract member, then M must be labeled override. ... or both M and M′ override a third member M'' which is defined in a base class of both the classes containing M and M'.

也在部分 5.1.3 类成员与OP有类似的override使用

Also in section 5.1.3 Class Members there is a similar use of override to OP

trait A { def f: Int }
trait B extends A { def f: Int = 1 ; def g: Int = 2 ; def h: Int = 3 }
trait C extends A { override def f: Int = 4 ; def g: Int }
trait D extends B with C { def h: Int }

因此至少它看起来确实是一种特定的行为.

thus at least it does seem a specified behaviour.

基于 fr_andres 的回答学习 Scala,第 9 章.对象、案例类和特征

扩展类 A 和特性 B 和 C 的类实际上是扩展一个类,它扩展另一个类,它扩展另一个类,当编译为 .class 二进制文件.

a class extending class A and traits B and C is actually extending one class, which extends another class, which extends another class, when compiled to the .class binary file.

由于线性化而有效地暗示

which due to linearization implies effectively

LoggerA (has concrete log)
  ^
  |
LoggerB (also has concrete log)
  ^
  |
Logged3

override 在覆盖具体实现时是必需的.

and override is necessary when overriding a concrete implementation.

这篇关于为什么抽象方法有时需要覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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