読者です 読者をやめる 読者になる 読者になる

by shigemk2

当面は技術的なことしか書かない

トレイトの順番

Scala
abstract class Engineer {
  println ("class Engineer constructor")

  def work(time:Int)
}

class Person extends Engineer {
  println ("class Person constructor")

  def work(time:Int) {
    println ("Person#work start")
    println ("1つのタスクを"+time+"分で行います")
    println ("Person#work end")
  }
}

trait Programmer extends Engineer {
  println("trait Programmer constructor")

  abstract override def work(time:Int) = {
    println("Programmer#work start")
    super.work(time - 15)
    println("Programmer#work end")
  }
}

trait Agiler extends Engineer {
  println("trait Agiler constructor")

  abstract override def work(time:Int) = {
    println("Programmer#work start")
    super.work(time / 2)
    println("Programmer#work end")
  }
}

object Engineer {
  def main(args: Array[String]) {
    var p4 = new Person with Programmer with Agiler
    println(p4.work(60)) // (60/2)-15
    var p3 = new Person with Agiler with Programmer
    println(p3.work(60)) // (60-15)/2
  }
}