zoukankan      html  css  js  c++  java
  • Scala高手实战****第18课:Scala偏函数、异常、Lazy值编码实战及Spark源码鉴赏

    本篇文章主要讲述Scala函数式编程之偏函数,异常,及Lazy

    第一部分:偏函数

    偏函数:当函数有多个参数,而在使用该函数时不想提供所有参数(比如函数有3个参数),只提供0~2个参数,此时得到的函数便是偏函数。

      */
    object C18 {
    
      def main(args: Array[String]): Unit = {
    
        val sample=1 to 10
        val isEven:PartialFunction[Integer,String]={
          case x if x%2==0=>x+" is even"
          case _=>"other"
        }
        val isOdd:PartialFunction[Integer,String]={
          case x if x%2==1=>x+" is odd"
        }
    
        isEven(4)
    
        lazy val x=3
        println(x)
    
      }
    
    }
    

    被lazy修饰的只有在被调用的时候才会实例化

    object HelloExceptionAndLazyValue {
      def main(args: Array[String]): Unit = {
        try{
            1/0
        }catch {
          case ioException:IOException => println("IOException:" + ioException.toString())
          case illegalArgs:IllegalArgumentException => println("IllegalArgumentException:" + illegalArgs.toString())
          case arithmeticInstruction:ArithmeticInstruction =>("ArithmeticException"+arithmeticInstruction.toString())
        }  finally{
    
        }
    //    val score = 100
        lazy val score = 100
        println("......"+score)
        println("......")
      }
    }
    

      

      

  • 相关阅读:
    http协议
    web应用
    前端基础-jquery
    jQuery的事件
    2.UML类图基本介绍
    1.设计模式的七大原则
    使用OpenFeign远程调用时请求头处理报错问题
    SpringCloud Config-分布式配置中心
    19. 类加载器详解
    18. 类加载过程详解
  • 原文地址:https://www.cnblogs.com/sunrunzhi/p/10000095.html
Copyright © 2011-2022 走看看