《Programming In Scala》第10章 Composition and Inheritance 例子。
源代码:
/**
* 《Programming In Scala》第10章 Composition and Inheritance 例子
*/
package layout
//工厂单例对象
object Element {
//数组元素,内容为多行
private class ArrayElement(
val contents: Array[String]
) extends Element
//行元素,内容为单行
private class LineElement(s: String) extends Element {
val contents = Array(s)
override def width = s.length
override def height = 1
}
//单一字符重复填充元素,填充成指定长和宽的元素
private class UniformElement(
ch: Char,
override val width: Int,
override val height: Int
) extends Element {
private val line = ch.toString * width
def contents = Array.make(height, line)
}
//工厂方法,创建多行元素
def elem(contents: Array[String]): Element =
new ArrayElement(contents)
//工厂方法,创建单字填充元素
def elem(chr: Char, width: Int, height: Int): Element =
new UniformElement(chr, width, height)
//工厂方法,创建单行元素
def elem(line: String): Element =
new LineElement(line)
}
//引入所有工厂方法
import Element.elem
//图形元素基类
abstract class Element {
//元素内容
def contents: Array[String]
//元素宽度(字符数)
def width: Int = contents(0).length
//元素高度(字符数)
def height: Int = contents.length
//将自己放在that上面
def above(that: Element): Element = {
val this1 = this widen that.width
val that1 = that widen this.width
elem(this1.contents ++ that1.contents)
}
//将自己放在that的左边
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 =
if (w <= width) this
else {
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 =
if (h <= height) this
else {
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"
}
//主单例对象,包括文件主函数
object Spiral {
//空格元素
val space = elem(" ")
//转角元素
val corner = elem("+")
//构建螺旋图形,nEdges为构成螺旋的边数,direction为螺旋最后一条边的方向
def spiral(nEdges: Int, direction: Int): Element = {
//螺旋最内部元素
if (nEdges == 1)
elem("+")
else {
//递归调用,边数逐次减一
val sp = spiral(nEdges - 1, (direction + 3) % 4)
//竖行
def verticalBar = elem('|', 1, sp.height)
//横行
def horizontalBar = elem('-', sp.width, 1)
//上
if (direction == 0)
(corner beside horizontalBar) above (sp beside space)
//右
else if (direction == 1)
(sp above space) beside (corner above verticalBar)
//下
else if (direction == 2)
(space beside sp) above (horizontalBar beside corner)
//左
else
(verticalBar above corner) beside (space above sp)
}
}
def main(args: Array[String]) {
//打印n边螺旋
val nSides = 6
println(spiral(nSides, 0))
}
}
执行结果:
+-----
|
| +-+
| + |
| |
+---+
分享到:
相关推荐
Scala学习笔记,大全笔记
学习scala笔记,学习scala笔记,学习scala笔记,学习scala笔记,学习scala笔记,
该文档详细的讲述了Scala的基础操作方法和高级使用方法
scala编程 33章 中文pdf Scala编程实战 目录 第1章字符串. 11 第2章数值39 第3章控制结构.60 第4章类和属性.103 第5章方法147 第6章对象170 第7章包和导入.190 第8章特质200 第9章函数式编程214 第10 章集合242 第...
scala 学习笔记 方法 函数 类 对象 特质 推断 注解 编译 隐式转换 尾递归 元组 列表
[Scala学习笔记-中文资料] 从java角度看Scala
ScalaOverview.pdf ScalaTutorial.pdf ProgrammingInScala.pdf(这本是书) 博文链接:https://dogstar.iteye.com/blog/182673
《Scala函数式编程》是针对希望学习FP并将它应用于日常编码中的程序员而写的,内容包括:函数式编程的概念;函数式编程相关的各种“为什么”和“怎么做”;如何编写多核程序;练习和检测。 从OOP到FP,思路的转化 ...
快学Scala完整版,Scala编程(中文版)&Scala程序设计-多线程编程实践,非常清晰,适合阅读
第10讲-Scala编程详解:数组操作之数组转换 共5页 第11讲-Scala编程详解:Map与Tuple 共8页 第12讲-Scala编程详解:面向对象编程之类 共12页 第13讲-Scala编程详解:面向对象编程之对象 共9页 第14讲-Scala编程详解...
第10讲-Scala编程详解:数组操作之数组转换 共5页 第11讲-Scala编程详解:Map与Tuple 共8页 第12讲-Scala编程详解:面向对象编程之类 共12页 第13讲-Scala编程详解:面向对象编程之对象 共9页 第14讲-Scala编程详解...
第10讲-Scala编程详解:数组操作之数组转换 共5页 第11讲-Scala编程详解:Map与Tuple 共8页 第12讲-Scala编程详解:面向对象编程之类 共12页 第13讲-Scala编程详解:面向对象编程之对象 共9页 第14讲-Scala编程详解...
scala学习
内含Scala编程PDF版书籍 可详细学习Scala编程 内含Scala编程PDF版书籍 可详细学习Scala编程
第2章,“Scala的第一步”,展示给你如何使用Scala完成若干种基本编程任务,而不牵涉过多关于如何工作的细节。本章的目的是让你的手指开始敲击并执行Scala代码。 第3章,“Scala的下一步”,演示更多的基本编程任务...
Scala编程(第三版)中英文,代码全 Martin Odersky 是Scala 编程语言的缔造者。他是瑞士洛桑理工学院(EPFL)的教授,同时也是Typesafe, Inc.(已更名为Lightbend)的创始人。他的研究方向是编程语言和系统,更具体...
scala例子 scala例子 scala例子 scala例子 scala例子 scala例子
Scala学习资源Scala编程语言抓住了很多开发者的眼球。如果你粗略浏览Scala的网站,你会觉得Scala是一种纯粹的面向对象编程语言,而又无缝地结合了命令式编程和函数式编程风格。Christopher Diggins认为: 不太久...