by shigemk2

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

Scalaの継承

コード

scala> abstract class Engineer {                                                
| def work():Unit
| }
defined class Engineer

scala> class Programmer(name:String,age:Int) extends Engineer {
     | def work() = printf("%s(%d)さんはコーディングします",name,age)
     | }
defined class Programmer

scala> val p = new Programmer("taro", 25)
p: Programmer = Programmer@c32fc9

scala> p.work
taro(25)さんはコーディングします
scala> abstract class Engineer {
| def work():Unit
| def study() = printf("勉強します")
| }
defined class Engineer
scala> class Programmer(name:String,age:Int) extends Engineer {
     | def work() = printf("%s(%d)さんはコーディングします",name,age)
     | override def study() = println("プログラミングの勉強をします")
     | }
defined class Programmer

scala> abstract class Engineer(val name:String) {
     | println("Engineer.name = " + name)
     |
     | def work():Unit
     | }
defined class Engineer

scala> class Programmer(name:String,age:Int) extends Engineer(name) {
     | println("Programmer.name=" + name)
     | println("Programmer.age=" + age)
     |
     | def work() = printf("%s(%d)さんはコーディングします",name,age)
     | }
defined class Programmer
scala> class Programmer(name:String,age:Int) extends Engineer(name) {
     | println("Programmer.name=" + name)
     | println("Programmer.age=" + age)
     |
     | def work() = printf("%s(%d)さんはコーディングします",name,age)
     | }
defined class Programmer

scala> val p = new Programmer("taro", 30)
Engineer.name = taro
Programmer.name=taro
Programmer.age=30
p: Programmer = Programmer@77b29a