【发布时间】:2012-12-26 12:11:37
【问题描述】:
我正在尝试定义一个特征 C,它扩展了一些特征 A,B,... 所有特征,C 和 A,B,... 实现一个共同特征T。特征C应该通过在A,B,..中调用T的实现来实现T:
trait T{
def f()
}
trait A extends T{
def f(){
print("A")
}
}
trait B extends T{
def f(){
print("B")
}
}
trait C 的期望行为如下:
val x=new A with B with C[A,B]{}
x.f()
// should produce output
A
B
这里我尝试定义 trait C,但编译错误:
trait C[A<:T,B<:T] extends T{
self:A with B =>
override def f(){
// error: A does not name a parent class of trait C
super[A].f()
// error: B does not name a parent class of trait C
super[B].f()
}
}
我需要在C 方法A.f() 和B.f() 中调用。
有什么解决办法吗?
【问题讨论】:
标签: scala traits abstract-type