快学scala 第八章 继承 读书笔记及习题答案代码

chapter 8 继承

标签:快学scala


一、笔记

  1. scala中重写一个非抽象方法必须使用override修饰符。scala中调用超类方法与java完全一样:
 
 
  1. public class Employee extends Person{
  2. override def toString = super.toString + "[salary=" + salary + "]" //super.toString会调用超类Person的toString
  3. }
  1. 测试某个对象是否属于某个给定的类,可以用isInstanceOf方法,如果测试成功,可以用asInstanceOf方法将引用转换为子类的引用。
 
 
  1. if(p.isInstanceOf[Employee]){
  2. val s = p.asInstanceOf[Employee] //s的类型为Employee
  3. }
  4. 如果p指向的设计Employee类及其子类的对象,则p.isInstanceOf[Employee]将会成功;如果p不是Employee,会抛出异常。

如果要测试p指向的是一个Employee对象但又不是其子类的话,用

 
 
  1. if(p.getClass == classof[Employee])
  2. classOf方法定义在scala.Predef对象中,会被自动引入。
  1. scala中,不像java,不需要对抽象方法使用abstract关键字,只需要省去方法体。但和java一样,如果某个类至少存在一个抽象方法,则该类必须声明abstract。 
    在子类中重写超类的抽象方法时,不需要使用override关键字。
 
 
  1. abstract class Persion(val name: String){
  2. def id: Int //没有方法体,这是一个抽象方法
  3. }
  1. scala的类继承层级中,与java中基本类型相对应的类,以及Unit类型都扩展自AnyVal。所有其他类都是AnyRef的子类,AnyRef是java虚拟机中Object类的同义词。AnyVal和AnyRef都扩展自Any类,Any是整个继承层级的根节点。 
    AnyVal没有追加任何方法,他是所有值类型的一个标记。Nothing类型没有实例,比如空列表Nil的类型是List[Nothing],它是List[List]的子类型,T可以是任何类型。Unit并不是其他类型的超类型,但是编译器允许任何值被替换成()。
  2. 对象相等性。AnyRef的eq方法检查两个引用是否指向同一个对象。AnyRef的equals方法调用eq。建议实现类的时候,重写equals方法,以提供一个自然的、相称的相等性判断。
 
 
  1. final override def equals(other: Any)= { //确保定义的equals方法类型参数为Any
  2. val that = other.asInstanceOf[Item]
  3. if (that == null) false
  4. else description == that.description && price == that.price
  5. }

二、习题答案

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} 
}

 
 
  1. class BankAccount(initialBalance:Double){
  2. private var balance = initialBalance
  3. override def toString(): String= "initialBalance amount: " + balance
  4. def deposit(amount:Double) = { balance += amount; balance}
  5. def withdraw(amount:Double) = {balance -= amount; balance}
  6. }
  7. class checkingAccount(initialBalance: Double) extends BankAccount(initialBalance){
  8. override def deposit(amount: Double): Double = {super.deposit(amount -1) }
  9. override def withdraw(amount: Double) :Double = {super.withdraw(amount -1) }
  10. }
  11. val account: BankAccount = new checkingAccount(100)
  12. println(account)
  13. println(account.deposit(10))

8.2 扩展前一个练习的BankAccount类,新类SavingsAccount每个月都有利息产生(earnMonthlyInterest方法被调用),并且有每月三次免手续费的存款或取款。在earnMonthlyInterest方法中重置交易计数。

 
 
  1. class BankAccount(initialBalance:Double){
  2. private var balance = initialBalance
  3. override def toString(): String= "initialBalance amount: " + balance
  4. def deposit(amount:Double) = { balance += amount; balance}
  5. def withdraw(amount:Double) = {balance -= amount; balance}
  6. }
  7. class SavingsAccount(initialBalance: Double) extends BankAccount(initialBalance){
  8. private var num: Int = _
  9. def earnMonthlyInterest()={
  10. num = 3
  11. super.deposit(1)
  12. }
  13. override def deposit(amount: Double):Double= {
  14. num -= 1
  15. if(num < 0) super.deposit(amount -1) else super.deposit(amount)
  16. }
  17. override def withdraw(amount: Double):Double= {
  18. num -= 1
  19. if(num < 0) super.withdraw(amount -1) else super.withdraw(amount)
  20. }
  21. }

