ArrayElement(Array("hello")) above
ArrayElement(Array("world!"))
和的相似下面表达式也不能正常工作第个ArrayElement高度为 2而第 2个高度只是: ArrayElement(Array("one", "two")) beside
ArrayElement(Array("one"))
代码10.13展示了个私有帮助思路方法widen能够带个宽度做参数并返回那个宽度Element结果包含了这个Element内容居中左侧和右侧留需带空格以获得需要宽度代码10.13还展示了个类似思路方法heighten能在竖直方向执行同样功能widen思路方法被above以确保Element堆叠在起有同样宽度类似heighten思路方法被beside以确保靠在起元素具有同样高度有了这些改变布局库可以待用了
import Element.elem
abstract Element {
def contents: Array[String]
def width: Int = contents(0).length
def height: Int = contents.length
def above(that: Element): Element = {
val this1 = this widen that.width
val that1 = that widen this.width
elem(this1.contents that1.contents)
}
def beside(that: Element): Element = {
val this1 = this heighten that.height
val that1 = that heighten this.height
elem(
for ((line1, line2) < - this1.contents zip that1.contents)
yield line1 + line2
)
}
def widen(w: Int): Element =
(w < = width) this
{
val left = elem(' ', (w - width) / 2, height)
var right = elem(' ', w – width - left.width, height)
left beside this beside right
}
def heighten(h: Int): Element =
(h < = height) this
{
val top = elem(' ', width, (h - height) / 2)
var bot = elem(' ', width, h – height - top.height)
top above this above bot
}
override def toString = contents mkString "\n"
}
代码 10.13 有了widen和heighten思路方法Element
最新评论