scala - 如何获得混合特征的具体类
问题描述
假设我具有以下特征和类别:
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
解决方案
尝试.getClass.getSuperclass.getSimpleName
trait A {
def foo(): Unit
}
trait B extends A {
abstract override def foo(): Unit = {
println("B is instantiated in " + getClass.getSuperclass.getSimpleName)
super.foo()
}
}
class C extends A {
def foo() = {
println("C::foo()")
}
}
val c = new C with B
c.foo()
//B is instantiated in C
//C::foo()
推荐阅读
- java - 将 UTC 时间 HH:mm:ss 转换为设备时间 HH:mm:ss 并添加夏令时(如果有)
- javascript - JQuery $(function).animate 不是函数
- html - XSL 到 HTML:当内部为空时如何保留关闭的 div 标签?
- javascript - FullCalendar v4 动态设置日历的高度
- pandas - 如何将仅包含数字的数据框中的列拆分为熊猫中的多列
- python - Groupby 用于选择多列 Pandas python
- swift - iOS 13 模拟器:reverseGeocodeLocation - [GEOAddressObject] [NSLocale currentLocale] 对 NSLocaleCountryCode@ 失败
- c++ - ::clock 尚未声明
- laravel - 如何获取在线用户(实时)
- unit-testing - Delta Control 中的 KDB+ Q 单元测试分析代码