Groovyって継承もできるんだよな?と思って書いてみたサンプル

Groovyでの継承の例。
ウィザードリーのキャラクタをイメージして書いてみた。
ジョブチェンジすると5歳ふえるぞ!
コンストラクタの書き方が良くわからない。


// 全種族に共通のプロパティ
public class Specie {
int age // ねんれい
int power // ちから
int speed // すばやさ
int vital // たいりょく
int piety // しんこうしん
int wisdom // ちえ
String sex // せいべつ
String job // ジョブ

toString() {
strRet = "job is " + this.job + "\n" +
"sex is " + this.sex + "\n" +
"age is " + this.age.toString() + "\n" +
"power is " + this.power.toString() + "\n" +
"vital is " + this.vital.toString() + "\n" +
"piety is " + this.piety.toString() + "\n" +
"wisdom is " + this.wisdom.toString() + "\n"
return strRet
}
}
// 人間という種族を定義
public class Human extends Specie {
// 初期値:コンストラクタにしたい!
setHuman(strSex,strJob){
this.age = 10
this.power = 8
this.speed = 8
this.vital = 7
this.piety = 8
this.wisdom = 9
this.sex = strSex
this.job = strJob
}

jobChange(strJob){
this.job = strJob
this.age+= 5
}
}
// エルフという種族を定義
public class Elf extends Specie {
setElf(strSex,strJob){
this.age = 9
this.power = 6
this.speed = 6
this.vital = 8
this.piety = 11
this.wisdom = 12
this.sex = strSex
this.job = strJob
}

jobChange(strJob){
this.job = strJob
this.age+= 5
}
}

taro = new Human()
taro.setHuman('male','Warrior')
println "taro is this\n" + taro.toString()
// たろうはジョブチェンジをした。
taro.jobChange('Lord')
println "taro is this\n" + taro.toString()

keiko = new Elf()
keiko.setElf('female','Mage')
println "keiko is this\n" + keiko.toString()

そして実行結果はこんな感じになる

taro is this
job is Warrior
sex is male
age is 10
power is 8
vital is 7
piety is 8
wisdom is 9

taro is this
job is Lord
sex is male
age is 15
power is 8
vital is 7
piety is 8
wisdom is 9

keiko is this
job is Mage
sex is female
age is 9
power is 6
vital is 8
piety is 11
wisdom is 12