1. 实现一个Bug类,对沿着水平线爬行的虫子建模。move方法向当前方向移动,turn方法让虫子转身,show方法打印出当前的位置。让这些方法可以被串接调用。例如:
bugsy.move(4).show().move(6).show().turn().move(5).show()
上述代码应显示 4 10 5。
package ex18_01 class Bug { var x = 0 var y = 0 var curr_direction = 0 def move(len: Int) = { curr_direction match { case 0 => x += len case 1 => y += len case 2 => x -= len case 3 => y -= len } this } def turn() = { curr_direction = (curr_direction + 1) % 4 this } def show() = { curr_direction match { case 0 => print(x + " ") case 1 => print(y + " ") case 2 => print(x + " ") case 3 => print(y + " ") } this } } object Main extends App { val bugsy = new Bug bugsy.move(4).show().move(6).show().turn().move(5).show() } /*output: 4 10 5 */
2. 为前一个练习中的Bug类提供一个流利接口,达到能编写如下代码的效果:
bugsy move 4 and show and then move 6 and show turn around move 5 and show
解答://TODO
3. 完成18.1节中的流利接口,以便我们可以做出如下调用:
book set Title to "Scala for the Impatient" set Author to "Cay Horstmann"
package ex18_03 object Main extends App { val book = new Document book set Title to "Scala for the Impatient" set Author to "Cay Horstmann" println(book) } // 知识点: 单例类型 object Title // This object is used as an argument for a fluent interface object Author class Document { private var title = "" private var author = "" private var useNextArgAs: Any = null def set(obj: Title.type): this.type = { useNextArgAs = obj; this } def set(obj: Author.type): this.type = { useNextArgAs = obj; this } def to(arg: String): this.type = { if (useNextArgAs == Title) title = arg if (useNextArgAs == Author) author = arg this } override def toString = getClass.getName + "[title=" + title + ", author=" + author + "]" } /*output: eg18_01_b.Document[title=Scala for the Impatient] */
4. 实现18.2节中被嵌套在Network类中的Member类的equals方法。两个成员要想相等,必须属于同一个网络。
解答://TODO
5. 考虑如下类型别名
type NetworkMember = n.Member forSome { val n: Network }
和函数
def process(m1: NetworkMember, m2: NetworkMember) = (m1, m2)
这与18.8节中的process函数有什么不同?
解答:该函数接受相同或不同网络的成员,而18.8节中的process函数则拒绝那些来自不同网络的成员。
package ex18_05 // 知识点: 存在类型 import javax.swing._ import scala.collection.mutable._ object Main extends App { type NetworkMember = n.Member forSome { val n: Network } def process(m1: NetworkMember, m2: NetworkMember) = (m1, m2) val chatter = new Network val myFace = new Network val fred = chatter.join("Fred") val wilma = chatter.join("Wilma") val barney = myFace.join("Barney") process(fred, wilma) // OK 接受相同网络的成员 process(fred, barney) // OK 接受不同网络的成员 fred.contacts += wilma // OK //fred.contacts += barney //ERROR } class Network { class Member(val name: String) { val contacts = new ArrayBuffer[Member] override def toString = getClass.getName + "[name=" + name + ",contacts=" + contacts.map(_.name).mkString("[", ",", "]") + "]" } private val members = new ArrayBuffer[Member] def join(name: String): Member = { val m = new Member(name) members += m m } override def toString = getClass.getName + "[members=" + members + "]" } /*output: */
6.
7.
8.
9.
10.
时间: 2024-08-24 20:03:27