scala self generic illegal inheritance
Basicaly this is what I am trying to do. A trait that has a member and another trait that inherits that member and initialize it. Then a class that takes a generic type that must be inherited from the first trait and accesses that member
object main extends App{
val z = new c[b]()
z.p()
}
trait a{
val x : String
}
trait b extends a {
val x = 1
}
class c [A <: a] {
self: A =>
def p(): Unit ={
print(x)
}
}
but in the line
val z = new c[b]()
c[b] is underlined and the compiler complains that
"Illegal inheritance, self-type c[b] does not conform to A"
I want to define multiple traits that inherit from a father trait and then define a class that takes one of the fathers child traits and accesses its values
我找到了答案,我只需在创建对象时扩展对象,如下所示:
object main extends App{
val z = new c[b] with b
z.p()
}
trait a{
val x : String
}
trait b extends a {
val x = "1"
}
class c[A <: a]{
this: A =>
def p(): Unit ={
print(x)
}
}
链接地址: http://www.djcxy.com/p/51032.html
上一篇: 你如何设计面向对象的项目?
下一篇: 斯卡拉自我通用的非法继承