trait

object Test05_trait {
  def main(args: Array[String]): Unit = {
    val person = new Person("AA");
    person.hello()
    person.happy()
    person.ku()
    person.HaiMan()
  }

}
class Person(name:String) extends God with Mg {
  def hello(): Unit={
    println(s"$name say hello")
  }

  // 子类来实现
  override def HaiMan(): Unit = {
    println("子类实现害人方法")
  }
}

trait God{
  def happy():Unit={
    println("god . ..... say")
  }
}

trait Mg{
  def ku():Unit={
    println("Mg . ..... say")
  }
  // 不写实现,类似java接口或抽象类方法
  def HaiMan():Unit
}

/**
 * AA say hello
 * god . ..... say
 * Mg . ..... say
 * 子类实现害人方法
 */

 

 

case

当正常操作
object Test06_case_class {
  def main(args: Array[String]): Unit = {
    val hsq1 = new Dog("hsq", 5)
    val hsq2 = new Dog("hsq", 5)
    println(hsq1) // com.xiaoke.scala.Dog@357246de
    println(hsq2) // com.xiaoke.scala.Dog@28f67ac7
    println(hsq1.equals(hsq2)) false // 因为equals底层: (this == obj);
    println(hsq1 == (hsq2))    false
    // java中 基本数据类型==比较的是值,复合数据类型,比较的是内存地址
  }

}

class Dog(name:String, age:Int){
}

当加入case
object Test06_case_class {
  def main(args: Array[String]): Unit = {
    val hsq1 = new Dog("hsq", 5)
    val hsq2 = new Dog("hsq", 5)
    println(hsq1) // Dog(hsq,5)
    println(hsq2) // Dog(hsq,5)
    println(hsq1.equals(hsq2)) true // 因为equals底层: (this == obj);
    println(hsq1 == (hsq2))    true
    // java中 基本数据类型==比较的是值,复合数据类型,比较的是内存地址
  }
}
case  class Dog(name:String, age:Int){
}

 

match

    // match 相当于java中的switch

    val tuple = (1.0, 88, "abc", false, 'a')

    val iterator = tuple.productIterator
    val units = iterator.map(
      (x) => {
        x match {
          case 1 => println(s"..$x.... is int")
          case 88 => println(s"$x .... is 88")
          case false => println(s"$x...is false")
          case w: Int if w > 50 => println(s"$w...is > 50")
          case _ => println("default")
        }
      }

    )

    while (iterator.hasNext){
      val unit = iterator.next()
      println(unit)
    }

 

partialFunction

  def ooxx: PartialFunction[ Any,String] = {
    case "hello" => "val is hello"
    case x:Int => s"$x...is int"
    case _ => "none"
  }


  def main(args: Array[String]): Unit = {
    ooxx("hello")
  }

 

 

implicit

  
  // 自定义一个foreach方法
  def main(args: Array[String]): Unit = {
    var list = new util.LinkedList[Int]()
    list.add(1)
    list.add(2)
    list.add(3)
    //list没有foreach
    def foreach[T](list:util.LinkedList[T], fun:(T)=> Unit): Unit ={
      val iter = list.iterator()
      while (iter.hasNext) {
        fun(iter.next())
      }
    }
    foreach(list, println)
  }
  
  
  
  
  优化:将参数提取到类上
        def main(args: Array[String]): Unit = {
        var list = new util.LinkedList[Int]()
        list.add(1)
        list.add(2)
        list.add(3)
        //list没有foreach
        val myself = new Myself(list)
        myself.foreach(println)
      }
    }

class Myself[T](list: util.LinkedList[T]){
      def foreach( fun:(T)=> Unit): Unit ={
        val iter = list.iterator()
        while (iter.hasNext) fun(iter.next())
      }
      
      
      
      // 使用隐式转换(隐式方法)
  def main(args: Array[String]): Unit = {
    var list = new util.LinkedList[Int]()
    list.add(1)
    list.add(2)
    list.add(3)
    //list没有foreach
    implicit def myselfMessage[T](list: util.LinkedList[T])={
      new Myself(list)
    }
    list.foreach(println)
   // myselfMessage(list).foreach(println)
  }
}
class Myself[T](list: util.LinkedList[T]){
  def foreach( fun:(T)=> Unit): Unit ={
    val iter = list.iterator()
    while (iter.hasNext) fun(iter.next())
  }
}


