2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

抽象メソッドをsuper経由で使う ~ abstract override ~

Last updated at Posted at 2016-10-27

よくあることですがあるメソッド overrideする時に元のメソッドをoverrideしたメソッドを呼び出すことがありますね

trait Fuge {
  def doSomething(): Unit = println("hello");
}

trait Hoge extends Fuge {
  override def doSomething(): Unit = super.doSomething()
}

ところでsuper経由で呼び出すメソッドが抽象メソッドだとこういう場合コンパイルが通りません

trait Test {
  def doTest(): Unit
}

trait BeforAndAfterTest extends Test {
  override def doTest(): Unit = {
    println("befor");
    super.doTest();
    println("after");
  }
}

こういう時にoverrideと一緒にabstractもつけるとコンパイルが通ります

trait Test {
  def doTest(): Unit
}

trait BeforAndAfterTest extends Test {
  abstract override def doTest(): Unit = {
    println("befor");
    super.doTest();
    println("after");
  }
}

abstract override それほど使うケースがないのであまり知られていませんね
それでは快適なscala lifeを

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?