【问题标题】:How to access inner classes via an explicit namespace?如何通过显式命名空间访问内部类?
【发布时间】:2014-11-05 01:41:10
【问题描述】:
trait Base
{
    val widget = new Widget {}

    trait Widget
}

trait Child1 extends Base
{
    override val widget = new Widget {}

    trait Widget extends super.Widget
}

trait Child2 extends Base
{
    override val widget = new Widget {}

    trait Widget extends super.Widget
}

class Sample extends Child1 with Child2
{
    override val widget = // Here be dragons
}

有没有办法在 Sample 中正确组合小部件?如果 Child1#Widget 和 Child2#Widget 有一个专有名称,它工作正常,但我很好奇是否可以像这样访问它们。

new Child1#Widget with Child2#Widget 失败了:“不是构造函数的合法前缀”

override val widget =
{
    val a = this: Child1
    val b = this: Child2

    new a.Widget with b.Widget {}
}

“非法继承:继承不同类型的trait Widget实例”

【问题讨论】:

    标签: scala inheritance inner-classes traits


    【解决方案1】:

    你可以试试这个:

    class Sample extends Child1 with Child2 {
      override val widget = new Widget {}
      trait Widget extends super[Child1].Widget with super[Child2].Widget
    }
    

    或者只是:

    class Sample extends Child1 with Child2 {
      override val widget = new super[Child1].Widget with super[Child2].Widget {}
    }
    

    【讨论】:

    猜你喜欢
    • 2019-02-05
    • 2016-11-02
    • 2015-01-08
    • 2018-11-20
    • 2021-09-28
    • 2013-11-07
    • 1970-01-01
    • 2011-10-11
    • 2011-12-31
    相关资源
    最近更新 更多