《快学Scala》第五章 学习笔记+练习题答案

1.改进Counter类,让它不要在大于Int.maxValue时变为负数

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

2.写一个BankAccount类,加入deposit和withdraw方法,和一个只读的balance属性

    class BankAccount(var balance: Double) {
      def deposit(num: Double): Unit = {
        balance = balance + num
      }

      def withdraw(num: Double): Unit = balance -= num
    }

3.编写一个Time类,增加仅仅读属性hours和minutes,和一个检查某一时刻是否早于还有一时刻的方法before(other:Time):Boolean。Time对象应该以new Time(hrs,min)方式构建。当中hrs以军用时间格式呈现(介于0和23之间)

    class Time(private val hours: Int = 0, private val minutes: Int = 0) {
      if (hours < 0 || hours > 23) throw new Exception("construct Time error")

      def before(other: Time): Boolean = {
        if (hours < other.hours) true
        else if (hours == other.hours && minutes < other.minutes) true
        else false
      }
    }

4.又一次实现前一个类中的Time类,将内部呈现改成午夜起的分钟数(介于0到24*60-1之间)。
不要改变公有接口。也就是说,client代码不应因你的改动而受影响

    class Time2(private val hours: Int = 0, private val minutes: Int = 0) {
      if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59) throw new Exception("construct Time error")
      private val all_minutes = hours * 60 + minutes

      def before(other: Time2): Boolean = {
        all_minutes < other.all_minutes
      }
    }

5.创建一个Student类。增加可读写的JavaBeans属性name(类型为String)和id(类型为Long)。
有哪些方法被生产?(用javap查看。)你能够在Scala中调用JavaBeans的getter和setter方法吗?应该这样做吗?

生成了name(),name_=(),id(),id_=(),setName(),getName(),setId(),getId() 编写代码例如以下

    import scala.beans.BeanProperty
    class Student(){
      @BeanProperty var name:String =_
      @BeanProperty var id:Long =_
    }

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

    class Person(var age:Int){
      if (age<0) age
    }

7 编写一个Person类,其主构造器接受一个字符串,该字符串包含名字,空格和姓,如new Person(“Fred Smith”)。提供只读属性firstName和lastName。主构造器参数应该是var,val还是普通参数?为什么?
必须为val。如果为var,则对应的此字符串有get和set方法,而Person中的firstName和lastName为只读的,所以不能重复赋值。如果为var则会重复赋值而报错

val

8 创建一个Car类,以只读属性对应制造商,型号名称,型号年份以及一个可读写的属性用于车牌。提供四组构造器。每一个构造器都要求制造商和型号为必填。型号年份和车牌可选,如果未填,则型号年份为-1,车牌为空串。你会选择哪一个作为你的主构造器?为什么?

原本打算使用两个必填参数作为主构造类,但是这样不好解决年份不可变的问题

    class Car(val manufacturer: String, val model: String) {
      var year = -1
      var plate_num = ""

      def this(manufacturer: String, model: String, year: Int) {
        this(manufacturer, model)
        this.year = year
      }

      def this(manufacturer: String, model: String, plate_num: String) {
        this(manufacturer,model)
        this.plate_num = plate_num
      }

      def this(manufacturer: String, model: String,year: Int, plate_num: String) {
        this(manufacturer,model)
        this.year = year
        this.plate_num = plate_num
      }
    }

所以可以选择吧年份加入到主构造类里,但是这样写 3个构造类就可以了。

class Car1(val manufacturer: String, val model: String, val year: Int = -1) {
  var plate_num = ""

  def this(manufacturer: String, model: String) {
    this(manufacturer, model,year=-1)
  }

  def this(manufacturer: String, model: String, plate_num: String) {
    this(manufacturer, model)
    this.plate_num = plate_num
  }

  def this(manufacturer: String, model: String, year: Int, plate_num: String) {
    this(manufacturer, model)
    this.plate_num = plate_num
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值