快学Scala 第八章习题答案

来源:互联网 发布:aris软件流程 编辑:程序博客网 时间:2024/05/01 23:44

8.1 扩展如下的BankAccount类

新类CheckingAccount对每次存款和取款都收取1美元的手续费

class BankAccount(initialBalance:Double){  private var balance = initialBalance  def deposit(amount:Double) = {balance += amount;balance}  def withdraw(amount:Double) = {balance -= amount;balance}}
class CheckingAccount(initialBalance:Double) extends BankAccount(initialBalance){    override def deposit(amount:Double) = {        super.deposit(amount - 1)    }    override def withdraw(amount:Double) = {    super.withdraw(amount + 1)    }}

8.2 扩展一个练习的BankAccount类

新类SavingAccount每个月都有利息产生(earnMonthlyInterest方法被调用),
并且有每月三次免手续费存款或取款。在earnMonthlyInterest方法中重置交易计数。

class SavingAccount extends BankAccount{    private var num:Int = _    def earnMonthlyInterest()={        num = 3        super.deposit(1)    }    override def deposit(amount:Double):Double = {    num -= 1    if(num < 0) super.deposit(amount - 1)    else super.deposit(amount)    }    override def withdraw(amount:Double):Double={    num -= 1    if(num < 0) super.withdraw(amount + 1)    else super.withdraw(amount)    }}

8.3 翻开你喜欢的Java或C++教科书

一定会找到用来讲解继承层级的示例,可能是员工,宠物,图形或类似的东西。
用Scala来实现这个示例。

//Thinking in Java中的代码class Art{  Art(){    System.out.println("Art constructor");  }}class Drawing extends Art{  Drawing(){    System.out.println("Drawing constructor");  }}public class Cartoon extends Drawing{  public Cartoon(){    System.out.println("Cartoon constructor");  }}
// Scala代码class Art{    println("Art constructor")}class Drawing extends from Art{    println("Drawing constructor")}class Cartoon extends from Art{    println("Cartoon constructor")}

8.4定义一个抽象类Item

加入方法price和description。SimpleItem是一个在构造器中给出价格和描述的物件。
利用val可以重写def 这个事实。Bundle是一个可以包含其他物件的物件。其价格是打包中所有物件的价格之和。
同时提供一个将物件添加到打包当中的机制,以及一个合适的description方法。

abstract class Item{    def price():Double    def description():String    override def toString():String={        "description:" + description() + " price:" + price()    }}class SimpleItem(val price:Double,val description:String) extends Item{}class Bundle extends Item{    val items = new ArrayBuffer[Item]()    def addItem(item:Item){        items += item    }    def price():Double = {        var total = 0        items.foreach(total += _.price())        total    }    def description():String={        item.mkString(" ")    }}

8.5设计一个Point类

其x和y坐标可以通过构造器提供。提供一个子类LabeledPoint,其构造器接受一个标签值和x,y坐标,比如:new LabeledPoint(“Black Thursday”,1929,230.07)

class Point(x:Int,y:Int){}class LabeledPoint(label:String,x:Int,y:Int) extends from Point(x,y){}

8.6定义一个抽象类Shape

一个抽象方法centerPoint,以及该抽象类的子类Rectangle和Circle。为子类提供合适的构造器,并重写centerPoint方法

abstract class Shape{    def centerPoint()}class Rectangle(startX:Int,StartY:Int,endX:Int,endY:Int) extends Shape{    def centerPoint()   }class Circle(x:Int,y:Int,radius:Double) extends Shape{    def centerPoint(){}}

8.7提供一个Square类

扩展自java.awt.Rectangle并且是三个构造器:一个以给定的端点和宽度构造正方形,一个以(0,0)为端点和给定的宽度构造正方形,一个以(0,0)为端点,0为宽度构造正方形。

import java.awt.{Point,Rectangle}class Square(Point:Point,width:Int) extends Rectangle(point.x, point.y, width, width){    def this(){        this(new Point(0,0),0)    }    def this(){        this(new Point(0,0),width)    }}

8.8编译8.6节中的Person和SecretAgent类

并利用javap分析类文件,总共有多少name的getter方法?它们分别取什么值?(提示:可以使用-c 和 -private 选项)
总共两个。Person中取得的是传入的name,SecretAgent中取得的是默认的”secret”

9.在8.10节的Creature类中,将val range替换成一个def。

如果你在Ant子类中也用def的话会有什么效果?如果在子类中使用val又会有什么效果?为什么?
在Ant中使用def没有问题。但是如果使用val则无法编译。因为val只能重写不带参数的def。这里的def是带参数的

10. 文件scala/collection/immutable/Stack.scala包含如下定义:

class Stack[A] protected (protected val elems: List[A])

请解释protected关键字的含义。(提示:回顾我们在第五章关于私有构造器的讨论)
此构造器只能被其子类来调用,而不能被外界直接调用。

0 0