Scala面向对象编程 类 类定义 定义一个计数器类,可以对计数器增加某值,也可以获取计数器当前值。
常规方式 Counter.scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 package cn.studybigdata.scalaclass Counter { var value = 0 def increment (num: Int ): Unit = { value = value + num } def current (): Int = { return value } } object Counter { def main (args: Array [String ]): Unit = { val counter = new Counter () counter.value = 5 println(counter.value) counter.increment(1 ) println(counter.current()) } }
成员可见性
Counter.scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 package cn.studybigdata.scalapackage oop.accessclass Counter { private var count = 0 def value : Int ={ count } def value_= (v:Int ): Unit ={ if (v>0 ) count = v } def increment (num: Int ): Unit = { count = count + num } def current (): Int = { return count } } object Main { def main (args: Array [String ]): Unit = { val counter = new Counter () counter.value = -1 println(counter.value) counter.increment(1 ) println(counter.current()) } }
方法定义 def 方法名(参数列表):返回结果类型={方法体}
方法参数
方法小括号
无参方法可以不加括号,调用时也不要带括号;
无参方法带括号,调用时最好也带上括号;
方法体大括号
方法调用
方法只有一个参数时,可以使用中缀操作符方式调用方法
返回值
可以省略return关键字,方法体中最后一个语句的值作为返回值
建议添加返回值类型。
不需要返回值时,可以省略 =,但是不要省略大括号
构造器 对象 继承 特质 模式匹配 包 面向对象小例子 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 package cn.studybigdata.scalapackage oop.absclasstrait Flyable { var maxFlyHeight: Int def fly (): Unit } trait HasLegs { var legs: Int = 4 def run (): String = { if (legs == 2 ) { s"I have $legs legs, I run slower" } else if (legs == 4 ) { s"I have $legs legs, I run faster" } else { s"I have $legs legs" } } } abstract class Animal { var id: Int = Animal .increaseId val name: String var age: Int = _ def action () def speak (): Unit = { println(s" name: $name ; age: $age !" ) } } object Animal { var id = 0 def increaseId : Int = { id += 1 id } } class Dog (override val name: String , age: Int ) extends Animal with HasLegs with Flyable { private var weight: Int = _ def this (name: String , age: Int , weight: Int ) { this (name, age) println(age) this .weight = weight } override def action (): Unit = { println("看家护院" ) } override def speak (): Unit = { println(s"id: $id ; name: $name ; age: $age ; weight:$weight !" ) } override def run (): String = { s"I am $name , I have $legs legs, I run very fast" } override var maxFlyHeight: Int = 0 override def fly (): Unit = { println("I can not Fly" ) } } object Dog { def apply (name: String , age: Int ): Dog = new Dog (name, age) def apply (name: String , age: Int , weight: Int ): Dog = new Dog (name, age, weight) def unapply (dog: Dog ): Option [(Int , String , Int , Int )] = Some (dog.id, dog.name, dog.age, dog.weight) } class Bird (override val name: String , age: Int ) extends Animal with Flyable { override def action (): Unit = { println("消灭害虫" ) } override def speak (): Unit = { println(s"id: $id ; name: $name ; age: $age ; maxFlyHeight:$maxFlyHeight !" ) } override var maxFlyHeight: Int = 1000 override def fly (): Unit = { println(s" I am %s , I can fly %d m" , name, maxFlyHeight) } } object Test { def main (args: Array [String ]): Unit = { val bird1 = new Bird ("bird1" ,5 ) bird1.speak() val dog1 = Dog ("dog1" , 10 ) dog1.age = 11 dog1.speak() println(dog1.run()) val Dog (id, name, age, weight): Dog = dog1 println(id) println(name) println(age) println(weight) } }