문제

Final methods can not be overridden in a subclass. But with the magic of Scala it seems as this is possible.

Consider the following example:

trait Test {
  final def doIt(s: String): String = s
}

object TestObject extends Test {
  def doIt: String => String = s => s.reverse
}

The method doIt in the object TestObject has not the same signature as doIt in trait Test. Thus doIt is overloaded instead of overridden. But a normal call to doIt always calls the method in TestObject:

val x = TestObject.doIt("Hello")                //> x  : String = olleH

Question: How can I call the original method doIt on the TestObject. Is this possible or is this method "sort of overridden"?

도움이 되었습니까?

해결책

You can use TestObject as Test like this:

(TestObject: Test).doIt

다른 팁

Another a little bit hackisch way is to call doIt with named parameter like this:

val x = TestObject.doIt(s = "Hello") 

Try this:

TestObject.asInstanceOf[Test].doIt("Hello")
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top