2014-09-27 84 views

回答

30

不是說有人這樣做,但:

$ 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 } 
                     ^

可能有時人們想這樣做。 (For instance.

+0

這是完全正確的。不過,我花了一段時間才說服自己。有關更詳細,痛苦的細節,請參閱[此重複問題]中的更新(http://stackoverflow.com/questions/30265070/whats-the-point-of-nonfinal-singleton-objects-in-scala)。另見[這個問題](http://stackoverflow.com/questions/17308141/what-are-the-subtle-differences-between-val-and-singleton-objects) – 2015-05-15 18:11:51

相關問題