Scala Mixin with self=> traits



假设我有一个基本特征,我的特征正在扩展基本特征。我想创建一个具有混合 self=> 特征的类,并且可以动态调用重载。

以下是解决方案

package minxin.neel.test
/**
* Created by Neelamadhab.
*/
class TestMix2 {
def hello = println("Hello NEEL...")
}
class BaseMixInClass {
self: BaseTrait =>
self.method
}
trait BaseTrait {
def method = println("Inside method in BaseTrait")
} 
trait Trait2 extends BaseTrait {
override def method = println("Hello, Inside method in Trait2")
}
trait Trait3 extends BaseTrait {
override def method = println("Hello, Inside method in Trait3")
}
trait Trait4 extends BaseTrait

object MyMixinTest extends App {
new BaseMixInClass with Trait2
new BaseMixInClass with Trait3
new BaseMixInClass with Trait4
new BaseMixInClass with BaseTrait
}

输出将是:

Hello, Inside method in Trait2
Hello, Inside method in Trait3
Inside method in BaseTrait
Inside method in BaseTrait

最新更新