Created
September 16, 2013 16:14
-
-
Save f81/6582838 to your computer and use it in GitHub Desktop.
第9章:Scalaのトレイト(trait) ref: http://qiita.com/f81@github/items/5b96af593812286eec49
This file contains 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
scala> trait Job | |
defined trait Job |
This file contains 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
scala> class Job(name: String) | |
defined class Job |
This file contains 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
scala> trait Job(name: String) | |
<console>:1: error: traits or objects may not have parameters | |
trait Job(name: String) | |
^ |
This file contains 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
scala> trait Job { val name: String } | |
defined trait Job | |
scala> new Job { val name = "student" } | |
res0: Job = $anon$1@518677ee | |
scala> res0.name | |
res1: String = student |
This file contains 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
scala> trait Job { val name: String } | |
defined trait Job | |
scala> new { val name = "student" } with Job | |
res2: Job = $anon$1@701ce28a | |
scala> res2.name | |
res3: String = student |
This file contains 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 Job { | |
def getName() { | |
"student" | |
} | |
} |
This file contains 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 Trait1 { | |
def main(args: Array[String]){ | |
val man = new Man | |
man.say() | |
} | |
} | |
trait Human { | |
val name = "飛行帽" | |
def sayName() { | |
println("My name is " + name) | |
} | |
} | |
class Man extends Human |
This file contains 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 Trait2 { | |
def main(args: Array[String]){ | |
val man = new Man("虎丸龍次", "男塾", "一号生") | |
man.say() | |
} | |
} | |
trait Human { | |
val name: String | |
} | |
trait Job { | |
val job: String | |
} | |
trait Grade { | |
val grade: String | |
} | |
class Man(n: String, j: String, g: String) extends Human with Job with Grade { | |
val name = n | |
val job = j | |
val grade = g | |
def say() { | |
println("名前は" + this.name + "じゃ。" + job + grade + "じゃ。") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment