scala学习day02

1.scala里的特殊类,样例类

package com.shujia.scala

object Demo7CaseClass {
  def main(args: Array[String]): Unit = {

    //样例 类可以不通过new 创建对象
    val student: Studnet2 = Studnet2("张三", 23, "文科一班")

    println(student)
    //可以直接通过属性名操作属性
    println(student.age)

  }
}

/**
  *
  * 样例类
  * scala再编译的过程中会自动再类上增加 常用的方法
  *
  */
case class Studnet2(name: String, age: Int, clazz: String)


2.伴生对象

package com.shujia.scala

object Demo8Apply {
  def main(args: Array[String]): Unit = {
    Student3.print()
    Student3.apply("java")

    //通过伴生对象,去掉new关键字
    val student: Student3 = Student3("张三")

    println(student)

  }
}

class Student3(name: String) {

  var _name: String = name


  override def toString = s"Student3(${_name})"
}


/**
  * object中的方法都是静态方法,可以直接通过类名调用
  * apply 是一个特殊的方法,可以直接通过类名 加() 调用
  *
  */

// 伴生对象
object Student3 {

  def print(): Unit = {
    println("student3")
  }

  def apply(name: String): Student3 = new Student3(name)
}

3.函数

package com.shujia.scala

object DemoFun1 {
  def main(args: Array[String]): Unit = {
    /**
      * scala中的函数,可以独立存在,可以在任何位置定义函数
      */

    /**
      * * def:定义函数的关键字
      * * add:函数名
      * * x y 函数的参数
      * * Int:函数返回值类型
      * * =:后面是函数的内容
      */
    def add(x:Int,y:Int): Int ={
      val s =x+y
      return s
    }

    //调用函数
    //函数调用不需要依赖对象
    val s:Int = add(10,20)
    println(s)

    /**
      *
      * 函数的简写
      *1.return可以省略,将返回值放在最后一行即可
      * 2.如果函数只有一行代码,{}可以省略
      * 3.返回值类型可以不写,scala会自动推断
      * 4.如果参数列表为空,()可以省略
      *
      *
      */

    def add1(x:Int,y:Int)=x+y

    def print()=println("scala")

    println(add1(10,20))
    print()

    /**
      * lambda表达式,或者叫匿名对象
      * =>:前面是函数的参数
      * =>;后面是函数的内容和返回值
      *
      */

    val add2=(x:Int,y:Int) => x+y
    val add3=(a:Int,b:Int) => a+b
    println(add2(10,20))
  }

}

package com.shujia.scala

object DemoFun2 {
  def main(args: Array[String]): Unit = {

    /**
      * 在面向对象编程中,对象是基本的单位,将对象传来传去(对象是有类型的)
      *
      * 在面向函数编程中,函数是基本的单位,将函数传来传去(函数是不是应该也有类型)
      *
      * 函数的类型:由参数类型和返回值类型决定
      *
      * def fun(s:String):String = s
      *
      * fun是一个餐数为String,返回值为String类型的函数 String => String
      *
      * 函数式编程(高阶函数)
      *
      * 1.函数作为参数
      * 2.以函数作为返回值(可以忽略)
      *
      */

    /**
      * 1.以函数作为参数
      * fun:的参数是一个函数,(是一个参数为String 返回值为String的函数)
      *
      */

    def fun(f:String => String): Unit ={
      //可以读进来的函数进行调用
      val str:String = f("java")
      println(str)
    }


    //参数为String 返回值为String
    def fun2(s:String): String ={
      "fun2"+s
    }

    def fun3(s:String):String={
      "fun3"+s
    }

    //调用函数的时候传入一个函数
    //以函数作为参数
    fun(fun2)
    //可以进行多次调用,传入不同的函数
    fun(fun3)

    /**
      * 通过匿名函数简化函数的调用
      *
      * (s:String) => "匿名函数:"+s
      */

    fun((s:String) => "匿名函数:"+s)
  }
}

package com.shujia.scala

import scala.collection.mutable.ListBuffer

