Class
Scala中,class方式定义如下:
class className(param:paramType,...){}
class 实例化的方法和Java类似,也是用new关键字
Scala 中,定义class时候同时定义了class的primary constructor,每个class 的primary constructor就是 class的定义部分:
className(param:paramType,...)
定义class时,如果传入的参数指明是val/var类型,则该参数成为类的一个val/var类型成员变量,否则该参数仅仅是primary constructor的参数
比如:
scala> class Demo(a:Int){}
defined class Demo
scala> val demo = new Demo(1)
demo: Demo = Demo@4c7b39a1
scala> demo.a
:10: error: value a is not a member of Demo
demo.a
^
scala> class Demo(val a:Int){}
defined class Demo
scala> val demo = new Demo(1)
demo: Demo = Demo@4a6483ee
scala> demo.a
res2: Int = 1
scala> demo.a = 121
:9: error: reassignment to val
demo.a = 121
^
scala> class Demo(var a:Int){}
defined class Demo
scala> val demo = new Demo(1)
demo: Demo = Demo@22e93648
scala> demo.a=10
demo.a: Int = 10
scala> demo.a
res3: Int = 10
class中可以定义函数,和普通函数定义方式类似:
scala> class Demo(val a:Int){
| def printA(){
| println(a)
| }
| }
defined class Demo
scala> val demo = new Demo(1)
demo: Demo = Demo@18980fb7
scala> demo.printA
1
scala中,如果被调用的函数函数列表为空,也可以省略(),如上面代码所示
如果不指定primary constructor参数为val类型,则可以在class内部用变量保存参数值,比如:
scala> class Demo(a:Int,b:Int){
| val val1 = a
| val val2 = b
| }
defined class Demo
scala> val demo = new Demo(1,2)
demo: Demo = Demo@29f70c05
scala> demo.val1
res20: Int = 1
scala> demo.val2
res21: Int = 2
类成员访问限定符:
scala 中,访问限定符有三种,默认, private, protected;
默认类型和Java中public类似,不需要显示指明;
private类型只能在class内部访问,protected类型只能在子类中访问;
默认类型成员变量和protected类型成员变量可以被子类继承。
示例如下:
scala> class Demo {
| private val a = "A"
| protected val b = "B"
| val c = "C"
|
| def printA() = println(a)
| def printB() = println(b)
| def printC() = println(c)
| }
defined class Demo
scala> val demo = new Demo()
demo: Demo = Demo@1dc8e190
scala> demo.a
:10: error: value a in class Demo cannot be accessed in Demo
demo.a
^
scala> demo.printA
A
scala> demo.b
:10: error: value b in class Demo cannot be accessed in Demo
Access to protected value b not permitted because
enclosing object $iw is not a subclass of
class Demo where target is defined
demo.b
^
scala> demo.printB
B
scala> demo.c
res7: String = C
scala> demo.printC
C
辅助构造函数
除了Primary Constructor,scala中还可以定义辅助构造函数,定义方式如下:
def this(......)
比如:
scala> class Demo(val a:Int,val b:Int){
| def this(a:Int) = this(a,1)
| }
defined class Demo
scala> val demo = new Demo(0)
demo: Demo = Demo@26ec5d79
scala> demo.a
res18: Int = 0
scala> demo.b
res19: Int = 1
class 继承
scala中class继承和Java中类似,也是使用关键字extends,但是只有子类的Primary Constructor能够传递参数给父类的构造函数,和Java类似,Scala中,class
只能单继承,不能多继承。示例如下:
scala> class Parent(val a:Int,val b:Int) {
| def print() = println("From Parent")
| }
defined class Parent
scala>
| class Child(override val a:Int,override val b:Int) extends Parent(a,b) {
| override def print() = println("From child")
| }
defined class Child
scala> class Child2(a:Int,b:Int) extends Parent(a,b) {
| override def print() = println("From child")
| }
defined class Child2
scala> val child = new Child(1,2)
child: Child = Child@51743c6e
scala> child.a
res22: Int = 1
scala> child.b
res23: Int = 2
scala> val child2 = new Child2(2,3)
child2: Child2 = Child2@3d63ea08
scala> child2.print
From child
scala> child2.a
res28: Int = 2
scala> child2.b
res29: Int = 3
子类覆盖父类属性、方法,需要添加override关键字,如上所示。
参考自:
Programming Scala 2nd Edition
阅读(878) | 评论(0) | 转发(0) |