Calling a method on the superclass in a self-typed trait in scala

折月煮酒 提交于 2019-11-30 17:07:04

You need abstract override and no self type for that.

trait OtherStuff extends Foo {                                
  abstract override def bar() = super.bar() + " with OtherStuff"
}

Then class Quux extends Foo with OtherStuff does what you want.

This article might be of interest.

or you can do a overloading like the following

class Foo {
  def bar() : String = "Foos bar"}
trait OtherStuff {
  self : Foo =>
  def bar( s : String) : String = self.bar() + s}

class Quux extends Foo with OtherStuff
(new Quux).bar(" with other stuff")

the thing is, with self type annotation, the "other stuff" defined in OtherStuff is part of Foo when the Trait is mixed with Foo, rather than a sub-type relationship.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!