Scala 組合和繼承–小結(jié)

2018-09-28 18:19 更新

組合和繼承–小結(jié)

前面我們基本完成了布局元素的函數(shù)庫,現(xiàn)在我們就可以寫個程序來使用這個函數(shù)庫,下面顯示螺旋線的程序如下:

object Spiral {
  val space = elem (" ")
  val corner = elem ("+")
  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]) {
   val nSides=args(0).toInt
   println(spiral(nSides,0))
 }
}

因為 Sprial 為一單例對象,并包含 main 方法,因此它為一 Scala 應(yīng)用程序,可以在命令行使用 scala Sprial xx 來運行這個應(yīng)用。

root@mail:~/scala# scala Spiral 5
+----
|    
| ++ 
|  | 
+--+ 
root@mail:~/scala# scala Spiral 23
+----------------------
|                      
| +------------------+ 
| |                  | 
| | +--------------+ | 
| | |              | | 
| | | +----------+ | | 
| | | |          | | | 
| | | | +------+ | | | 
| | | | |      | | | | 
| | | | | +--+ | | | | 
| | | | | |  | | | | | 
| | | | | ++ | | | | | 
| | | | |    | | | | | 
| | | | +----+ | | | | 
| | | |        | | | | 
| | | +--------+ | | | 
| | |            | | | 
| | +------------+ | | 
| |                | | 
| +----------------+ | 
|                    | 
+--------------------+ 

這個例子的完整代碼如下:

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.fill(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 height: Int = contents.length
  def width: Int =  contents(0).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
    Element.elem(
      for(
        (line1,line2) <- this1.contents zip that1.contents
      ) yield line1+line2
    )
  }
  def widen(w: Int): Element =
  if (w <= width) this
  else {
    val left = Element.elem(' ', (w - width) / 2, height)
        var right = Element.elem(' ', w - width - left.width, height)
        left beside this beside right
  }
def heighten(h: Int): Element =
  if (h <= height) this
  else {
    val top = Element.elem(' ', width, (h - height) / 2)
        var bot = Element.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 ("+")
  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]) {
   val nSides=args(0).toInt
   println(spiral(nSides,0))
 }
}

到目前為止,你看到了 Scala 里與面向?qū)ο缶幊逃嘘P(guān)的更多的概念。其中,你遇到了抽象類,繼承和派生,類層次,參數(shù)化字段,及方法重載。你應(yīng)當(dāng)已經(jīng)建立了在 Scala 里構(gòu)造不太小的類層次的感覺。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號