我可以private final
在 Scala 中使用修饰符做什么?
鉴于以下代码:
1| class A { def callFoo = foo; private final def foo = "bar of A" }
2| class B extends A { private final def foo = "bar of B"}
3| println((new A()).callFoo)
4| println((new B()).callFoo)
第 3 行和第 4 行打印:
1| bar of A
2| bar of A
可以理解为什么第 2 行不打印bar of B
,因为实际上有两个foo
定义,而 B 中的后者不会覆盖 A 中的前者。否则 Scala 将需要override
- 而不是final
修饰符。
那么为什么 Scala 不简单地禁止修饰符的组合private final
呢?