【发布时间】:2020-11-29 05:50:53
【问题描述】:
假设我具有以下特征和类别:
trait A {
def foo(): Unit
}
trait B extends A {
abstract override def foo(): Unit = {
// Can I determine the name of the concrete class here?
super.foo()
}
}
class C extends A {
def foo() = {
println("C::foo()")
}
}
val c = new C with B
c.foo()
有没有办法从特征 B 中确定实例化它的具体类的名称?即 C
【问题讨论】:
-
有没有办法从特征 B 中确定实例化它的具体类的名称?即 C
B未在C中实例化。 -
你的意思是
val c = new C with B? -
好吧,我相信你可以打电话给
this.getClass.getName但是,你为什么想要类名呢?你想用这些信息做什么? -
坦率地说,这听起来像是一个坏主意。如果您的 trait 需要了解扩展它们的类,那么您的设计就失败了。
-
但是在这种情况下,“最终演员”不是 C,它是一个匿名类。
标签: scala inheritance overriding