Created
November 9, 2011 12:52
-
-
Save seratch/1351338 to your computer and use it in GitHub Desktop.
example1.scala #daimonscala 21
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 Person { | |
| val DEFAULT_NAME = "anonymous" | |
| } | |
| class Person(var name: String) { | |
| def this() { | |
| this(Person.DEFAULT_NAME) | |
| } | |
| def canWriteCode = false | |
| } | |
| class Programmer1(name: String) extends Person(name) { | |
| override def canWriteCode = true | |
| } | |
| trait ProgrammingSkill { _: Person => | |
| override def canWriteCode = true | |
| } | |
| class Programmer2(name: String) extends Person(name) with ProgrammingSkill | |
| val person = new Person | |
| println(person.name) // anonymous | |
| person.name = "foo" | |
| println(person.name) // foo | |
| println(person.canWriteCode) // false | |
| val brian = new Person("Brian") with ProgrammingSkill | |
| println(brian.name) // Brian | |
| println(brian.canWriteCode) // true | |
| val andy = new Programmer1("Andy") | |
| println(andy.name) // Andy | |
| println(andy.canWriteCode) // true | |
| val kaz = new Programmer2("Kaz") | |
| println(kaz.name) // Kaz | |
| println(kaz.canWriteCode) // true | |
| // compatible with JavaBean | |
| import reflect.BeanProperty | |
| class Person(@BeanProperty var name: String) { | |
| def this() { | |
| this(Person.DEFAULT_NAME) | |
| } | |
| def canWriteCode = false | |
| } | |
| val person = new Person | |
| println(person.name) // anonymous | |
| println(person.getName()) // anonymous | |
| person.setName("renamed") | |
| println(person.getName()) // renamed | |
| println(person.canWriteCode) // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment