【发布时间】:2016-10-29 15:10:34
【问题描述】:
我有类似下面的代码(我简化了它):
trait A {
val CONST_VALUE = 10
}
class B(someValue: Int, values: Array[Int]) extends A {
//some methods
}
object B {
def apply(someValue: Int) = B(someValue, Array.ofDim[Array[Byte]](someValue).map(block => Array.fill[Byte](A.CONST_VALUE)(0)))
}
基本上,我在特征A 中声明了一个常量CONST_VALUE。我正在尝试在伴随对象B 中使用它来实例化类B。但是,我无法从伴随对象 B 访问 A.CONST_VALUE。(我遇到编译错误)。
那我该怎么做呢?
【问题讨论】:
-
旁注 - 为了使变量成为常量,您需要添加
final关键字:final val CONST_VALUE = 10。
标签: scala