我有一个特性,扩展了两个具有相同名称的函数,但它与内部有点不同,我想知道如何知道将调用哪个函数?
我的特性B
有print()
,特质C
有print()
,如果我这样继承它们:
trait A extends B with C {
def print()
}
每个印刷品印刷其他东西,哪个印刷品会被调用?
答案 0 :(得分:6)
在您遇到名称冲突的特定情况下,您将收到编译时错误。假设D
是实现类:
class D extends A with C with B
def main(args: Array[String]): Unit = {
val d = new D
println(d.print())
}
你会看到:
Error:(25, 9) class D inherits conflicting members:
method print in trait B of type ()Unit and
method print in trait C of type ()Unit
(Note: this can be resolved by declaring an override in class D.)
class D extends A with B with C
但是,如果我们通过向override print()
添加D
来帮助编译器,并使其调用super.print()
,它将打印支持{{{1}的行列中的最后一个特征。 1}}方法,即:
print
我们得到“世界”。如果我们切换trait A { }
trait B { def print() = println("hello") }
trait C { def print() = println("world") }
class D extends A with B with C {
override def print(): Unit = super.print()
}
和B
:
C
我们得到“你好”。
答案 1 :(得分:1)
原始的Schärli,Ducassé,Nierstrasz,Black论文中Traits最重要的特征之一是通过重命名和隐藏解决冲突。 Scala的Traits完全没有这个功能。
在Scala中,根本不允许发生冲突。它们被类型系统检测到并拒绝。 (原始论文是在Smalltalk的背景下,没有类型系统,因此使用了不同的方法。)
答案 2 :(得分:0)
Scala编译器为您提供编译错误。
为什么不自己使用Scala REPL看到它。
scala> trait B { def print(): Unit = println("B") }
defined trait B
scala> trait C { def print(): Unit = println("C") }
defined trait C
scala> trait A extends B with C { def foo = print() }
cmd11.sc:1: trait A inherits conflicting members:
method print in trait B of type ()Unit and
method print in trait C of type ()Unit
(Note: this can be resolved by declaring an override in trait A.)
trait A extends B with C { def foo = print() }
^
Compilation Failed
我认为您可以轻松理解使用编译器错误