一、类
1.1简单类和无参方法
- 类的定义通过class关键字实现
scala> class Dog { | private var leg = 4 | def shout(content: String) { | println(content) | } | def currentLeg = leg | } defined class Dog scala> val dog = new Dog dog: Dog = Dog@1d66664f scala> dog shout "汪汪汪" 汪汪汪 scala> println(dog currentLeg) <console>:14: warning: postfix operator currentLeg should be enabled by making the implicit value scala.language.postfixOps visible. This can be achieved by adding the import clause 'import scala.language.postfixOps' or by setting the compiler option -language:postfixOps. See the Scaladoc for value scala.language.postfixOps for a discussion why the feature should be explicitly enabled. println(dog currentLeg) ^ 4
注:调用无参方法时,可以选择加或者不加(),如果方法定义没有加(),那么调用时就不能带括号。
1.2 getter和setter方法
Scala类中的每个属性,编译后,会有一个私有的字段和相应的getting、setting方法生成。
- 自己定义getter和setter方法
class Dog2 { private var _leg = 4 def leg = _leg def leg_=(newLeg: Int) { _leg = newLeg } } scala> val dog2 = new Dog2 dog2: Dog2 = Dog2@1b406bc2 scala> dog2.leg_=(10)//setter scala> println(dog2.leg) 10 scala> dog2.leg_=(15) scala> println(dog2.leg) 15
注:自己手动创建变量的getter和setter方法需要遵循以下原则:
1) 字段属性名以“_”作为前缀,如:_leg
2) getter方法定义为:def leg = _leg
3) setter方法定义时,方法名为属性名去掉前缀,并加上后缀,后缀是:“leg_=”,如例子所示
1.3Bean属性
JavaBeans规范定义了Java的属性是像getXXX()和setXXX()的方法。许多Java工具都依赖这个命名习惯。为了Java的互操作性。
- 将Scala字段加@BeanProperty时,这样的方法会自动生成。
import scala.beans.BeanProperty class Person { @BeanProperty var name: String = _ //添加@BeanProperty } scala> val person = new Person person: Person = Person@6431e18 scala> person.setName("Nick") scala> person.getName res15: String = Nick scala> println(person.name) Nick
注:Person将会生成四个方法:
1)name:String
2)name_=(newValue:String): Unit
3)getName():String
4)setName(newValue:String):Unit
1.4嵌套类
在class里面,再定义一个class
- 模拟局域网内聊天的场景
import scala.collection.mutable.ArrayBuffer class Network { class Member(val name: String) { //嵌套类 val contacts = new ArrayBuffer[Member] } private val members = new ArrayBuffer[Member] def join(name: String) = { val m = new Member(name) members += m m } }
- 使用该嵌套类
scala> val chatter1 = new Network chatter1: Network = Network@4c372496 scala> val chatter2 = new Network chatter2: Network = Network@770f78cc scala> val fred = chatter1.join("Fred") fred: chatter1.Member = Network$Member@2922bf92 scala> val wilma = chatter1.join("Wilma") wilma: chatter1.Member = Network$Member@79dcfb0d scala> val barney = chatter2.join("Barney") barney: chatter2.Member = Network$Member@4b6be6fc scala> fred.contacts += wilma res17: fred.contacts.type = ArrayBuffer(Network$Member@79dcfb0d) scala> fred.contacts += barney//不再同一局域网,无法加好友 <console>:17: error: type mismatch; found : chatter2.Member required: chatter1.Member fred.contacts += barney ^
1.5 伴生对象
上个例子中,不同网络间无法添加联系人,要使的他们能够,则
- 将member作为network的伴生对象
scala> object Network2 { | class Member(val name: String) { | val contacts = new ArrayBuffer[Member] | def description = name + "的联系人:" + | (for (c <- contacts) yield c.name).mkString(" ") | } | } defined object Network2 scala> import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer scala> class Network2 { | private val members = new ArrayBuffer[Network2.Member] | def join(name: String) = { | val m = new Network2.Member(name) | members += m | m | } | def description = "该局域网中的联系人:" + | (for (m <- members) yield m.description).mkString(", ") | } defined class Network2 warning: previously defined object Network2 is not a companion to class Network2. Companions must be defined together; you may wish to use :paste mode for this. scala> val chatter3 = new Network2 chatter3: Network2 = Network2@4a8b2ea0 scala> val chatter4 = new Network2 chatter4: Network2 = Network2@1a79ce5 scala> val fred2 = chatter3.join("Fred") fred2: Network2.Member = Network2$Member@4aef70c4 scala> val wilma2 = chatter3.join("Wilma") wilma2: Network2.Member = Network2$Member@10d598ca scala> val barney2 = chatter4.join("Barney") barney2: Network2.Member = Network2$Member@29238c0d scala> fred2.contacts += wilma2 res21: fred2.contacts.type = ArrayBuffer(Network2$Member@10d598ca) scala> fred2.contacts += barney2 res22: fred2.contacts.type = ArrayBuffer(Network2$Member@10d598ca, Network2$Member@29238c0d) scala> println(chatter3.description) 该局域网中的联系人:Fred的联系人:Wilma Barney, Wilma的联系人: scala> println(chatter4.description) 该局域网中的联系人:Barney的联系人: scala> println(fred2.description) Fred的联系人:Wilma Barney scala> println(wilma2.description) Wilma的联系人: scala> println(barney2.description) Barney的联系人:
1.6类型投影
- 在不同局域网添加联系人,还有一种方法是类型投影,注意关键符号”#“。
import scala.collection.mutable.ArrayBuffer //投影 class Network3 { class Member(val name: String) { val contacts = new ArrayBuffer[Network3#Member] } private val members = new ArrayBuffer[Member] def join(name: String) = { val m = new Member(name) members += m m } } scala> val chatter5 = new Network3 chatter5: Network3 = Network3@26627b56 scala> val chatter6 = new Network3 chatter6: Network3 = Network3@35d80a2 scala> val fred3 = chatter5.join("Fred") //Fred 和 Wilma加入局域网1 fred3: chatter5.Member = Network3$Member@3797459f scala> val wilma3 = chatter5.join("Wilma") wilma3: chatter5.Member = Network3$Member@1a30c741 scala> val barney3 = chatter6.join("Barney") //Barney加入局域网2 barney3: chatter6.Member = Network3$Member@54b4191f scala> fred3.contacts += wilma3 res31: fred3.contacts.type = ArrayBuffer(Network3$Member@1a30c741) ^ scala> fred3.contacts += barney3 //不同局域网添加联系 res37: fred3.contacts.type = ArrayBuffer(Network3$Member@1a30c741, Network3$Member@54b4191f)