快学Scala第五章习题答案

来源:互联网 发布:小米vr眼镜vb好不好 编辑:程序博客网 时间:2024/05/02 02:37

5.1 改进5.1节的Counter类,让它不要在Int.MaxValue时变成负数。

class Counter{    private var value = Int.MaxValue    def increment(){      if(value < Int.MaxValue)value +=1      else value    }    def current = value}

5.2编写一个BankAccount类,加入deposit和withdraw方法,和一个只读的balance属性。

class BankAccount(val balance:Int = 0){     //只读    def deposit(){}    def withdraw(){}  }

5.3便携一个Time类,加入只读属性hours和minutes,和一个检查某一时刻是否早于另一时刻的方法

  // before(other:Time):Boolean。 Time对象应该以new Times(hrs, min)方式构建,其中hrs小时以  //  军用时间格式呈现  class Time(val hours:Int,val minutes:Int ){    def before(other:Time):Boolean={      hours < other.hours || (hours == other.hours && minutes < other.minutes )    }    override def toString():String = {      hours + " : " + minutes    }  }

5.4 重新实现前一个练习中的Time类,将内部呈现改成自午夜起的分钟数(介于0-24*60-1)之间。不要改变公有接口。

也就是说,客户端代码不应受到你的修改而受到影响

  class Time2(val hours:Int,val minutes:Int){    def before(other:Time2):Boolean={      hours*60 + minutes < other.hours*60 + other.minutes    }    override def toString():String = {      (hours * 60 + minutes).toString    }  }

5.5 创建一个Student类,加入可读写的JavaBeans属性name(类型为String)和id(类型为Long).

有哪些方法被生成?(javap查看)你可以在Scala中调用JavaBeans版的getter和setter方法吗?应该这样做吗?

import scala.reflect.BeanProperty//???//还是import scala.beans.BeanPropertyclass Stdudent{  @BeanProperty var name:String = _  @BeanProperty var id:Long = _}

5.6 在5.2节的Person类中提供一个主构造器,将负年龄转换为0

  class Person3(var age:Int){    age = if(age < 0) 0 else age  }

5.7 编写一个Person类,其主构造器接受一个字符串,该字符串包含名字,空格和姓,如new Person(“Fred Smith”)

提供只读属性firstName 和 lastName。主构造器参数应该是var val 还是普通参数呢?为什么?

  class Person7(val name:String){    val firstName:String = name.split(" ")(0)    val lastName:String = name.split(" ")(1)  }

5.8 创建一个Car类,以只读属性对应制造商,型号名称,型号年份以及一个可读写的属性用于车牌。

// 提供四组构造器,每个构造器fc都要求制造商和型号为必填。型号年份和车牌可选。如果未填,则型号年份为-1,
车牌为空串。你会选择哪个作为你的主构造器?为什么?

  class Car(val maker:String,val typeName:String,val year:Int = -1,var varLice:String=""){  }

5.9 用Java,C#,C++重做前一个练习

5.10 考虑如下的类:

  class Employee(val name:String, var salary:Double){    def this(){      this("John Q. Public",0.0)    }  }

//重写该类,使用显式的字段定义,和一个缺省主构造器。你更倾向于使用哪种形式?为什么?

  class Employee2{    val name:String = "John Q. Public"    var salary:Double = 0.0  }
0 0