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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值