一、概述
今天在《Scala In Programming》中学习了关于Scala的组合与继承,下面通过一个实例作为知识的巩固。
二、实例展示
1、实例名称:
根据给定的边界值,画出一个螺旋。
2、实例描述:
a、首先给出四个类:Element、ArrayElement、UniformElement、LineElemnt,其中Element为抽象的超类,其余三个类分别继承于Element。
import scalaDemo.Element._ /** * Created by user on 2016/1/18. */ abstract class Element { def contents: Array[String] def height: Int = contents.length def width: Int = contents(0).length def above(that: Element): Element = { val thisParam = this widen that.width val thatParam = that widen this.width elem(thisParam.contents ++ thatParam.contents) } def beside(that: Element): Element = { val thisParam = this highten that.height val thatParam = that highten this.height elem( for((line1,line2) <- thisParam.contents zip thatParam.contents) yield line1 + line2 ) } def widen(w: Int): Element = { if(w <= width) this else{ val left = elem(' ',(w - width)/2,height) val right = elem(' ', w - width - left.width, height) left beside this beside right } } def highten(h: Int): Element = { if(h <= height){ this }else{ val top = elem(' ', width, (h - height)/2) val bot = elem(' ', width, h - height - top.height) top above this above bot } } override def toString = contents.mkString("\n") }
/** * Created by user on 2016/1/18. */ class ArrayElement(contentsParam: Array[String]) extends Element{ override def contents: Array[String] = { contentsParam } }
/** * Created by user on 2016/1/18. */ class UniformElement(char: Char, width: Int, height: Int) extends Element{ private val line = char.toString * width override def contents: Array[String] = { Array.apply(line + ": " + height) } }
/** * Created by user on 2016/1/18. */ class LineElement(line: String) extends Element{ override def contents: Array[String] = { Array.apply(line) } }
b、定义一个object:Element
import scalaDemo.classes.{ArrayElement, LineElement, UniformElement, Element} /** * Created by user on 2016/1/18. */ object Element { def elem(contents: Array[String]): Element = { new ArrayElement(contents) } def elem(char: Char, width: Int, height: Int): Element = { new UniformElement(char,width,height) } def elem(line: String): Element = { new LineElement(line) } }
c、测试实例
import scalaDemo.Element.elem import scalaDemo.classes.Element /** * Created by user on 2016/1/18. */ object Spiral{ val space = elem(" ") val corner = elem("+") def spiral(edges: Int, direction: Int): Element = { if(edges == 1){ elem("+") }else{ val sp = spiral(edges - 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]) { val constents = new Array[String](3) constents(0) = "AAAAAAA" + "," constents(1) = "BBBBBBB" + "," constents(2) = "CCCCCCC" println(spiral(constents.length,3)) } }
三、测试结果
请查看附件中的图片