Created
September 17, 2013 07:39
-
-
Save Arakaki/6591170 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
| package com.mycode | |
| object App extends LoggerSupport with LoginSupport with DbSupport{ | |
| def main(args : Array[String]) { | |
| //開始ログ | |
| logger("START main") | |
| //ログイン | |
| login("arakaki") | |
| //SQLを実行 | |
| val result = query("SELECT * FROM users") | |
| //ログアウト | |
| logout("arakaki") | |
| //終了ログ | |
| logger("END main") | |
| } | |
| } | |
| /** | |
| * ログ出力機能 | |
| */ | |
| trait LoggerSupport { | |
| def logger(message:String){ | |
| println(message) | |
| } | |
| } | |
| /** | |
| * DB接続周りの実装 | |
| */ | |
| trait DbSupport { | |
| def query(sql:String) :String ={ | |
| //sqlを実行 | |
| "テスト" | |
| } | |
| } | |
| /** | |
| * ログイン機能 | |
| */ | |
| trait LoginSupport extends LoggerSupport{ | |
| def login(loginId:String) ={ | |
| logger(s"ログインしましたID:[${loginId}}]") | |
| true | |
| } | |
| def logout(loginId:String) ={ | |
| logger(s"ログアウトしましたID:[${loginId}}]") | |
| true | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment