快學Scala習題解答—第八章 繼承

[size=large][b]9 繼承[/b][/size]
[b]9.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}
}[/b]
繼承語法的使用。代碼如下
class CheckingAccount(initialBalance:Double) extends BankAccount(initialBalance){
override def deposit(amount: Double): Double = super.deposit(amount - 1)

override def withdraw(amount: Double): Double = super.withdraw(amount + 1)
}


[b]9.2 擴展前一個練習的BankAccount類,新類SavingsAccount每個月都有利息產生(earnMonthlyInterest方法被調用),並且有每月三次免手續費的存款或取款。在earnMonthlyInterest方法中重置交易計數。[/b]
class SavingsAccount(initialBalance:Double) extends BankAccount(initialBalance){
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)
}
}


[b]9.3 翻開你喜歡的Java或C++教科書,一定會找到用來講解繼承層級的實例,可能是員工,寵物,圖形或類似的東西。用Scala來實現這個示例。[/b]
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 Art{
println("Drawing constructor")
}

class Cartoon extends Drawing{
println("Cartoon constructor")
}


[b]9.4 定義一個抽象類Item,加入方法price和description。SimpleItem是一個在構造器中給出價格和描述的物件。利用val可以重寫def這個事實。Bundle是一個可以包含其他物件的物件。其價格是打包中所有物件的價格之和。同時提供一個將物件添加到打包當中的機制,以及一個適合的description方法[/b]
import collection.mutable.ArrayBuffer


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 = 0d
items.foreach(total += _.price())
total
}

def description(): String = {
items.mkString(" ")
}
}


[b]9.5 設計一個Point類,其x和y座標可以通過構造器提供。提供一個子類LabeledPoint,其構造器接受一個標籤值和x,y座標,比如:new LabeledPoint("Black Thursday",1929,230.07)[/b]
class Point(x:Int,y:Int){
}

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


[b]9.6 定義一個抽象類Shape,一個抽象方法centerPoint,以及該抽象類的子類Rectangle和Circle。爲子類提供合適的構造器,並重寫centerPoint方法[/b]
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() {}
}

[b]

9.7 提供一個Square類,擴展自java.awt.Rectangle並且是三個構造器:一個以給定的端點和寬度構造正方形,一個以(0,0)爲端點和給定的寬度構造正方形,一個以(0,0)爲端點,0爲寬度構造正方形[/b]
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(width:Int){
this(new Point(0,0),width)
}
}


[b]9.8 編譯8.6節中的Person和SecretAgent類並使用javap分析類文件。總共有多少name的getter方法?它們分別取什麼值?(提示:可以使用-c和-private選項)[/b]
總共兩個。Person中取得的是傳入的name,而SecretAgent中取得的是默認的"secret"

[b]9.9 在8.10節的Creature類中,將val range替換成一個def。如果你在Ant子類中也用def的話會有什麼效果?如果在子類中使用val又會有什麼效果?爲什麼?[/b]
在Ant中使用def沒有問題。但是如果使用val則無法編譯。因爲val只能重寫不帶參數的def。這裏的def是帶參數的

[b]9.10 文件scala/collection/immutable/Stack.scala包含如下定義:
class Stack[A] protected (protected val elems: List[A])
請解釋protected關鍵字的含義。(提示:回顧我們在第5章中關於私有構造器的討論) [/b]
此構造方法只能被其子類來調用,而不能被外界直接調用

[b]Blog URL:[/b][url]http://www.ivanpig.com/blog/?p=475[/url]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章