SCALA-DAY01

1 Introducción a scala

  1. Orientado a objetos y orientado a funciones
  2. La escritura estática mejora la eficiencia
  3. Ejecutar en JVM y JS

Caracteristicas 

  1. Elegante y conciso 
  2. alta velocidad 
  3. Compatible con la ecología HADOOP
  4. la chispa está escrita en scala
  5. La programación funcional es buena para la transmisión de datos

2 construcción del entorno scala

2.1 Configuración de variables de entorno

descargar 

 

entorno de windows

Unzip-System-Advanced Settings-System Environment Variables Configurar SCALA_HOME

entorno linux 

Descomprimir la configuración / etc / profile

2.2 IDEA construye el proyecto scala

2.2.1 Instalar el complemento de scala

retstart reiniciar IDEA

2.2.2 nuevo proyecto 

// 1 object中的所有的内容默认是static
object Demo1 {
  // 等同于java的main方法   程序的入口
  def main(args: Array[String]): Unit = {
    // 打印数据  简洁
    println("hello scala")
  }
}

Hay dos tipos de clases y objetos en scala

El contenido del objeto se modifica estáticamente de forma predeterminada, por lo que el método principal solo se puede ejecutar cuando el método principal está escrito en el objeto.

 

3 tipo de datos

Cualquiera es similar a la clase principal de todas las clases de Objeto en Java

Cualquier valor AnyVal

          Relacionado con la clase de referencia AnyRef: 1 todas las clases de java 2 clases arbitrarias de scala (clase de scala y clase de herramienta de colección)

Todos los tipos en scala son objetos (int i ---> i: Int) La clase padre de i también es Any

Null es una subclase de todos los ValRef

Nada es una subclase de todas las clases: excepción   

3.1 Tipo numérico

nombre de val: String = "zss"

val name2 = "ls"

val id: Long = 1232 

var edad = 21 

package com._51doit.day01.demo

/**
 * FileName: Demo3
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030
 * Description: 变量
 * 1 定义
 * 2 取值  打印
 */
object Demo3 {
  def main(args: Array[String]): Unit = {
    //
    /*
     */
    /**
     * 1 数据类型自动的推导
     * 2 声明数据类型
     * 3 代码的行末可以不写  不建议写 ;
     * 4 scala是一种强数据类型的编程语言  变量一旦声明数据类型是确定的不可再变 (数据类型转换)
     *   name=12 错误的
      */
    var name = "zss"   // 数据类型自动的推导
    var nickname:String = ""   // 声明数据类型
    name="lss" // 再赋值
    // name.prt   name.sout  快速返回打印
    println(name)
    /**
     * 变量定义
     *  val    value   值
     *  var    variable
     */
    val id = "abc"  // 使用val修饰的遍历不能再修改值  常量

    /**
     * 总结
     *    1  在scala中修饰变量的两个关键字 是  val 和 var
     *    2 val 常量 不能变化
     *    3 var 可以再赋值
     *    4 建议使用val声明变量
     *    5 变量定义的时候要赋值
     *  静态类型
     */
   // val age:Int  = 0

   // 变量的取值   s"$name"
    println(id)
    print(name)
    //println(name+"--"+id+"--"+nickname)
    println()
    println("$name,$id")
    println(s"$name-$id")//lss,abc
    println(s"$name")
   printf("" +
     "" +
     "" +
     "" +
     "" +
     "" +
     "" +
     "")

  }

}

3.2 Conversión de tipos de datos

1 在scala中有toXXX方法 进行类型转换的
2 v2.isInstanceOf[StringOps]
3 asInstanceOf[P] 强制类型转换   要有继承关系
有类型转换的需求使用toXX  没有对应的方法 使用 asInstanceOf[DataType]
package com._51doit.day01.demo

import scala.collection.immutable.StringOps

/**
 * FileName: DataConv
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030
 * Description:
 * 1 在scala中有toXXX方法 进行类型转换的
 * 2 v2.isInstanceOf[StringOps]
 * 3 asInstanceOf[P] 强制类型转换   要有继承关系
 * 有类型转换的需求使用toXX  没有对应的方法 使用 asInstanceOf[DataType]
 */
object DataConv {
  def main(args: Array[String]): Unit = {
    val v1 = "123"
    val v2 = 22

    /**
     * 1 在scala中有toXXX方法 进行类型转换的
     * toInt
     * toDouble
     * toFloat...
     */
    println(v1.toInt+v2)
    // 2 判断是否是某种数据类型
    val b: Boolean = v2.isInstanceOf[StringOps]
    println(b)
    // 3 asInstanceOf[P] 强制类型转换   要有继承关系
    val b1 = new B()
    val a1 = new A()
   // b1.asInstanceOf[A] 可以向上转型  强制类型转换
    /**
     * ClassCastException: com._51doit.day01.demo.A cannot be cast to com._51doit.day01.demo.B
     */
    a1.asInstanceOf[B]

  }

}

