1. 程式人生 > >《快學Scala》第5章 類 練習

《快學Scala》第5章 類 練習

1. 改進5.1節的Counter類,讓它不要在Int.MaxValue時變成負數。

/**
 * Created by Ibuki Suika on 2014/5/26.
 */

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

object ScalaApp {
  def main(args: Array[String]) {
    val counter = new Counter
    for (_ <- 1 to Int.MaxValue) {
      counter.increment()
    }
    println(counter.current)
  }
}
2. 編寫一個BankAccount類,加入deposit和withdraw方法,和一個只讀的balance屬性。
/**
 * Created by Ibuki Suika on 2014/5/26.
 */

class BankAccount(val balance: Int) {
  def deposit {println("deposit")}
  def withdraw {println("withdraw")}
}

object ScalaApp {
  def main(args: Array[String]) {
    val account = new BankAccount(100)
    account.deposit
    account.withdraw
    println(account.balance)
  }
}
3. 編寫一個Time類,加入只讀屬性hours和minutes,和一個檢查某一時刻是否早於另一時刻的方法before(other: Time): boolean。Time物件應該以new Time(hrs, min)方式構建,其中hrs小時數以軍用時間格式呈現(介於0和23之間)。
/**
 * Created by Ibuki Suika on 2014/5/26.
 */

class Time(private var hrs: Int, private var min: Int) {
  if (hours < 0 || hours >= 24) hrs = 0
  if (min < 0 || min >= 60) min = 0
  def before(other: Time) = {
    hours < other.hours || (hours == other.hours && minutes < other.minutes)
  }
  def hours = hrs
  def minutes = min
}

object ScalaApp {
  def main(args: Array[String]) {
    val time1 = new Time(9, 20)
    val time2 = new Time(10, 12)
    println(time1.before(time2))
  }
}
4. 重新實現前一個練習中的Time類,將內部呈現改成自午夜起的分鐘數(介於0到24x60-1之間)。不要改變公有介面。也就是說,客戶端程式碼不應因你的修改而受影響。
/**
 * Created by Ibuki Suika on 2014/5/26.
 */

class Time(hrs: Int, min: Int) {
  private var total = 0
  if (hrs >= 0 && hrs < 24) {
    total += hrs * 24
  }
  if (min > 0 && min < 60) {
    total += min
  }
  def before(other: Time) = {
    total < other.total
  }
  def hours = total / 60
  def minutes = total % 60
}

object ScalaApp {
  def main(args: Array[String]) {
    val time1 = new Time(9, 20)
    val time2 = new Time(10, 12)
    println(time1.before(time2))
  }
}
5. 建立一個Student類,加入可讀寫的JavaBeans屬性name(型別為String)和id(型別為Long)。
/**
 * Created by Ibuki Suika on 2014/5/26.
 */

import scala.beans.BeanProperty

class Student(@BeanProperty var name: String, @BeanProperty var id: Long) {
}

object ScalaApp {
  def main(args: Array[String]) {
    val student = new Student("Tom", 1000)
    println(student.name)
    println(student.id)
  }
}
6. 在5.2節的Person類中提供一個主構造器,將負年齡轉換為0。
/**
 * Created by Ibuki Suika on 2014/5/26.
 */

class Person(private var privateAge: Int) {
  if (privateAge < 0) privateAge = 0
  def age = privateAge
  def age_=(value: Int) {
    if (value > 0) {
      privateAge = value
    }
  }
}

object ScalaApp {
  def main(args: Array[String]) {
    val person = new Person(-1)
    println(person.age)
    person.age = 10
    println(person.age)
  }
}
7. 編寫一個Person類,其主構造器接受一個字串,該字串包含名字、空格和姓,如new Person("Fred Smith")。提供只讀屬性firstName和LastName。
/**
 * Created by Ibuki Suika on 2014/5/26.
 */

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

object ScalaApp {
  def main(args: Array[String]) {
    val person = new Person("Fred Smith")
    println(person.firstName)
    println(person.lastName)
  }
}
8. 建立一個Car類,以只讀屬性對應制造商、型號名稱、型號年份以及一個可讀寫的屬性用於車牌。提供四組構造器。每一個構造器都要求製造商和型號名稱為必填。型號年份以及車牌為可選,如果未填,則型號年份設定為-1,車牌設定為空字串。你會選擇哪一個作為你的主構造器?為什麼?
/**
 * Created by Ibuki Suika on 2014/5/26.
 */

class Car(val maker: String, val name: String, val year: Int, var card: String) {
  def this(maker: String, name: String, year: Int) {
    this(maker, name, year, "")
  }
  def this(maker: String, name: String, card: String) {
    this(maker, name, -1, card)
  }
  def this(maker: String, name: String) {
    this(maker, name, -1, "")
  }
}

object ScalaApp {
  def main(args: Array[String]) {
    val car = new Car("IBM", "Nexu", 1999)
    println(car.maker)
    println(car.name)
    println(car.year)
    println(car.card)
  }
}
10.考慮如下類:

class Employee(val name: String, var salary: Double) {

  def this() {this("John Q. Public", 0.0)}

}

重寫該類,使用顯式的欄位定義,和一個預設主構造器。你更傾向於使用哪一種形式?為什麼?

/**
 * Created by Ibuki Suika on 2014/5/26.
 */

class Employee {
  private var emName = ""
  private var emSalary = 0.0
  def this(n: String, s: Double) {
    this()
    this.emName = n
    this.emSalary = s
  }
  def name = emName
  def salary = emSalary
}

object ScalaApp {
  def main(args: Array[String]) {
    val employee = new Employee("Jim", 1800.9)
    println(employee.name)
    println(employee.salary)
  }
}