如何访问“覆盖” Scala 中的内部类?

发布于 2024-08-24 14:12:17 字数 584 浏览 2 评论 0原文

我有两个特征,一个扩展另一个,每个都有一个内部类,一个扩展另一个,具有相同的名称:

trait A {
    class X {
        def x() = doSomething()
    }
}

trait B extends A {
    class X extends super.X {
        override def x() = doSomethingElse()
    }
}

class C extends B {
    val x = new X() // here B.X is instantiated
    val y = new A.X() // does not compile
    val z = new A.this.X() // does not compile
}

How do I access AX class in the C class's身体?重命名 BX 而不是隐藏 AX 并不是首选方法。

让事情变得有点复杂的是,在我遇到这个问题的情况下,特征具有类型参数(本示例中未显示)。

I have two traits, one extending the other, each with an inner class, one extending the other, with the same names:

trait A {
    class X {
        def x() = doSomething()
    }
}

trait B extends A {
    class X extends super.X {
        override def x() = doSomethingElse()
    }
}

class C extends B {
    val x = new X() // here B.X is instantiated
    val y = new A.X() // does not compile
    val z = new A.this.X() // does not compile
}

How do I access A.X class in the C class's body? Renaming B.X not to hide A.X is not a preferred way.

To make things a bit complicated, in the situation I have encountered this problem the traits have type parameters (not shown in this example).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

trait A {
    class X {
        def x() = "A.X"
    }
}

trait B extends A {
    class X extends super.X {
        override def x() = "B.X"
    }
}

class C extends B {
  val self = this:A
  val x = new this.X()
  val y = new self.X()
}

scala> val c = new C
c: C = C@1ef4b

scala> c.x.x 
res0: java.lang.String = B.X

scala> c.y.x
res1: java.lang.String = A.X
trait A {
    class X {
        def x() = "A.X"
    }
}

trait B extends A {
    class X extends super.X {
        override def x() = "B.X"
    }
}

class C extends B {
  val self = this:A
  val x = new this.X()
  val y = new self.X()
}

scala> val c = new C
c: C = C@1ef4b

scala> c.x.x 
res0: java.lang.String = B.X

scala> c.y.x
res1: java.lang.String = A.X
爱给你人给你 2024-08-31 14:12:17

对于那些对这个奇异问题感兴趣的人,我发现它也可以用作函数的返回值。由于我的特征 A 和 B 有类型参数,因此应该会导致更简洁的代码:

trait A[T, U, V] {
    class X {
        def x() = "A.X"
    }

    def a = this:A[T, U, V]
}

trait B[T, U, V] extends A[T, U, V] {
    class X extends super.X {
        override def x() = "B.X"
    }
}

class C extends B[SomeClass, SomeOtherClass, ThirdOne] {
    val aVerbose = this:A[SomeClass, SomeOtherClass, ThirdOne] // works but is a bit ugly
    val aConcise = a
    val x = new this.X()
    val y = new aVerbose.X()
    val z = new aConcise.X()
}

scala> val c = new C()
c: C = C@1e852be

scala> c.x.x()
res2: java.lang.String = B.X

scala> c.y.x()
res3: java.lang.String = A.X

scala> c.z.x()
res4: java.lang.String = A.X

For those interested in this exotic issue, I have discovered it works also as a return value of a function. Since my traits A and B have type parameters, it should lead to more concise code:

trait A[T, U, V] {
    class X {
        def x() = "A.X"
    }

    def a = this:A[T, U, V]
}

trait B[T, U, V] extends A[T, U, V] {
    class X extends super.X {
        override def x() = "B.X"
    }
}

class C extends B[SomeClass, SomeOtherClass, ThirdOne] {
    val aVerbose = this:A[SomeClass, SomeOtherClass, ThirdOne] // works but is a bit ugly
    val aConcise = a
    val x = new this.X()
    val y = new aVerbose.X()
    val z = new aConcise.X()
}

scala> val c = new C()
c: C = C@1e852be

scala> c.x.x()
res2: java.lang.String = B.X

scala> c.y.x()
res3: java.lang.String = A.X

scala> c.z.x()
res4: java.lang.String = A.X
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文