Personaliza dos clases  

/**
 * FileName: B
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030 
 * Description: 
 */
class B  extends A{
}

/**
 * FileName: A
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030 
 * Description: 
 */
class A {

}

4 Control de proceso 

4.1 si la sintaxis

1 sintaxis:

if () {} else {} 

if () {} else if () {} ... else {} 

 Si(){

    if () {} else {}

2 Definición de matriz 

3 El valor de la matriz 

4 Uso básico de la clase Random

package com._51doit.day01.demo.i_f

import java.util.Random

/**
 * FileName: IfDemo1
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030
 * Description:
 * 在scala中有流程控制语句
 * if()  else  if() else if()  else
 * if(){
 * if(){
 * if(){}else {}
 * }
 * }
 * for
 * while
 */
object IfDemo1 {
  def main(args: Array[String]): Unit = {
    // 定义个数组
    val arr = Array[String]("java", "js", "sql", "scala")
    //数组的取值  arr(index)  0开始计数
    // println(arr(1))
    // 随机获取一个数组的元素
    val random = new Random()
    // 生成一个随机数据  不会超过参数
    val i: Int = random.nextInt(arr.length)
    val sub = arr(i)
    println(sub)
    // 判断元素的值  根据值做判断
    if (sub == "java") {
      println("java基础")
    } else if (sub == "js") {
      println("javascript")
    } else if (sub == "sql") {
      println("结构化查询语言")
    } else {
      println("高级语言")
    }


  }

}
package com._51doit.day01.demo.i_f

import scala.util.Random

/**
 * FileName: IfDemo2
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030 
 * Description: 
 */
object IfDemo2 {
  def main(args: Array[String]): Unit = {
    // 定义数组
    val arr: Array[String] = Array("java", "js", "sql", "scala")
   // val arr2: Array[Int] = Array(1, 2, 3, 4)  自动类型推导
    // 取值println(arr(1))
    // 随机数据
    val random = new Random()
    val i: Int = random.nextInt(arr.length)
    val element: String = arr(i)
    println(element)
    // 判断
    if(element.startsWith("j")){
      println("以j开头")
      if(element.endsWith("a")){
        println("java基础")
      }else{
        println("javascript")
      }
    }else{
      println("不以j开头")
      if(element.length==3){
        println("结构化查询语言")
      }else{
        println("高级编程语言scala")
      }
    }
  }
}

4.2 Valor de retorno del bloque de código

package com._51doit.day01.demo.i_f

/**
 * FileName: BlockDemo
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030
 * Description: 
 */
object BlockDemo {
  def main(args: Array[String]): Unit = {
    /**
     * 1 代码块的最后一行内容作为代码块的返回值
     * 2 if的返回值是 执行分支的最后一行内容
     * 3 返回值类型设计各个分支的最后一行内容的父类型
     * 4 最后一行是 打印语句  返回的内容是 ()  : 没有返回内容
     */
    val res = {
      "hello"
      12
      12.3
      "abc"
    }
    println(res)
    val res2:Any = if ("a" != "a") {
      "a"
    } else if ("a" == "b") {
      13.2
    } else {
      12
      println("   ")
    }
    println(res2)
  }

}

4.3 para bucle

La sintaxis básica de loop fro (e <- expression | set | array)

1 a 10 [1,10]

1hasta 10 [1,10)

package com._51doit.day01.demo.f_o_r

/**
 * FileName: ForDemo
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030
 * Description:
 * for(i <- 1 to 10 ){}
 * for(i <- 1 until 10) {}
 * for(e <- arr){}
 */
object ForDemo {
  def main(args: Array[String]): Unit = {
    // 遍历数组
    val arr = Array("tom" , "jim" , "cat" , "jerry" ,"jack" , "ross")

    for(i <- 1 to 10){// 1  10
     // println(i)
    }
    //增强循环   将集合中的每个元素  依次赋值给 i变量
    val range  = 1 to 10
    for(i <- range){// [1  10]
     // println(i)
    }
    for(i <- 1 until 10){ // [1,10)
     // println(i)
    }

    // 1 依次获取数组中的每个元素 赋值给e变量
    for (e <- arr){
    //  println(e)
    }
    // 2 使用角标的形式
    for(index <- 0 to arr.length-1){
      println(arr(index))
    }
  for(i <- 0 until arr.length){
    println(arr(i))
  }
  }
}

 Modo de guardia para (i <- si)

package com._51doit.day01.demo.f_o_r

/**
 * FileName: ForDemo2
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030
 * Description:  守卫模式
 * 1 简洁 减少循环次数
 * 2 在scala中没有
 *      break  结束循环
 *      continue  跳出当次循环
 */
object ForDemo2 {
  def main(args: Array[String]): Unit = {

   /* for(i<- 1 to 10 if i%2==0){ // 单个条件守卫模式
      println(i)
    }*/
    for(i<- 1 to 10 if i%2==0 && i>5){ // 多条件的守卫模式   &&
      println(i)
    }

  /*  for(i <- 1 to 10 ){
      if(i%2!=0){
        println(i)
      }
    }*/

  }

}

Control de procesos

romper termina el bucle
continuar saltando fuera del bucle actual

volver saltar fuera del método

package com._51doit.day01.demo.f_o_r
import scala.util.control.Breaks._
/**
 * FileName: ForDemo3
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030
 * Description:   控制循环
 * 在scala中没有
 * break     结束循环
 * continue  跳出当次循环
 */
object ForDemo3 {


  def main(args: Array[String]): Unit = {
    /*  for(i <- 1 to 10){
        println(s"hello ${i}")
        if(i==5){
          return
        }
      }*/
    // 使用守卫模式实现停止当前循环
    var flag = true
    for (i <- 1 to 10 if flag) {
      println(s"hello ${i}")
      if(i==5) flag=false
    }

  }
}

Seguir 

package com._51doit.day01.demo.f_o_r

/**
 * FileName: ForDemo4
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030
 * Description: 
 */
object ForDemo4 {
  def main(args: Array[String]): Unit = {
   // val arr = Array("a","b","c" ,"f")
   // arr.foreach(e=>println(e))
   // arr.foreach(e=>println(e.toUpperCase.equals("A")))
   /* arr.foreach(e=>{
      val str: String = e.toUpperCase()
      if(str.equals("A")){
        println(str)
      }
    })*/
    import scala.util.control.Breaks._
    val arr = Array(1,2,3,4,5,6,7,8)
    arr.foreach(e=>{
      breakable(
        if(e!=5 && e!=6 && e!=2){
        println(e)
      }
      )
    })

  }
}

Derivado

/**
 * FileName: ForDemo5
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030
 * Description:
 * 推导式 yield  处理
 *    获取符合条件的每个元素 处理以后  放在新的集合中
 */
object ForDemo5 {
  def main(args: Array[String]): Unit = {
    val arr = Array(1,2,3,4,5,6,7,8,9)
    // 推导式
    val res: Array[Int] = for (e <- arr if e > 4) yield e*10
    res.foreach(e=>println(e))
  }
}

Filtre los estudiantes que aprobaron el grado y colóquelos en una nueva matriz 

/**
 * FileName: Student
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030 
 * Description: 样例类
 * 作为以前的pojo类使用
 * Student  类似于java中的javabean
 *   有getset方法
 *   重写toString
 *   重写equals  hashcode
 */
case class Student(sid:Int , name:String , score:Double)
package com._51doit.day01.demo.f_o_r

import com._51doit.day01.demo.pojo.Student

/**
 * FileName: Demo
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030
 * Description:
 * * 学生成绩类    数组
 * * 1 学生类  放在学生成绩数组
 * * 2 放在数组中  遍历  守卫模式获取及格的人  推导式放入新的数组
 */
object Demo {
  def main(args: Array[String]): Unit = {
  val stus =  Array[Student] (
    Student(1,"jim",88),
    Student(2,"tom",66),
    Student(3,"班长",99) ,
    Student(4,"rose",58)
  )
    // 遍历数组中的每个学生  守卫模式  判断学生成绩大于60 符合条件  推导式 收集符合条件的学生放入新的数组中
   // val names: Array[String] = for (stu <- stus if stu.score > 60) yield stu.name.toUpperCase
    val res: Array[Student] = for (stu <- stus if stu.score > 60) yield stu
    //res.foreach(println(_))
    res.foreach(println(_))

  }

}

Bucle anidado  

/**
 * FileName: ForDemo6
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030
 * Description:  嵌套循环 
 */
object ForDemo6 {
  def main(args: Array[String]): Unit = {
   /* for(i <- 1 to 9 ; j<-1 to 3){
      println(s"$i $j")
    }*/
    for(i <- 1 to 9 ; j <- 1 to i){
      print(s"$j * $i ="+(i*j)+"\t")
      if(i==j){
        println()
      }
    }
  }
}

5 recorrido de matriz

para (e <- arr) 

para (i <- 0 a | hasta longitud arr. [-1]) 

arr. para cada 

arr.map

/**
 * FileName: ArrayEach
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030
 * Description: 
 */
object ArrayEach {
  def main(args: Array[String]): Unit = {
    val arr = Array("tom", "jim", "peiqi", "乔治", "nimou")
    // 1 遍历   arr.for 回车
    for (elem <- arr) {
    }
    // 2 角标
    for (i <- 0 to arr.length - 1) {
      arr(i)
    }
    //3 角标
    for (i <- 0 until arr.length) {
      arr(i)
    }
    // 4 forEach  方法是没有返回值  常用于集合打印
    // arr.foreach(e=>println(e))  // e=>e     _
    // arr.foreach(println(_))
    // arr.foreach(println) //  默认打印每个元素
    //arr.foreach(e=>println(e.toUpperCase))
    // 打印数据
    // arr.foreach(e=>print(e+"  "))
    // 处理数据
    var res = ""
    arr.foreach(e => { // =>后面的代码是每个元素执行一次
      res += e + " "
    })
    // println(res.trim)
    // 5  map方法  方法是可以有返回值   对集合中的每个元素进行操作
    // arr.map(println)
    //  arr.map(println(_))
    // arr.map(e=>println(e))
    //val res2: Array[String] = arr.map(_.toUpperCase)
    /*val res3: Array[Any] = arr.map(e=>{
      if(e.startsWith("j")){
        e.toUpperCase
      }
    })*/

    /**
     * _ 代表每个元素
     */
    arr.foreach(println)
    arr.foreach(println(_))
    arr.map(_.toUpperCase)
    arr.map(e => println(e))
  }

}

5 Métodos y funciones 

¿Cuál es el método? Es la encapsulación de una pieza de código lógico para completar la tarea de procesar datos.

Valor de retorno Nombre del método (tipo de parámetro)

Función: la función del método es la misma que una pieza de código lógico para completar la tarea de procesar datos, llamar y reutilizar

Las funciones son más avanzadas que los métodos, pueden existir solas, entendidas como variables      especiales, las funciones se pueden utilizar como parámetros o valores de retorno.

5.1 Definición de método 

package com._51doit.day01.demo.f_m

/**
 * FileName: Demo1
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030
 * Description: 
 */
object Demo1 {

  def  maxValue(a:Double , b:Double ,c:Double) ={
   var max:Double = 0
    if (a>b && a>c){
      max = a
    }else if(b>c){
      max = b
    }else{
      max =c
    }
    max
  }


  /**
   * 定义一个方法
   *   处理的数据
   *   返回的数据
   *   怎么处理
   *   关键字  def 定义方法
   *   add  方法名
   *   (x:Int , y:Int , z:Int) 方法的参数类型列表
   *   {x+y+z}  方法体
   *   Unit 是没有返回值的方法  void
   */
  def add(x:Int , y:Int , z:Int)={
    println(x+y+z)
  }

  /**
   * 没有参数的方法  ()
   * 调用的时候可以写()  可以不写
   */
  def  show(): Unit ={
    println("I show my  face!!")
  }

  /**
   * 没有参数的方法  ()可以省略 调用的时候省略()
   */
  def  show2(): Unit ={
    println("I show your face!!")
  }
  def main(args: Array[String]): Unit = {
    add(1,2,6)
    val max = maxValue(100,34,67)
    println(max)
    show()
    show2()
  }
}
package com._51doit.day01.demo.f_m

/**
 * FileName: Demo2
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030 
 * Description: 
 */
class Demo2 {

  /**
   *
   */
  def muti(x:Int,y:Int) ={
    x*y
  }

}

 

package com._51doit.day01.demo.f_m

/**
 * FileName: Test1
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030
 * Description:
 * 方法的定义
 *  def  方法名(参数列表):返回值={方法体}
 *  方法的调用
 *  1 如果方法在object中  静态方法 直接 Demo1.方法名(实参)
 *  2 如果方法在class中  普通方法  new Demo2().方法(实参)
 */
object Test1 {
  def main(args: Array[String]): Unit = {
    Demo1.show2()
    val res: Int = new Demo2().muti(3, 2)
    println(res)
  }

}

5.2 Definición de función

package com._51doit.day01.demo.f_m

/**
 * FileName: Demo3
 * Author:   多易教育-DOIT
 * Date:     2020/10/30 0030
 * Description:  函数的定义和调用
 *   函数 : 功能和方法一样
 *接收数据     返回结果处理数据
 *  () =>{}
 *  要处理的数据类型  => {怎么处理}
 */
object Demo3 {

  /**
   * 定义一个函数  将两个字符串转大写以后拼接
   */
 val f1 = (str1:String, str2:String)=>{
    (str1+str2).toUpperCase  //代码块的最后一行是函数的返回值
  }

 /* def m(str1:String, str2:String)={
    (str1+str2).toUpperCase
  }*/
  val f2 = (x:Long, y:Long)=>{
    x+y
  }


  def main(args: Array[String]): Unit = {
    val res: String = f1("abc", "efg")
    println(res)


  }

}

 

 

 

 

 

 

 

 

 

Supongo que te gusta

Origin blog.csdn.net/qq_37933018/article/details/109378812
Recomendado
Clasificación