1
我在閱讀編程斯卡拉,第三版,由Lex勺子;比爾威納斯;馬丁Odersky,並嘗試沿途的例子。NPE中使用scala Array.fill
繼例如形成書
abstract class Element {
def contents: Array[String]
val height = contents.length
val width = if (height == 0) 0 else contents(0).length
}
class UniformElement(
ch: Char,
override val width: Int,
override val height: Int
) extends Element {
private val line = ch.toString * width
def contents = Array.fill(height)(line)
}
val e: Element = new UniformElement('x', 2, 3)
給顯示java.lang.NullPointerException,在REPL嘗試時,或在Eclipse中的工作表。
但是,如果我改變
private val line = ch.toString * width
到
private def line = ch.toString * width
沒有錯誤observerd。
我無法理解爲什麼? 有人可以解釋嗎?
我使用Scala的2.11.8
編輯
答案後從@acidghost,我更改如下類UniformElement,並沒有得到NPE。 :)
class UniformElement(
ch: Char,
val w: Int,
val h: Int
) extends Element {
override val width: Int = w
override val height: Int = h
private val line = ch.toString * width
def contents = Array.fill(height)(line)
}
有道理。 我從構造函數參數中刪除了重寫,並將其添加到類體中。那沒有錯誤。 (帶更新的類定義的附加問題)。 我想,這是正確的方法? 非常感謝! –
是的,這似乎是正確的方式! – acidghost