首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 其他相关 >

scala(二)Learn CH2 Programming in Scala from others

2012-09-09 
scala(2)Learn CH2 Programming in Scala from othersscala(2)Learn CH2 Programming in Scala from other

scala(2)Learn CH2 Programming in Scala from others
scala(2)Learn CH2 Programming in Scala from others

Scala is all object
number is object, function is object. All the objects' base class is Any, AnyVal is all the valued objects' parent object consist of (int, boolean, float)
scala>1+2
scala>var f: Int =>Int = (1+)
scala>f(2)
Int=3

scala>var f: Int => Int =(3*_)
f: Int => Int =<function1>
scala>f(3)    //res6:Int =9

Anonymous Function
scala> ( (para:String) => println("hello " + para) ) ("world!")
hello world!
scala> var fn = new (String => Unit) { def apply(para:String):Unit=println("hello" + para)}
fn: java.lang.Object with String => Unit = <function1>
scala> fn(" sillycat")
hello sillycat

The Object class in eclipse:
package com.sillycat.easyscala
object Love {
  //args parameter name
  //Array[String] parameter type
  //Unit return type, equals to void
  def main(args: Array[String]): Unit = {
    //val is equals to final String exp
    val exp: String = "Happy Valenten's Day"
    val name = "Kiko"
    //String v = ""
    var v = exp + ", " + name
    println(v)
  }
}

Try to loop the function in different way:
package com.sillycat.easyscala

object Loop {

  def main(args:Array[String]) : Unit = {
    //loop like java
    var i = 0
    while (i< args.length ){
      println(args(i))
      i = i + 1
    }
   
    for(arg <- args)
      println(arg)
     
    args.foreach(arg => println(arg))
   
    args.foreach(println)
  }
}

references:
http://snowriver.org/blog/tag/scala/
http://fakechris.iteye.com/blog/105865
http://www.ibm.com/developerworks/cn/java/j-scala/
http://blog.csdn.net/ant_yan/article/details/7245890



热点排行