object DemoFun3 {
  def main(args: Array[String]): Unit = {
    /**
      * 函数的应用
      */
    
    val list: List[Int] = List(1,2,3,4,5,6,7)

    val rdd = new RDD(list)
    rdd.print()

    rdd.add(1)
    rdd.print()

    rdd.inc(2)
    rdd.print()

    rdd.map((value:Int) => value-100)
    rdd.map((value:Int) =>value+100)
    rdd.map((value:Int) => value*100)
    rdd.map((value:Int) => value/100)

    /**
      * 面向接口编程
      * 将不一样的逻辑抽象成接口(java)或者(scala),让用户自定义逻辑
      *
      */

    val list2:List[Int] = List(1,2,3,4,5,6,7)
    val rdd2 = new RDD(list2)

    /**
      * 对集合中的元素做什么操作完全由用户自定义
      */

    rdd2.map((value:Int) => value+1)
    rdd2.map((value:Int) => value/2)

    //简写,类型推断
    rdd2.map(value => value+2)
    rdd2.print()

    /**
      * 在集合中已经自带了map函数
      */
    val ints = List(1,2,3,4,5,6,7,8)

    val ints2: List[Int] = ints.map(i => i+1)

    println(ints2)
  }
}

class RDD(list: List[Int]){
  //list是一个不可变的集合
  var _list:List[Int] = list
  
  //对集合进行相加
  def add(k:Int): Unit ={
    //ListBuffer 可变函数
    val listBuffer: ListBuffer[Int] = new ListBuffer[Int]
    
    var i = 0
    while (i<_list.size){
      val value: Int = _list(i)

      //scala中. 和 () 都可省略
      //listBuffer.+=(value+k)

      //插入元素
      listBuffer += value + k

      i=i+1
    }
    _list=listBuffer.toList
  }
  
  
  //对集合进行相减
  def inc(k:Int): Unit ={
    //ListBuffer 可变集合
    val listBuffer = new ListBuffer[Int]
    
    var i = 0
    while (i<_list.size){
      val value = _list(i)
      
      listBuffer += value-k
      
      i=i+1
    }
    _list=listBuffer.toList
  }

  //可以对集合中的元素进行任何操作
  def map(f:Int => Int): Unit ={

    //ListBuffer 可变集合
    val listBuffer = new ListBuffer[Int]
    
    var i=0
    while (i<_list.size){
      val value: Int = _list(i)

      listBuffer += f(value)

      i=i+1
    }
    _list=listBuffer.toList
  }
  def print(): Unit ={
    println(_list)
  }
}

4.隐式转换

package com.shujia.scala

object DemoImplicit {
  def main(args: Array[String]): Unit = {
    val str: String = "100"

    val int: Int = str.toInt

    /**
      * 隐式转换
      *动态给类增加新的方法
      *
      * 将对象的类型转换成其他类型
      */

    //1.隐式转换方法

    /**
      *隐式转换
      * 1.和方法名无关
      * 2.和方法的参数类型返回值类型有关,同一个作用域中不能有两个参数类型
      */

    implicit def inToString(i:Int): String ={
      println("隐式转换方法被调用")
      i.toString
    }

    implicit def doubleToString(i:Double): String ={
      println("隐式转换方法被调用")
      i.toString
    }

    def print(s:String): Unit ={
      println(s)
    }

    print("java")

    //显示转换
    //println(1.toString)

    //方法在调用的时候会在当前作用域中找到一个类型可以匹配上隐式转换
    print(100)
    print(3.14)

    //结果相当于显示的调用类型转换方法
    print(inToString(100))

    //在当前作用域中有一个int ---> String的方法,所以所有的int类型都有String类型的方法
    val i:Int = 100
    println(i.startsWith("1"))

  }
}

package com.shujia.scala

import com.shujia.scala.a.Read

import scala.io.Source

object DemoImplicit2 {
  def main(args: Array[String]): Unit = {
    val path: String = "scala/data/students.txt"

//    val read = new Read(path)
//    val lines:List[String] =read.read()

    /**
      * String类型会被隐式转换成Read
      *
      */

    val strings: List[String] = path.read()
    println(strings)

  }
}

object a{
  /**
    * 隐式转换类
    *
    * 所有的String类型都会被隐式转换成Read类型
    */

  implicit class Read(path:String){
    println("Read的默认构造函数")

    def read(): List[String] ={
      val lines: List[String] = Source.fromFile(path).getLines().toList
      lines
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值