Scala 基础语法代码<转>

来源:互联网 发布:软件人才外包 编辑:程序博客网 时间:2024/06/05 05:39

下面的代码包含了基本的Scala的语法内容。包括:判断,循环代码片段,方法定义,调用。 虚拟类,继承,接口,case,package,单例模式


[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package org.exinglo.scala  
  2.   
  3. class LearnScala {  
  4.   
  5. }  
  6.   
  7. object Test{  
  8.   val variable1 = "this is my first scala variable"  
  9.       
  10.   def func1(): String = "hello world" //不带参数的函数被调用时,可以不加括号  
  11.       
  12.   def func2(who : String): String = { return "hello world " + who}  
  13.     
  14.   def func3 = (x : Int, y : Int) => x + y //这是个匿名函数的写法,调用的时候,func3(1,2)  
  15.     
  16.   //这里给了y一个默认值,如果用户传过来,那就用用户的  
  17.   def func4(x: Int)(y : Int = 1): Int = x + y //加脂法的写法,调用时, func4(1)(2)  
  18.     
  19.   // * 代表多个参数的意思,比如 printEveryChar("c","b", "a"),可以传递多个string  
  20.   def printEveryChar(c : String*) = {  
  21.     c.foreach(x => println(x))  
  22.   }  
  23.       
  24.   def main(args : Array[String]) {  
  25.     println(variable1)  
  26.       
  27.     for(i <- 0 to 10 if i%2 == 0){  
  28.     println("i is a even number: " + i)  
  29.     }  
  30.       
  31.     for(i <- 0 until 10){  
  32.       println("")  
  33.     }  
  34.       
  35.     var (n, r) = (100)  
  36.     while(n > 0){  
  37.       n = n - 1  
  38.     }  
  39.       
  40.     val p = new Person //这里可以省略括号  
  41.     println("information about person:" + p.name + " " + p.age) //如果p.name没有赋值的话,这里会显示为null  
  42.       
  43.   }  
  44.     
  45.     
  46. }  


[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package org.exinglo.scala  
  2.   
  3. class Person{ //类默认是public类型的  
  4.   var name : String = _ //会生成setter和getter方法  
  5.   val age = 10 //只会生成getter方法  
  6.   private[this] val gender = "male" //无法在下面的main方法访问gender,这个属性只能在对象内部访问  
  7.       
  8. }  
  9.   
  10.   
  11. //生成对象时 val p = new Person2("Java", 10)  
  12. //如果参数声明时不带val或者var,那么这个变量相当于内部变量,比如 class Person(name: String) 这里的name就无法在外部被访问到了  
  13. class Person2(val name : String, val age : Int){//括号中的内容属于主构造器,其中的参数会作为类的属性存在  
  14.   println("this is the person2's constructor")//这里属于默认构造器,生成对象时,会执行这句话  
  15.     
  16.   var gender : String = _ //这里要求初始化  
  17.     
  18.   //附属构造器必须叫做this,必须首先调用已经存在的子构造器或者其他附属构造器  
  19.   def this(name : String, age : Int, gender : String){  
  20.     this(name, age)//必须调用默认的构造器,=  
  21.     this.gender = gender  
  22.   }  
  23. }  
  24.   
  25. //成成Student对象的时候,会先打印父类构造器的打印语句,然后但因student类的语句  
  26. class Student(val grade : Int, name : String, age : Int) extends Person2(name, age) {  
  27.   println("this is the subclass of Person, grade is:" + grade)  
  28.     
  29.   override def toString() : String =  {""//覆盖父类的方法,一定要加上override关键字,其实不仅仅是父类的方法,覆盖父类的var val都要加上override  
  30. }  


[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package org.exinglo.abstractandtrait  
  2.   
  3. class ABstractAndTrait {  
  4.   
  5. }  
  6.   
  7. //抽象类(abstract class)  
  8. //• 类的⼀一个或者多个⽅方法没有没完整的定义, 当然也可以没有任何抽象方法。  
  9. //  对于子类覆盖父类的非抽象方法,一定要override关键字  
  10. //• 声明抽象⽅方法不需要加abstract关键字,只需要不写⽅方法体  
  11. //• ⼦子类重写⽗父类的抽象⽅方法时不需要加override  
  12. //• ⽗父类可以声明抽象字段(没有初始值的字段)  
  13. //• ⼦子类重写⽗父类的抽象字段时不需要加override  
  14. abstract class PersonX{  
  15.   def speak  
  16.   val name:String  
  17.   val age:Int  
  18. }  
  19.   
  20. class Student extends PersonX{  
  21.     
  22.   //必须要实现抽象方法,像这种不写等于号的情况,表示返回类型是Unit类型  
  23.   def speak{  
  24.     println("speak")  
  25.   }  
  26.     
  27.   val name:String = ""  
  28.   val age:Int = 3  
  29. }  
  30.   
  31. trait Logger{  
  32.   def log(msg:String)  
  33. }  
  34.   
  35. //这里的trait consoleLogger可以不实现父类的方法,也可以覆盖父类的方法实现,也可以实现父类的虚方法  
  36. trait ConsoleLogger extends Logger{  
  37.   def log(msg:String){  
  38.     println("save money:" + msg)  
  39.   }  
  40. }  
  41.   
  42. //因为是覆盖上级的方法,必须使用override关键字  
  43. trait MessageLogger extends ConsoleLogger{  
  44.   override def log(msg:String){  
  45.     println("save money to account:" + msg)  
  46.   }  
  47. }  
  48. //如果一个类没有继承其他的类,实现的第一个trait只能用extends,其他的用with关键字  
  49. class Test extends ConsoleLogger{  
  50.   def test{  
  51.     log("hellp")  
  52.   }  
  53. }  
  54.   
  55. abstract class Account{  
  56.   def save  
  57. }  
  58.   
  59. class MyAccount extends Account with ConsoleLogger{  
  60.   def save{  
  61.     log("100")  
  62.   }  
  63. }  
  64.     
  65. object Run extends App{  
  66.     
  67.   //这里是一个很好玩的地方,单独给一个对象混入了一个特质trait,并且这个特质与类的定义继承的特质有冲突  
  68.   //打印出来的是MessageLogger中的log方法  
  69.   val acc = new MyAccount with MessageLogger  
  70.   acc.save  
  71. }  


[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package org.exinglo.abstractandtrait  
  2.   
  3. class ApplyTest(val msg:String) {  
  4.   def apply() = msg  
  5.   def test{  
  6.     println(msg)  
  7.   }  
  8. }  
  9.   
  10. object ApplyTest{  
  11.   //下面是apply方法的最常用的方式  
  12.   def apply() = new ApplyTest("object calls new")  
  13.     
  14.   //object的方法相当于java中的静态方法  
  15.   def static{  
  16.     println("I am a static method")  
  17.   }  
  18. }  
  19.   
  20. object Basic extends App{  
  21.     
  22.   //类似于下面,嗲用object的方法  
  23.   ApplyTest.static  
  24.     
  25.   val t = new ApplyTest("new")  
  26.   println(t())  
  27.     
  28.   //据说下面这样可以调用object ApplyTest中的apply方法,但是我的编译器总是报错  
  29.   //后来发现是object的apply方法一开始声明没有加上小括号造成的,如def apply = xxxx  
  30.   val tt = ApplyTest()  
  31.   println(tt())  
  32.     
  33.   // ApplyTest()是调用object的apply方法  
  34.   // val t = new ApplyTest(); t() 调用的是class ApplyTest的apply方法  
  35. }  


[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package org.exinglo.abstractandtrait  
  2. /* 
  3.  * package com.xyz{ 
  4.  * //------------- 
  5.  *  package spark 
  6.  *  } 
  7.  * } 
  8.  *  
  9.  * { //这里只在程序块中可见 
  10.  *   import com.yyy 
  11.  *    
  12.  * } 
  13.  *  
  14.  * import java.util.{HashMap =>JavaHashMap} //定义一个别名 
  15.  *  
  16.  *  
  17.  * package aa.bb.cc.dd 
  18.  *  
  19.  * class Xxx{ 
  20.  *   private[dd] def variable = {} //根据private[]中指定的不同,可见范围不同 
  21.  * } 
  22.  *  
  23.  * */  
  24. class PackageAndCase {  
  25.   
  26. }  
  27.   
  28. object BasicT extends App{  
  29.   val value = 3  
  30.     
  31.   val result = value match{  
  32.     case 1 => "one"  
  33.     case 2 => "two"  
  34.     case _ => "some other number"  
  35.   }  
  36.     
  37.   val result2 = value match{  
  38.     case i if i == 1 => "one"  
  39.     case i if i == 2 => "two"  
  40.     case _ => "other number"  
  41.   }  
  42.     
  43.   def t(obj : Any) = obj match{  
  44.     case x:Int => println("Int")  
  45.     case s:String => println("String")  
  46.     case _ => println("unkonwn type")  
  47.   }  
  48.     
  49.   t(2)  
  50. }  
0 0
原创粉丝点击