我正在阅读“Scala 编程”中的第 10 章(组合和继承),我对代码清单 10.9 有点困惑。作者的代码清单如下:
abstract class Element{
def contents:Array[String]
def width: Int = if(height==0)0elsecontents(0).length
def height: Int = contents.length
def above(that: Element): Element =
new ArrayElement(this.contents ++ that.contents)
def beside(that: Element): Element=
new ArrayElement(
for(
(line1, line2)<-this.contents zip that.contents
) yield line1+line2
)
override def toString = contents mkString "\n"
}
我的困惑在于“上面”和“旁边”方法,这些方法是从抽象基类 Element 实例化一个子类 ArrayElement。这实际上是正确的还是我在这里遗漏了什么?