楼主: zli08
15042 55

Beginning Scala 2nd Edition [推广有奖]

21
chitchatla(真实交易用户) 发表于 2015-5-6 19:32:50
  1. Listing 3-21. Implementing case features in a class on its own
  2. classStuff(val name: String,valage: Int) {
  3. overridedeftoString = "Stuff("+name+","+age+")"
  4. overridedefhashCode= name.hashCode+ age
  5. overridedefequals(other: AnyRef)= othermatch {
  6. case s: Stuff=> this.name== s.name &&this.age == s.age
  7. case _ => false
  8. }
  9. }
  10. objectStuff {
  11. defapply(name: String, age: Int) = newStuff(name,age)
  12. defunapply(s: Stuff)= Some((s.name, s.age))
  13. }
复制代码

22
setecastro(真实交易用户) 发表于 2015-5-6 20:37:46
  1. Listing 3-22. Using def in the value class
  2. class SomeClass(val i: Int) extends AnyVal {
  3. def twice() = i*2
  4. }
  5. Here SomeClass is a user-defined value class that wraps the Int parameter and encapsulates a twice
  6. method. To invoke the twice method, create the instance of the SomeClass class as follows:
  7. scala> val v = new SomeClass(9)
  8. v: SomeClass = SomeClass@9
  9. scala> v.twice()
  10. res5: Int = 18
复制代码

23
江南士族(真实交易用户) 发表于 2015-5-6 20:56:26
看看吧

24
baijunde(真实交易用户) 发表于 2015-5-6 23:46:24
谢谢楼主分享

25
wuzubin123(未真实交易用户) 发表于 2015-5-7 21:47:51
谢谢分享

26
Nicolle(真实交易用户) 学生认证  发表于 2015-6-18 11:07:07
提示: 作者被禁止或删除 内容自动屏蔽

27
Nicolle(真实交易用户) 学生认证  发表于 2015-6-18 11:09:58
提示: 作者被禁止或删除 内容自动屏蔽

28
Nicolle(真实交易用户) 学生认证  发表于 2015-6-18 11:13:00
提示: 作者被禁止或删除 内容自动屏蔽

29
Nicolle(真实交易用户) 学生认证  发表于 2015-6-18 11:16:17
提示: 作者被禁止或删除 内容自动屏蔽

30
Nicolle(真实交易用户) 学生认证  发表于 2015-6-18 11:17:56
提示: 作者被禁止或删除 内容自动屏蔽

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2025-12-30 21:18