如果我获得你的权利,那么,用海峡机械师的标记就几乎是不可能的,正如你可能在某些类别中混杂在一起的,但可能含有密码,因此,你无需在你使用时重写抽象的方法。
trait Stackoverflow {
val foo = "FOO"
}
trait MetaStackoverflow {
val bar = "BAR"
}
class User(val name: String)
现改为:
scala> val u = new User("jjmontes") with Stackoverflow with MetaStackoverflow
//u: User with Stackoverflow with MetaStackoverflow = $anon$1@8825a5
scala> u.bar
//res0: java.lang.String = BAR
scala> u.foo
//res1: java.lang.String = FOO
Or like this:
scala> class UserA(name: String) extends User(name) with Stackoverflow
//defined class UserA
scala> val u = new UserA("jjmontes")
//u: UserA = UserA@1aecf45
scala> u.foo
//res8: java.lang.String = FOO
scala> u.bar
<console>:12: error: value bar is not a member of UserA
u.bar
Scala与Java有几乎完全的交错(只有少数几例病例除外,那里仍然有交错,但痛苦)。