《快学scala》Chapter 5 类

Posted aryastark

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《快学scala》Chapter 5 类相关的知识,希望对你有一定的参考价值。

package c5

class Counter {
  private var value = 0
  def increment(){
    if(value < Int.MaxValue){
      value += 1
    }
  }
  def current() = value
}
class BankAccount{
  val balance = 0
  def deposit(amount:Double){}
  def withdraw(){}
}
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}
}
class Time1(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 * 60 + minutes + ""}
}
import java.beans.BeanProperty

import scala.beans.BeanProperty/*查找包位置*/
class Student{
  @BeanProperty var name:String = _
  @BeanProperty var id:Long = _
}
/*p06*/
class Person(var age:Int){age = if(age < 0)0 else age}
/*p07*/
class Person1(val name:String){
  val (firstname,lastname) = name.split(" ")
}
/*p08*/
class Car(val maker:String,val typename:String){
  private var typeyear:Int = -1
  private var Board:String = ""
  def this(maker:String,typename:String,typeyear:Int,Board:String){
    this()
    this.typeyear=typeyear
    this.Board=Board
  }
}
/*p10*/
class Employ(){
  val name:String = "John Q.Public"
  var salary:Double = 0.0
}

  

以上是关于《快学scala》Chapter 5 类的主要内容,如果未能解决你的问题,请参考以下文章

快学Scala--类

快学Scala 第十一课 (类继承)

快学Scala 第八课 (嵌套类)

《快学Scala》第八章——继承

快学Scala 第六课 (类构造函数)

快学Scala 第三章 #4答案