用对象覆盖方法

时间:2011-09-28 13:56:20

标签: scala override

为什么可以使用object覆盖空括号方法?

trait A {
  def meth = {}
  def meth_p() = {}
}

class B extends A {
  object meth_p
} // compiles

覆盖没有括号的方法无法编译:

class B1 extends A {
  object meth
} // does not compile

以下任何组合都不起作用(没有override修饰符):

class BX extends A {
  // of course, each declaration should get its own class
  def meth = {}
  def meth_p() = {}
  def meth() = {}
  def meth_p = {}
  val meth = {}
  val meth_p = {}
  // ...
}

这是有记录且有用的行为吗?由于这种意外覆盖,我只是碰到了一个非常微妙的错误。

1 个答案:

答案 0 :(得分:6)

这肯定看起来像个臭虫。如果你这样做,反过来就会变得更奇怪。

class A {
   object m { override def toString = "object m" }
}

class B extends A {
   def m() = "def m"
}

scala> (new A).m
res0: object A#m = object m

scala> (new A).m() // doesn't compile

scala> (new B).m
<console>:10: error: ambiguous reference to overloaded definition,
both method m in class B of type ()java.lang.String
and  object m in class A of type object B#m
match expected type ?
              (new B).m
                      ^
scala> (new B).m()
java.lang.VerifyError: class B overrides final method m.()LA$m$;
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at scala.tools.nsc.interpreter.AbstractFileClassLoader.findClass(AbstractFileClassLoader.scala:52)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at scala.tools.nsc.interpreter.AbstractFileClassLoader.scala$tools$nsc$util$ScalaClassLoader$$super$loadClass(AbstractFileClassLoader.scala:17)
        at scala.tools.nsc.util.ScalaClassLoader$class.loadClass(ScalaClassLoader.scala:50)
        at scala.tools.nsc.interpreter.AbstractFileClassLoader.loadClass(AbstractFileClassLoader.scala:17)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at .<init>(<console>:10)
        at .<clinit>(<console>)
        at .<init>(<console>:11)
        at .<clinit>(<console>)
        at $print(<console>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)
        at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920)
        at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43)
        at scala.tools.nsc.io.package$$anon$2.run(package.scala:25)
        at java.lang.Thread.run(Unknown Source)