ProgramingTip

Scala에서 특성 및 추상 메서드 재정의

bestdevel 2020. 12. 1. 19:12
반응형

Scala에서 특성 및 추상 메서드 재정의


기본 추상 클래스 (특성)가 있습니다. 거리 인 방법이 foo()있습니다. 파생 파생 클래스에 의해 확장되고 구현됩니다. 파생 클래스에 혼합 할 수있는 특성을 foo()파생 클래스의 foo().

다음과 같은 것 :

trait Foo {
  def foo()
}

trait M extends Foo {
  override def foo() {
    println("M")
    super.foo()
  }
}

class FooImpl1 extends Foo {
  override def foo() {
    println("Impl")
  }
}

class FooImpl2 extends FooImpl1 with M 

자체 유형과 구조 유형을 시도했지만 작동하지 않습니다.


당신은 아주 가까웠습니다. M.foo에 추상 수정 튼튼 추가하면 'Stackable Trait'패턴이 생생합니다 : http://www.artima.com/scalazine/articles/stackable_trait_pattern.html

trait Foo {
  def foo()
}

trait M extends Foo {
  abstract override def foo() {println("M"); super.foo()}
}

class FooImpl1 extends Foo {
  override def foo() {println("Impl")}
}

class FooImpl2 extends FooImpl1 with M

참고 URL : https://stackoverflow.com/questions/2038370/traits-and-abstract-methods-override-in-scala

반응형