可堆叠特征模式可以与单例对象一起使用吗? [英] Can the stackable trait pattern be used with singleton objects?
本文介绍了可堆叠特征模式可以与单例对象一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想对单例对象使用 可堆叠特征模式,但我不能似乎找到了让编译器满意的方法:
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.
这篇关于可堆叠特征模式可以与单例对象一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文