我有一些扩展公共超类的案例类,我想使用productElement
方法从超类访问字段(我试图将基类声明为案例类,但我收到一个关于案例类继承危险的可怕警告但仍然不起作用)。
我可以想象一些这样的解决方案:
abstract class A(a: Int) extends Product {
def productArity = 1
def productElement(n: Int) = if (n == 0) a else throw new IndexOutOfBoundsException
}
case class B(b: Int) extends A(1) {
def productArity = super.productArity + 1
def productElement(n: Int) = if (n < super.productArity) super.productElement(n) else ....
}
但它变得如此丑陋,我什至无法完成。
有人知道更好的解决方案吗?