一、概述
今天在《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)) } }
三、测试结果
请查看附件中的图片
相关推荐
scala编程 33章 中文pdf Scala编程实战 目录 第1章字符串. 11 第2章数值39 第3章控制结构.60 第4章类和属性.103 第5章方法147 第6章对象170 第7章包和导入.190 第8章特质200 第9章函数式编程214 第10 章集合242 第...
第12讲-Scala编程详解:面向对象编程之类 共12页 第13讲-Scala编程详解:面向对象编程之对象 共9页 第14讲-Scala编程详解:面向对象编程之继承 共13页 第15讲-Scala编程详解:面向对象编程之Trait 共14页 第16讲-...
第12讲-Scala编程详解:面向对象编程之类 共12页 第13讲-Scala编程详解:面向对象编程之对象 共9页 第14讲-Scala编程详解:面向对象编程之继承 共13页 第15讲-Scala编程详解:面向对象编程之Trait 共14页 第16讲-...
第12讲-Scala编程详解:面向对象编程之类 共12页 第13讲-Scala编程详解:面向对象编程之对象 共9页 第14讲-Scala编程详解:面向对象编程之继承 共13页 第15讲-Scala编程详解:面向对象编程之Trait 共14页 第16讲-...
《Scala编程(第3版)》由直接参与Scala开发的一线人员编写,因而对原理的解读和应用的把握更加值得信赖。《Scala编程(第3版)》面向具有一定编程经验的开发者,目标是让读者能够全面了解和掌握Scala编程语言的核心...
第12讲-Scala编程详解:面向对象编程之类 共12页 第13讲-Scala编程详解:面向对象编程之对象 共9页 第14讲-Scala编程详解:面向对象编程之继承 共13页 第15讲-Scala编程详解:面向对象编程之Trait 共14页 第16讲-...
第12讲-Scala编程详解:面向对象编程之类 共12页 第13讲-Scala编程详解:面向对象编程之对象 共9页 第14讲-Scala编程详解:面向对象编程之继承 共13页 第15讲-Scala编程详解:面向对象编程之Trait 共14页 第16讲-...
第12讲-Scala编程详解:面向对象编程之类 共12页 第13讲-Scala编程详解:面向对象编程之对象 共9页 第14讲-Scala编程详解:面向对象编程之继承 共13页 第15讲-Scala编程详解:面向对象编程之Trait 共14页 第16讲-...
第12讲-Scala编程详解:面向对象编程之类 共12页 第13讲-Scala编程详解:面向对象编程之对象 共9页 第14讲-Scala编程详解:面向对象编程之继承 共13页 第15讲-Scala编程详解:面向对象编程之Trait 共14页 第16讲-...
第12讲-Scala编程详解:面向对象编程之类 共12页 第13讲-Scala编程详解:面向对象编程之对象 共9页 第14讲-Scala编程详解:面向对象编程之继承 共13页 第15讲-Scala编程详解:面向对象编程之Trait 共14页 第16讲-...
《Scala编程(第3版)》面向具有一定编程经验的开发者,目标是让读者能够全面了解和掌握Scala编程语言的核心特性,并能够深入理解Scala这门语言在设计取舍背后的动因。即便是不直接使用Scala或从事一线编程工作的...
Scala编程实战(包含源码)完整版Alvin Alexander著.part1.rar
内含Scala编程PDF版书籍 可详细学习Scala编程 内含Scala编程PDF版书籍 可详细学习Scala编程
资源包含《Scala编程》第3版英文版和第3版源代码,Scala编程第3版,目前是最新版,支持Scala 2.11以上。《Scala编程》是Scala语言的创始人参与编写的,涵盖的语法特性非常全面,并且作者解释了为什么这么设计,有...
Scala编程(第三版)中英文,代码全 Martin Odersky 是Scala 编程语言的缔造者。他是瑞士洛桑理工学院(EPFL)的教授,同时也是Typesafe, Inc.(已更名为Lightbend)的创始人。他的研究方向是编程语言和系统,更具体...
spark,scala编程指南第三版,非常好的资料,高清无水印
Scala编程;Scala编程;Scala编程;Scala编程;Scala编程
压缩包内是Scala编程完整版高清PDF文档,解压即可使用。
scala 编程基础课程,通过此文档,你可以快速掌握scala语言的基础开发
Scala编程综合进阶向导有33章。本书介绍了一种新的编程语言,它把面向对象和函数式编程概念有机地结合为整体,从而形成一种完整统一、语义丰富的新思维体系。