Skip to content

Instantly share code, notes, and snippets.

@takungsk
Created June 22, 2012 09:05
Show Gist options
  • Save takungsk/2971520 to your computer and use it in GitHub Desktop.
Save takungsk/2971520 to your computer and use it in GitHub Desktop.
traitの確認
trait 使って ソース分けてみた。
汎用的な def の処理を trait にしておいて
必要時に with で取り込んで実行する
object hoge1 {
def main(arg: Array[String]) {
println("へロー")
}
}
trait trait1 {
def f() = println("共通1")
}
この trait1 の f() を hoge1 で使いたい。
trait1 は別のscala ファイルにしておきたい。
// 実行結果
[info] Running hoge1
へロー
共通1
// trait のクラスにある 関数をよびだす。
object hoge1 {
def main(args: Array[String]) {
new hoge2(args)
}
}
class hoge2 extends App with trait1 {
def this(args: Array[String]) {
this()
println("へロー")
f() // trait に書いてある処理
}
}
// もとの 一つのファイルのときのソース
object hoge1 {
def main(arg: Array[String]) {
println("へロー")
}
}
// trait
// 関数を定義
trait trait1 {
def f() = println("共通1")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment