天天看點

scala(10)A Tour of Scala: Inner Classes

scala(10)A Tour of Scala: Inner Classes

Inner Classes

Just understand we can have inner classes

class Graph {

class Node {

var connectedNodes: List[Node] = Nil

def connectTo(node: Node) {

if (connectedNodes.find(node.equals).isEmpty) {

connectedNodes = node :: connectedNodes

}

}

}

var nodes: List[Node] = Nil

def newNode: Node = {

val res = new Node

nodes = res :: nodes

res

}

}

object GraphTest extends App {

val g = new Graph

val n1 = g.newNode

val n2 = g.newNode

val n3 = g.newNode

n1.connectTo(n2)

n3.connectTo(n1)

}

Mixin Class Composition

abstract class AbsIterator {

type T

def hasNext: Boolean

def next: T

}

trait RichIterator extends AbsIterator {

def foreach(f: T => Unit) { while (hasNext) f(next) }

}

class StringIterator(s: String) extends AbsIterator {

type T = Char

privatevari = 0

def hasNext = i < s.length()

def next = { val ch = s charAt i; i += 1; ch }

}

object StringIteratorTest extends App {

class Iter extends StringIterator("1234") with RichIterator

val iter = new Iter

iter foreach println

}

No one has the method foreach, but the trait has.

Nested Functions

…snip…

Anonymous Function Syntax

…snip…

Currying

…snip...

Automatic Type-Dependent Closure Construction

…snip...

Operators

Any method which takes a single parameter can be used as an infix operator in Scala.

class MyBool(x: Boolean){

def or(that: MyBool): MyBool = if(x) this else that

}

def xor(x: MyBool, y: MyBool) = (x or y)

Higher-Order Functions

class Decorator(left: String, right: String) {

def layout[A](x: A) = left + x.toString() + right

}

object FunTest extends App {

//defined the function, take other function and values as

//parameters

def apply(f: Int => String, v: Int) = f(v)

val decorator = new Decorator("[", "]")

println(apply(decorator.layout, 7)) //[7]

}

This function can take other functions and values as parameters and apply first.

Packages

A package is a special object which defines a set of member classes, objects and packages. Unlike other objects, packages are not introduced by a definition.

Import clauses

import p._ all members of p

import p.x the member x of p.

import p.{ x => a} the member x of p renamed as a.

import p.{x, y} the members x and y of p.

import p1.p2.z the member z of p2, itself member of p1.

import p1._, p2._ alternatively import p1._; import p2._

Implicitly imported into every compilation unit

java.lang

scala

scala.Predef

Pattern Matching

object MatchTest1 extends App {

def matchTest(x: Int): String = x match {

case 1 => "one"

case 2 => "two"

case _ => "many"

}

println(matchTest(3)) //many

}

object MatchTest2 extends App {

def matchTest(x: Any): Any = x match {

case 1 => "one"

case "two" => 2

case y: Int => "scala.Int"

}

println(matchTest("two")) //2

println(matchTest(2)) //scala.Int

println(matchTest(1))//one

}

Magic match, can be any types and even pattern.

References:

http://www.scala-lang.org/node/115

http://www.scala-lang.org/node/116

http://www.scala-lang.org/node/118

http://www.scala-lang.org/node/134