// 使用隐式转换(隐式类)
  def main(args: Array[String]): Unit = {
    var list = new util.LinkedList[Int]()
    list.add(1)
    list.add(2)
    list.add(3)
    //list没有foreach
    implicit class myself[T](list: util.LinkedList[T]){
      def foreach( fun:(T)=> Unit): Unit ={
        val iter = list.iterator()
        while (iter.hasNext) fun(iter.next())
      }
    }
    list.foreach(println)
    myself(list).foreach(println)
  }
}




// LinkedList更换成ArrayList 又不行了,所以更换List的父类Iterator
  def main(args: Array[String]): Unit = {
    var arrayListInfo = new util.ArrayList[Int]()
    arrayListInfo.add(1)
    arrayListInfo.add(2)
    arrayListInfo.add(3)
    var linkedListInfo = new util.LinkedList[Int]()
    linkedListInfo.add(1)
    linkedListInfo.add(2)
    linkedListInfo.add(3)
    //自定义LinkedList隐士转换
    implicit def linkedList[T](list:util.LinkedList[T])={
      val iter = list.iterator()
      new Myself(iter)
    }
    //自定义LinkedList隐士转换
    implicit def arrayList[T](list:util.ArrayList[T])={
      val iter = list.iterator()
      new Myself(iter)
    }
    arrayListInfo.foreach(println)
    linkedListInfo.foreach(println)
  }
}
class Myself[T](iter: util.Iterator[T]){
  def foreach( fun:(T)=> Unit): Unit ={
    while (iter.hasNext) fun(iter.next())
  }
}





//隐式参数
    implicit  val sss:String = "lisi"
    implicit  val aaa:Int = 18
    def ooxx(implicit name:String, age: Int): Unit ={
      println(s"name =$name,  age=$age")
    }
    ooxx
    ooxx("xiaoke", 18)
    结果:1.当不传递值,使用的是implicit的默认值 2.传递值,要么不传递,要么全部传递
    name =lisi,  age=18
    name =xiaoke,  age=18
    
    问题:如果上面的需要只传递一个值怎么办? 柯里化
    implicit  val aaa:String = "老王"
    def ooxx(age: Int)(implicit name:String): Unit ={
      println(s"name =$name,  age=$age")
    }
    ooxx(18)
    ooxx( 18)("小柯")
    结果: 
    name =老王,  age=18
    name =小柯,  age=18

 

scala写worldcount

idea中scala普通项目转maven项目
-> 右键 Add Framework Support


object WordCountScala {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setAppName("wordcount")
    // 本地单机运行
    conf.setMaster("local")
    val sc = new SparkContext(conf)
    val fileRdd = sc.textFile("D:\\code\\scala\\test\\one\\data\\testdata.txt")
    // hello world
    val words = fileRdd.flatMap((x:String)=>{x.split(" ")})
    // hello
    // world
    val tuple2 = words.map((x:String)=>{new Tuple2(x, 1)})
    // (hello, 1)
    // (world, 1)
    val res = tuple2.reduceByKey((x:Int, y:Int) => {x + y})
    /**
     * (spark,2)
     * (hello,3)
     * (msb,1)
     * (good,1)
     * (world,1)
     */
    val fanzhuan: RDD[(Int, Int)] = res.map((x)=>{  (x._2,1)  })
    /**
     * (2,1)
     * (3,1)
     * (1,1)
     * (1,1)
     * (1,1)
     */
    fanzhuan.foreach(println)
    val resOver: RDD[(Int, Int)] = fanzhuan.reduceByKey(_+_)
    /**
     * (1,3)
     * (3,1)
     * (2,1)
     */
    resOver.foreach(println)
    res.foreach(println)
    Thread.sleep(Long.MaxValue)
  }
}

pom:
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>2.3.4</version>
        </dependency>

 

posted on 2021-01-31 23:42  陕西小楞娃  阅读(80)  评论(0编辑  收藏  举报