本文介绍了可堆叠特征模式可以与单例对象一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想对单例对象使用 可堆叠特征模式,但我不能似乎找到了让编译器满意的方法:
I'd like to use the stackable trait pattern with singleton objects, but i can't seem to find how to make the compiler happy:
abstract class Pr { def pr() } trait PrePostPr extends Pr { abstract override def pr() { println("prepr") super.pr() println("postpr") } } object Foo extends Pr with PrePostPr { def pr() = println("Foo") }尝试在 repl 中对此进行评估会产生以下错误:
Trying to evaluate this in the repl produces the following error:
<console>:10: error: overriding method pr in trait PrePostPr of type ()Unit; method pr needs `override' modifier def pr() = println("Foo") 推荐答案可以,但是像这样:
abstract class Pr { def pr() } trait PrePostPr extends Pr { abstract override def pr() { println("prepr") super.pr() println("postpr") } } class ImplPr extends Pr { def pr() = println("Foo") } object Foo extends ImplPr with PrePostPr实现必须存在于超类/超特征之一中.抽象修改特征必须在继承列表中具有实现的类/特征之后.
The implementation has to be present in one of the superclasses/supertraits. The abstract modification trait has to come after the class/trait with the implementation in the inheritance list.
更多推荐
可堆叠特征模式可以与单例对象一起使用吗?
发布评论