将对象声明为“最终”的重点是什么? [英] What's the point of declaring an object as "final"?
本文介绍了将对象声明为“最终”的重点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚注意到可以在Scala中将对象声明为 final
:
I just noticed that it's possible to declare objects as final
in Scala:
final object O
这样做有什么意义?无论如何都不能从对象继承:
What's the point of doing that? One cannot inherit from objects anyway:
object A
object B extends A // not found: type A
推荐答案
并非所有人都这样做,但是:
Not that anyone does this, but:
$ scala -Yoverride-objects
Welcome to Scala version 2.11.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_11).
Type in expressions to have them evaluated.
Type :help for more information.
scala> trait A { object O } ; trait B extends A { override object O }
defined trait A
defined trait B
scala> trait A { final object O } ; trait B extends A { override object O }
<console>:8: error: overriding object O in trait A;
object O cannot override final member
trait A { final object O } ; trait B extends A { override object O }
^
有时人们可能想要这样做。 (例如。)
Possibly sometimes people want to do it. (For instance.)
这篇关于将对象声明为“最终”的重点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文