Scala:“覆盖”的目的是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16110370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:12:01  来源:igfitidea点击:

Scala: what is the purpose of 'override'

scala

提问by user1377000

I'm not sure what the purpose of overridekeyword is, in scala. If I have

我不确定override关键字的目的是什么,在 Scala 中。如果我有

trait Shape { def foo(v: Int) }
class Triangle extends Shape { override def foo(v: Int) {} }

it behaves (apparently at least) exactly the same as it does without override.

它的行为(显然至少)与没有override.

回答by axel22

In the case you are implementing an abstract method as in your example, it is not strictly necessary to add the overridemodifier.

如果您在示例中实现抽象方法,则不一定要添加override修饰符。

However, in case you want to overridea concrete method from the superclass, the overridemodifier is necessary. This is to avoid accidental overrides which could happen with mixing composition -- mixing-in traits during some refactoring could easily introduce a method definition that could be overridden by the method defined in the body of the class, hence the need for explicitly stating that a method is an override.

但是,如果您想要override来自超类的具体方法,override则需要使用修饰符。这是为了避免混合组合可能发生的意外覆盖——在某些重构过程中混合特征很容易引入一个方法定义,该方法定义可以被类主体中定义的方法覆盖,因此需要明确说明方法是一个覆盖。

回答by rarry

In your particular case, you got a comprehensive answer from axel22. I just want to add, that there is at least one more case where you may encounter override modifier. The keyword can also be used with trait methods.

在您的特定情况下,您从 axel22 获得了全面的答案。我只想补充一点,至少还有一种情况您可能会遇到覆盖修饰符。关键字也可以与 trait 方法一起使用。

Imagine that you have an abstract class:

想象一下,你有一个抽象类:

abstract class Writer {
  def print(str: String)
}

and its concrete implementation that prints on a console

及其在控制台上打印的具体实现

class ConsoleWriter extends Writer {
  def print(str: String) = println(str)
}

Now, you want to create a trait that will modify its behaviour. Look at the following implementation:

现在,您想要创建一个特性来修改其行为。看下面的实现:

trait Uppercase extends Writer {
  abstract override def print(str: String) = 
    super.print(str.toUpperCase())
}

Notice that a method has two modifiers: abstract and override. This is only allowed for traits and it means that the trait must be mixed into some class that has a concrete definition of the method in question

请注意,方法有两个修饰符:abstract 和 override。这仅适用于 trait,这意味着必须将 trait 混合到某个具有所讨论方法的具体定义的类中

With the definition above, you can do:

使用上面的定义,您可以执行以下操作:

val writer = new ConsoleWriter with Uppercase
writer.print("abc")

which will yield the result

这将产生结果

ABC

美国广播公司

Much in the same vain, you can add more traits:

同样徒劳无功,您可以添加更多特征:

trait WithSpaces extends Writer {
  abstract override def print(str: String) = 
    super.print(str.split("").mkString(" ").tail)
}

Now when you call

现在当你打电话

val writer = new ConsoleWriter with Uppercase with WithSpaces
writer.print("abc")

you will see:

你会看见:

A B C

美国广播公司

The above usage of an override modifier in traits is a distinguishing feature in scala and you won't see it in java.

上面在traits 中使用override 修饰符是scala 中的一个显着特性,你不会在java 中看到它。

回答by ArtemGr

It's for error checking.
Suppose you have

它用于错误检查。
假设你有

trait Shape { def foo(v: Int) = 1 }
class Triangle extends Shape { override def foo(v: Int) = 2 }

and then you change Shape to

然后你将 Shape 更改为

trait Shape { def bar(v: Int) = 1 }

In that case the "override" will tell you that the fooin Triangleoverrides nothing.

在这种情况下,“覆盖”会告诉您fooinTriangle不会覆盖任何内容。

See also:
http://docs.oracle.com/javase/7/docs/api/java/lang/Override.html
http://en.wikipedia.org/wiki/C%2B%2B11#Explicit_overrides_and_final

另见:
http: //docs.oracle.com/javase/7/docs/api/java/lang/Override.html
http://en.wikipedia.org/wiki/C%2B%2B11#Explicit_overrides_and_final