Created
June 17, 2013 02:00
-
-
Save f81/5794243 to your computer and use it in GitHub Desktop.
But I remember オブジェクト指向 ref: http://qiita.com/items/5cbba25f1d4d7c8953e5
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
class FullnameInJapan() extends Fullname { | |
override def get(firstname: String, lastname: String) = lastname + " " + firstname | |
} |
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
val user1 = new User(1, "源次", "富樫", new FullnameInJapan) |
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
val user = new User(1, "源次", "富樫") | |
user.firstname |
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 User1.scala | |
User1.scala:6: error: value id in class User cannot be accessed in this.User | |
println(user.id) | |
^ | |
one error found |
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 User2.scala | |
富樫 源次 | |
鯛雄 松尾 |
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
class User(val id: Long, val firstname: String, val lastname: String, val fullname:Fullname = new Fullname) { | |
def getFullname() = this.fullname.get(firstname, lastname) | |
} |
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
val user2 = new User(2, "鯛雄", "松尾") |
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
class Fullname() { | |
def get(firstname: String, lastname: String) = firstname + " " + lastname | |
} |
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
public class User { | |
private final long id; | |
private final String firstname; | |
private final String lastname; | |
public User(long id, String firstname, String lastname) { | |
this.id = id; | |
this.firstname = firstname; | |
this.lastname = lastname; | |
} | |
public long getId() { | |
return id; | |
} | |
public String getFirstname() { | |
return firstname; | |
} | |
public String getLastname() { | |
return lastname; | |
} | |
public String getFullname() { | |
return this.firstname + " " + this.lastname; | |
} | |
} |
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
class User(val id: Long, val firstname: String, val lastname: String) { | |
def getFullname() = this.firstname + " " + this.lastname | |
} |
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
class User(private val id: Long, private val firstname: String, private val lastname: String) { | |
def getFullname() = this.firstname + " " + this.lastname | |
} | |
val user = new User(1, "源次", "富樫") | |
println(user.id) |
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
class User(val id: Long, val firstname: String, val lastname: String, val fullname:Fullname = new Fullname) { | |
def getFullname() = this.fullname.get(firstname, lastname) | |
} | |
class Fullname() { | |
def get(firstname: String, lastname: String) = firstname + " " + lastname | |
} | |
class FullnameInJapan() extends Fullname { | |
override def get(firstname: String, lastname: String) = lastname + " " + firstname | |
} | |
val user1 = new User(1, "源次", "富樫", new FullnameInJapan) | |
println(user1.getFullname()) | |
val user2 = new User(2, "鯛雄", "松尾") | |
println(user2.getFullname()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment