scala語法總結

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/humanity11/article/details/81153420
object TestScala {
  implicit def nobody(nothing:Int)=nothing.toString
//  implicit def int2String(name:Int)=name.toString
  def main(args: Array[String]): Unit = {
    //various//變量
    //    testFormat
    //    testAbstractClass
    //    testSingleton
    //    testCase
    //    println(testMkString("hello world","jelkj"))
    //    testListTuple
    //testTclass
    //testImplic(2)
    //TestImplic.testImp
    quickSort(List(23,2,1,4,43,35,4,35,23,42,43,2,34,23)).map(println)
  }
  def testImplic(name:String):Unit={
    println(name)
  }
  def testTclass={
    val t=new TestT[String,Int]("hello",23);
    t.test(List(3,2,3,23,4,54,5))
  }
//集合
  def testListTuple={
    val s=Set(234,234,3,42,3,4,3,4)
    val l=List(List(23,23),List(232,23))
    val l3=List(23,23,12,4,2)
    val l2=List(42,234,342)
    println(l(0)(1))
    val m=Map("a"->"23","b"->"wd")
    println(m.get("b").getOrElse("none"))
    println(l.flatten)
    println(l.filter(x=>x==List(23,23)))
    println(l zip l2)
    println(l3.partition(x=>x%2==0))
    println(l3.foldLeft(12)(_-_))
    println(l3.reduceLeft(_+_))
  }
//單例
  def testSingleton={
    val a1=ApplyTest()
    println(a1.count)
    a1.incr
    println(a1.count)
    val a2=ApplyTest()
    a2.incr
    println(a2.count)
  }
//抽象類
  def testAbstractClass={
    val s=new Person3 {
      override def spark: Unit = "overide test"
      override val name: String = "hello test"
      override var age: Int = _
    }
    println(s.name)
  }
//繼承
  def testClass={
    val p=new Person;
    p.name="hello"
    val s=new Student("jack",23,"shang")
    println(s.name+"<"+s.age+s.city)
    println(p.age+":"+p.age)
  }
//變量var和val
  def various = {
    var gender = "man"
    val in = "不變量"
    gender = "woman"
    println(s"---gender:${gender},and the in is: ${in}")
  }
//格式
  def testFormat={
    val name="test format"
    val age=23
    val book=Book("test","test1")
    println("name="+name,"age="+age)
    println(s"name=${name},book name is:${book.name}")
  }
  def testFunction={
    def fun1(name:String)=println("hello boy")
    def fun2(name:String):String= s"hello ${name}"
    //匿名函數
    def fun3=(name:String,age:Int)=>s"hello ${name},age" +
      s"${age}"
    def fun4(name:String)(age:Int)=name+s"${age}";//value=fun4("hello")(23)
    def fun5(name:String)=(age:Int)=>name+s"${age}"//val fun=funt("hello"),val value=fun(23);
    def fun6(name:String*)={name.foreach(println)}
    def fun7(name:String="hello")=name+"boy"//fun7()/fun7("hi")
  }
  def testConditions={
    val name=if(23>9)"hello"else "hi"
  }
//循環
  def loop={
    for(char<- "hello this is test loop")println(char)
    var chars="hello this is test loop";
    for(pos<- chars)println(pos)
    for(i<- 1 to 100 if i%3==0)println("filter i%3!=0")
  }
//case 匹配
  def testCase={
    val value=3;
    val result= value match {//normally
      case 2 =>"2"
      case 3 =>"3"
      case _=>"0"
    }
    println(result)
    val result2=value match {
      case i if i==2=>"2"
      case i if i==3 =>"3"
      case i =>"0"
    }
    println(result2)
    val result3=(obj:Any)=>{
      obj match {
        case x:Int=>2
        case x:String=>"2"
        case Book(name,mark)=>"name"
        case _=>0
      }
    }
    println(result3("hello"))
    println(result3(Book("book","3")))
  }
  def testList={
    println(List(23,23).:: (23))
    println(23::List(23,23)::List(323))
  }
  def testMkString(arg:String*)={
    arg.mkString(",")
  }
  def quickSort(list:List[Int]):List[Int]={
    list match {
      case Nil=>Nil;
      case List()=>List();
      case head::tail=>{
        val (left,right)=tail.partition(_<head)
        quickSort(left):::head::quickSort(right)
      }
    }
  }
}
case class Book(name:String,mark:String)
class Person{
  var name:String = _;//_means name is var,and name have set and get
  val age:Int=23
  private[this] val gender="male"
}
class Person2(var name:String,val age:Int){
  println("this is the premary construct")
  val city:String="shanghai"
  var school:String=_;
  def this(name:String,age:Int,school:String){
    this(name,age)
    this.school=school;
  }
}
class Student(name:String,age:Int,val major:String) extends Person2(name,age){

  println(s"htis is the subclass of person,major is ${major}")
  override val city:String="student city"//must override
}
abstract class Person3{
  def spark
  val name:String;
  var age:Int;
}
trait Logger1{
  def log(msg:String):Unit={
    println("log:"+msg)
  }
}
class Logger1Test1 extends Logger1{
  def test=log("logger")
}
trait Logger2{
  def log(msg:String)
}
class Logger2Test extends Logger2{
  override def log(msg: String): Unit = "implements logger2"
}
class ApplyTest private{//防止外部調用
  def apply() = println("class apply")
  var count:Int=0;
  def incr={
    count+=1;
  }
}
object ApplyTest{
  var applyTest:ApplyTest=_;

  def apply() = if(applyTest==null){
    println("object apply")
    applyTest= new ApplyTest
    applyTest
  }else applyTest
}
class TestT[T,S](val first:T,val second:S){
  def test[S](list:List[S])=list.foreach(println)
}
//隱士轉換,implicit關鍵字
object TestImplic{
  //隱士類,與名字無關,只與入參類型、返回值有關
  implicit class a2A(a:Int){
    def add(b:Int)=a+b;
  }
  implicit def a2A(a:A)=new A1(a)
  implicit val name:String="im param"
  def testParm(implicit name:String)=println(name)
  def testImp()={
    val a=new A();
    a.say1();//test implicit method
    testParm//test implic params
    println(5.add(6))//test implicit class
  }
}
class A1(a:A){
  def say1()={
    println("hi,i m A1")
  }
}
class A{
  def say()={
    println("hi, i am A")
  }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章