Created
June 22, 2012 09:05
-
-
Save takungsk/2971520 to your computer and use it in GitHub Desktop.
traitの確認
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 に書いてある処理 | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// もとの 一つのファイルのときのソース | |
object hoge1 { | |
def main(arg: Array[String]) { | |
println("へロー") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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