chapter 8 继承
标签:快学scala
一、笔记
- scala中重写一个非抽象方法必须使用override修饰符。scala中调用超类方法与java完全一样:
public class Employee extends Person{
override def toString = super.toString + "[salary=" + salary + "]" //super.toString会调用超类Person的toString
}
- 测试某个对象是否属于某个给定的类,可以用isInstanceOf方法,如果测试成功,可以用asInstanceOf方法将引用转换为子类的引用。
if(p.isInstanceOf[Employee]){
val s = p.asInstanceOf[Employee] //s的类型为Employee
}
如果p指向的设计Employee类及其子类的对象,则p.isInstanceOf[Employee]将会成功;如果p不是Employee,会抛出异常。
如果要测试p指向的是一个Employee对象但又不是其子类的话,用
if(p.getClass == classof[Employee])
classOf方法定义在scala.Predef对象中,会被自动引入。
- scala中,不像java,不需要对抽象方法使用abstract关键字,只需要省去方法体。但和java一样,如果某个类至少存在一个抽象方法,则该类必须声明abstract。
在子类中重写超类的抽象方法时,不需要使用override关键字。
abstract class Persion(val name: String){
def id: Int //没有方法体,这是一个抽象方法
}
- scala的类继承层级中,与java中基本类型相对应的类,以及Unit类型都扩展自AnyVal。所有其他类都是AnyRef的子类,AnyRef是java虚拟机中Object类的同义词。AnyVal和AnyRef都扩展自Any类,Any是整个继承层级的根节点。
AnyVal没有追加任何方法,他是所有值类型的一个标记。Nothing类型没有实例,比如空列表Nil的类型是List[Nothing],它是List[List]的子类型,T可以是任何类型。Unit并不是其他类型的超类型,但是编译器允许任何值被替换成()。 - 对象相等性。AnyRef的eq方法检查两个引用是否指向同一个对象。AnyRef的equals方法调用eq。建议实现类的时候,重写equals方法,以提供一个自然的、相称的相等性判断。
final override def equals(other: Any)= { //确保定义的equals方法类型参数为Any
val that = other.asInstanceOf[Item]
if (that == null) false
else description == that.description && price == that.price
}
二、习题答案
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 BankAccount(initialBalance:Double){
private var balance = initialBalance
override def toString(): String= "initialBalance amount: " + balance
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): Double = {super.deposit(amount -1) }
override def withdraw(amount: Double) :Double = {super.withdraw(amount -1) }
}
val account: BankAccount = new checkingAccount(100)
println(account)
println(account.deposit(10))
8.2 扩展前一个练习的BankAccount类,新类SavingsAccount每个月都有利息产生(earnMonthlyInterest方法被调用),并且有每月三次免手续费的存款或取款。在earnMonthlyInterest方法中重置交易计数。
class BankAccount(initialBalance:Double){
private var balance = initialBalance
override def toString(): String= "initialBalance amount: " + balance
def deposit(amount:Double) = { balance += amount; balance}
def withdraw(amount:Double) = {balance -= amount; balance}
}
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)
}
}
8.3 翻开你喜欢的Java或C++教科书,一定会找到用来讲解继承层级的实例,可能是员工,宠物,图形或类似的东西。用Scala来实现这个示例。
/*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");}
public static void main(String args[]){
Cartoon book = new Cartoon();
}
}
/*scala*/
class Art{
println("Art constructor")
}
class Drawing extends Art{
println("Drawing constructor")
}
class Cartoon extends Drawing{
println("Cartoon constructor")
}
val book: Cartoon = new Cartoon
8.4 定义一个抽象类Item,加入方法price和description。SimpleItem是一个在构造器中给出价格和描述的物件。利用val可以重写def这个事实。Bundle是一个可以包含其他物件的物件。其价格是打包中所有物件的价格之和。同时提供一个将物件添加到打包当中的机制,以及一个适合的description方法
import collection.mutable.ArrayBuffer
abstract class Item{
def price():Double
def description(): String
override def toString(): String={
"description: " + description + " price: " + price()
}
}
class SimpleItem(price: Double, 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= {
items.mkString(" ")
}
}
8.5 设计一个Point类,其x和y坐标可以通过构造器提供。提供一个子类LabeledPoint,其构造器接受一个标签值和x,y坐标,比如:new LabeledPoint("Black Thursday",1929,230.07)
class Point(x: Double, y: Double){
}
class LabelPoint(label: String, x: Double, y: Double)extends Point(x,y){
println("Label: " + label + "x: " + x + "y: " + y )
}
val item = new LabelPoint("Black Thursday",1929,230.07)
8.6 定义一个抽象类Shape,一个抽象方法centerPoint,以及该抽象类的子类Rectangle和Circle。为子类提供合适的构造器,并重写centerPoint方法
abstract class Shape{
def centerPoint()={}
}
class Rectangle(x1: Int, y1: Int, x2: Int, y2: Int) extends Shape{
override def centerPoint()= {
}
}
class Circle extends Shape{
}
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(width:Int){
this(new Point(0,0),width)
}
}
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章中关于私有构造器的讨论)
此构造方法只能被其子类来调用,而不能被外界直接调用