8.3 翻开你喜欢的Java或C++教科书,一定会找到用来讲解继承层级的实例,可能是员工,宠物,图形或类似的东西。用Scala来实现这个示例。

 
 
  1. /*Java */
  2. class Art{
  3. Art(){System.out.println("Art constructor");}
  4. }
  5. class Drawing extends Art{
  6. Drawing() {System.out.println("Drawing constructor");}
  7. }
  8. public class Cartoon extends Drawing{
  9. public Cartoon() { System.out.println("Cartoon constructor");}
  10. public static void main(String args[]){
  11. Cartoon book = new Cartoon();
  12. }
  13. }
 
 
  1. /*scala*/
  2. class Art{
  3. println("Art constructor")
  4. }
  5. class Drawing extends Art{
  6. println("Drawing constructor")
  7. }
  8. class Cartoon extends Drawing{
  9. println("Cartoon constructor")
  10. }
  11. val book: Cartoon = new Cartoon

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

 
 
  1. import collection.mutable.ArrayBuffer
  2. abstract class Item{
  3. def price():Double
  4. def description(): String
  5. override def toString(): String={
  6. "description: " + description + " price: " + price()
  7. }
  8. }
  9. class SimpleItem(price: Double, description: String) extends Item{
  10. }
  11. class Bundle extends Item{
  12. val items = new ArrayBuffer[Item]()
  13. def addItem(item: Item){
  14. items += item
  15. }
  16. def price(): Double= {
  17. var total = 0
  18. items.foreach(total += _.price())
  19. total
  20. }
  21. def description(): String= {
  22. items.mkString(" ")
  23. }
  24. }

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

 
 
  1. class Point(x: Double, y: Double){
  2. }
  3. class LabelPoint(label: String, x: Double, y: Double)extends Point(x,y){
  4. println("Label: " + label + "x: " + x + "y: " + y )
  5. }
  6. val item = new LabelPoint("Black Thursday",1929,230.07)

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

 
 
  1. abstract class Shape{
  2. def centerPoint()={}
  3. }
  4. class Rectangle(x1: Int, y1: Int, x2: Int, y2: Int) extends Shape{
  5. override def centerPoint()= {
  6. }
  7. }
  8. class Circle extends Shape{
  9. }

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

 
 
  1. import java.awt.{Point, Rectangle}
  2. class Square(point:Point,width:Int) extends Rectangle(point.x,point.y,width,width){
  3. def this(){
  4. this(new Point(0,0),0)
  5. }
  6. def this(width:Int){
  7. this(new Point(0,0),width)
  8. }
  9. }

8.8 编译8.6节中的Person和SecretAgent类并使用javap分析类文件。总共有多少name的getter方法?它们分别取什么值?(提示:可以使用-c和-private选项)

总共两个。Person中取得的是传入的name,而SecretAgent中取得的是默认的"secret"

8.9 在8.10节的Creature类中,将val range替换成一个def。如果你在Ant子类中也用def的话会有什么效果?如果在子类中使用val又会有什么效果?为什么?

在Ant中使用def没有问题。但是如果使用val则无法编译,因为val只能重写不带参数的def。这里的def是带参数的.

8.10 文件scala/collection/immutable/Stack.scala包含如下定义: 
class Stack[A] protected (protected val elems: List[A]) 
请解释protected关键字的含义。(提示:回顾我们在第5章中关于私有构造器的讨论)

此构造方法只能被其子类来调用,而不能被外界直接调用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值