문제

기본 추상 클래스 (특성)가 있습니다. 추상적 인 방법이 있습니다 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에 추가하면 '스택 가능한 특성'패턴이 있습니다. 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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top