|
| 1 | +- Class |
| 2 | + |
| 3 | +```scala |
| 4 | +import scala.beans.BeanProperty |
| 5 | + |
| 6 | +abstract class Colorable(@BeanProperty var color: Int) |
| 7 | + |
| 8 | +class Point(protected var xc: Int, protected var yc: Int, color: Int) extends Colorable(color) { |
| 9 | + def this() = this(Int.MaxValue, Int.MaxValue, Int.MaxValue) |
| 10 | + |
| 11 | + def step(dx: Int, dy: Int) { |
| 12 | + this.xc = this.xc + dx |
| 13 | + this.xc = this.xc + dy |
| 14 | + } |
| 15 | + def move(x: Int, y: Int) = { |
| 16 | + this.xc = x |
| 17 | + this.yc = y |
| 18 | + this |
| 19 | + } |
| 20 | + override def toString(): String = s"($xc, $yc, $color)" |
| 21 | +} |
| 22 | + |
| 23 | +val pt = new Point (1,2,3) |
| 24 | +pt.setColor(456) |
| 25 | +println(pt.getColor()) |
| 26 | +``` |
| 27 | + |
| 28 | +- Case Class |
| 29 | + |
| 30 | +-- pattern matching support |
| 31 | +-- default implementations of equals and hashCode |
| 32 | +-- default implementations of serialization |
| 33 | +-- a prettier default implementation of toString, and |
| 34 | +-- the small amount of functionality that they get from automatically inheriting from scala.Product |
| 35 | + |
| 36 | +```scala |
| 37 | +case class Point (x: Int, y: Int, color: Int) |
| 38 | +val pt = Point(1, 2, 3) |
| 39 | +pt.productIterator.foreach(println) |
| 40 | + |
| 41 | +abstract class Colorable { |
| 42 | + def color: Int |
| 43 | +} |
| 44 | +case class Point(x: Int, y: Int, color: Int) extends Colorable |
| 45 | +val pt = Point(1, 2, 3) |
| 46 | +println(pt.isInstanceOf[Product]) |
| 47 | + |
| 48 | +pt.productIterator.foreach(println) |
| 49 | +pt.productElement(0) |
| 50 | +println(pt.toString) |
| 51 | +``` |
| 52 | + |
| 53 | +- Inner Class |
| 54 | + |
| 55 | +```scala |
| 56 | +class Graph { |
| 57 | + class Node { |
| 58 | + var connectedNodes: List[Node] = Nil |
| 59 | + } |
| 60 | + var nodes: List[Node] = Nil |
| 61 | + |
| 62 | + def newNode: Node = { |
| 63 | + val res = new Node |
| 64 | + nodes = res :: nodes |
| 65 | + res |
| 66 | + } |
| 67 | + |
| 68 | + def addNode(node: Node): this.Node = { |
| 69 | + nodes = node :: nodes |
| 70 | + node |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +val g1 = new Graph |
| 75 | +val n = g1.newNode |
| 76 | +g1.addNode(n) |
| 77 | + |
| 78 | +val g2 = new Graph |
| 79 | +val g2.addNode(n) // error found g1.Node, require g2.Node |
| 80 | + |
| 81 | +``` |
| 82 | + |
| 83 | +- Trait |
| 84 | + |
| 85 | +```scala |
| 86 | +trait Similarity[T] { |
| 87 | + var threshold: Int = 100 |
| 88 | + def isSimilar(x: T): Boolean |
| 89 | + def isNotSimilar(x: T): Boolean = !isSimilar(x) |
| 90 | +} |
| 91 | + |
| 92 | +trait Moveable[T] { |
| 93 | + def move(other: T): T |
| 94 | +} |
| 95 | + |
| 96 | +abstract class Colorable(val color: Int) |
| 97 | +class Point(var x: Int, var y: Int, color: Int) extends Colorable(color) with Similarity[Point] with Moveable[Point] { |
| 98 | + def isSimilar(that: Point): Boolean = if (that == null) { |
| 99 | + false |
| 100 | + } else if (Math.abs(x - that.x) < threshold && Math.abs(y - that.y) < threshold) { |
| 101 | + true |
| 102 | + } else { |
| 103 | + false |
| 104 | + } |
| 105 | + def move(other: Point) = { |
| 106 | + this.x = other.x |
| 107 | + this.y = other.y |
| 108 | + this |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +- Anonymous Class |
| 114 | + |
| 115 | +```scala |
| 116 | +trait SimpleTrait { |
| 117 | + def add1(x: Int) = { x + 1 } |
| 118 | + def add2(x: Int) |
| 119 | +} |
| 120 | + |
| 121 | +abstract class SimpleClass { |
| 122 | + def add3(x: Int) = { x + 3 } |
| 123 | + def add4(x: Int) |
| 124 | +} |
| 125 | + |
| 126 | +val a = new SimpleClass { |
| 127 | + def add4(x: Int) = x + 4 |
| 128 | +} |
| 129 | + |
| 130 | +val a = new SimpleTrait { |
| 131 | + def add2(x: Int) = x + 2 |
| 132 | +} |
| 133 | + |
| 134 | +val a = new SimpleClass with SimpleTrait { |
| 135 | + def add2(x: Int) = x + 2 |
| 136 | + def add4(x: Int) = x + 4 |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +- Implicit Class |
| 141 | + |
| 142 | +Restrictions: |
| 143 | +1. They must be defined inside of another trait/class/object |
| 144 | +2. They may only take one non-implicit argument in their constructor. |
| 145 | +3. There may not be any method, member or object in scope with the same name as the implicit class. |
| 146 | + |
| 147 | + |
| 148 | +``` |
| 149 | +object Helpers { |
| 150 | + implicit class IntWithTimes(x: Int) { |
| 151 | + def times[A](f: => A): Unit = { |
| 152 | + def loop(current: Int): Unit = |
| 153 | + if(current > 0) { |
| 154 | + f |
| 155 | + loop(current - 1) |
| 156 | + } |
| 157 | + loop(x) |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | +
|
| 162 | +import Helpers._ |
| 163 | +5.times(println("HI")) |
| 164 | +
|
| 165 | +``` |
| 166 | + |
| 167 | +A more concrete example: |
| 168 | + |
| 169 | +```scala |
| 170 | +class RDD(data: String) { |
| 171 | + def collect() = data |
| 172 | + def compute() = println("processing..") |
| 173 | +} |
| 174 | + |
| 175 | +abstract class SQLContext { |
| 176 | + def env: String |
| 177 | + def sql(strText: String): RDD = new RDD(s"[$env]result of executing $strText") |
| 178 | +} |
| 179 | + |
| 180 | +object LocalMode { |
| 181 | + implicit object sqlContext extends SQLContext { |
| 182 | + def env = "LocalMode" |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +object ClusterMode { |
| 187 | + implicit object sqlContext extends SQLContext { |
| 188 | + def env = "ClusterMode" |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +object myApp { |
| 193 | + // will search SQLContext instance from the scope |
| 194 | + implicit class SqlExecutor(sql: String)(implicit context: SQLContext) { |
| 195 | + def run = { |
| 196 | + val rdd = context.sql(sql) |
| 197 | + rdd.compute() |
| 198 | + rdd.collect() |
| 199 | + } |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +import myApp._ |
| 204 | +import ClusterMode._ // import LocalMode._ |
| 205 | +"select * from src".run |
| 206 | + |
| 207 | +``` |
| 208 | + |
| 209 | +- Operators |
| 210 | + |
| 211 | +```scala |
| 212 | +case class N(val x: Int) { |
| 213 | + def +(that: N) = N(x + that.x) |
| 214 | + def add(that: N) = this + that |
| 215 | + |
| 216 | + def -(that: N) = N(x - that.x) |
| 217 | + def minus(that: N) = this - that |
| 218 | + |
| 219 | + def - = N(-x) |
| 220 | + def negate = this -; // ; is required, it may causes ambiguities |
| 221 | + def unary_- = this -; // this is a hack |
| 222 | +} |
| 223 | + |
| 224 | +-(N(2) + N(3)) // functional style |
| 225 | +((N(2).+(N(3)))).- // OO style |
| 226 | +``` |
| 227 | + |
| 228 | +- Object & Companion Object |
| 229 | + |
| 230 | +Some most used function, which called implicitly |
| 231 | +1. def apply |
| 232 | +2. def unapply |
| 233 | +3. def update |
| 234 | + |
| 235 | +```scala |
| 236 | +class Point (val x: Int, val y: Int, val color: Int) |
| 237 | + |
| 238 | +object ConstantShape { |
| 239 | + val defaultColor = Int.MinValue |
| 240 | +} |
| 241 | + |
| 242 | +// object is singleton |
| 243 | +object Point { // companion object with the same name of the class, must defined in the file of the class |
| 244 | + val defaultColor = ConstantShape.defaultColor |
| 245 | + // built-in function |
| 246 | + def apply(x: Int, y: Int): Point = new Point(x, y, defaultColor) |
| 247 | + def apply(x: Int, y: Int, color: Int): Point = new Point(x, y, color) |
| 248 | +} |
| 249 | + |
| 250 | +Point(1, 2) // Point.apply(1, 2), without the companion object we have to add the “new” e.g. val a = new Point(1,2,3) |
| 251 | +Point(1, 2, 3) // Point.apply(1, 2, 3) |
| 252 | + |
| 253 | +class Row(val values: Array[Any]) { |
| 254 | + def apply(ordinal: Int): Any = values(ordinal) |
| 255 | + def apply(ordinal: Int, defaultValue: Any): Any = { |
| 256 | + if (values(ordinal) == null) { |
| 257 | + defaultValue |
| 258 | + } else { |
| 259 | + values(ordinal) |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + def update(ordinal: Int, value: Any) { |
| 264 | + values(ordinal) = value |
| 265 | + } |
| 266 | +} |
| 267 | + |
| 268 | +val row = new Row(Array(1, "abc")) |
| 269 | +row(0) = null // update |
| 270 | +println(row(0)) // call the apply version 1 |
| 271 | +println(row(0, "aaaa")) // call the apply version 2 |
| 272 | + |
| 273 | + |
| 274 | +object Extractor { // extractor |
| 275 | + def unapply(input: String): Option[(Int, Int, Int)] = if (input != null) { |
| 276 | + val parts = input.split(",") |
| 277 | + if (parts.length == 3) { |
| 278 | + // TODO more checking |
| 279 | + Some((parts(0).toInt, parts(1).toInt, parts(2).toInt)) |
| 280 | + } else { |
| 281 | + None |
| 282 | + } |
| 283 | + } else { |
| 284 | + None |
| 285 | + } |
| 286 | +} |
| 287 | + |
| 288 | +"1,2,3" match { |
| 289 | + case Extractor(x, y, color) => println (s"$x $y $color") |
| 290 | + case _ => sys.error("something wrong!") |
| 291 | +} |
| 292 | + |
| 293 | +"1,2,3,4" match { |
| 294 | + case Extractor(x, y, color) => println (s"$x $y $color") |
| 295 | + case _ => sys.error("something wrong!") |
| 296 | +} |
| 297 | + |
| 298 | +``` |
| 299 | + |
| 300 | +- Explicit Self-Type Reference |
| 301 | + |
| 302 | +Normally used in the Dependency Injection |
| 303 | + |
| 304 | +``` |
| 305 | +abstract class ConnectionPool { |
| 306 | + def getConnection: java.sql.Connection |
| 307 | +} |
| 308 | +
|
| 309 | +abstract class MySQLConnectionPool extends ConnectionPool { |
| 310 | + def getConnection: java.sql.Connection = { |
| 311 | + println("getting MySQL connection") |
| 312 | + null |
| 313 | + } |
| 314 | +} |
| 315 | +
|
| 316 | +trait SQLServerConnectionPool extends ConnectionPool { |
| 317 | + def getConnection: java.sql.Connection = { |
| 318 | + println("getting SQL Server connection") |
| 319 | + null |
| 320 | + } |
| 321 | +} |
| 322 | +
|
| 323 | +trait UserDao { |
| 324 | + self: ConnectionPool => |
| 325 | + def authenticate(user: String, pass: String) = { |
| 326 | + val conn = getConnection |
| 327 | + println ("authenticating..") |
| 328 | + true |
| 329 | + } |
| 330 | +} |
| 331 | +
|
| 332 | +trait BusinessDao { |
| 333 | + self: ConnectionPool => |
| 334 | + def makeDeal(fromUser: String, toUser: String, deal: Int) { |
| 335 | + val conn = getConnection |
| 336 | + println(s"making deal $fromUser -> $toUser: deal: $deal") |
| 337 | + } |
| 338 | +} |
| 339 | +
|
| 340 | +// Dependencies Injection |
| 341 | +val dao = new SQLServerConnectionPool with UserDao |
| 342 | +dao.authenticate("user1", "pass1") |
| 343 | +val dao2 = new MySQLConnectionPool with UserDao with BusinessDao |
| 344 | +dao2.authenticate("user1", "pass1") |
| 345 | +
|
| 346 | +
|
| 347 | +``` |
| 348 | + |
| 349 | + |
0 commit comments