zoukankan      html  css  js  c++  java
  • 函数

    package com.bjsxt.scala
    
    import java.util.Date
    
    object Lesson_fun {
      def main(args: Array[String]): Unit = {
        /**
         * 1.方法定义
         * 	1.方法定义使用“def”
         * 	2.方法可以传参,参数类型不能省略,方法的计算结果类型可以省略,会自动推断。
         * 	3.方法名和方法体“{...}”要有等号隔开,也可以不写“=”,不写“=”无论方法体最后一行计算结果是什么,都会被丢弃,返回Unit.
         * 	4.方法体中的返回值可以使用return返回,如果写“return”,方法体的返回类型不能省略。
         * 		也可以不使用return返回,不写return 方法体默认将最后一行计算的结果返回,建议不写return。
         * 	5.方法体中如果一行可以搞定,方法体的“{..}”可以省略不写。
         */
    //    def max(x:Int,y:Int) ={
    //      if(x>y){
    //         x
    //      }else{
    //         y
    //      }
    //    }
    //    def max(x:Int,y:Int) = if(x>y)  x else y
    //    
    //    println(max(1,2))
        
        /**
         * 2.递归函数
         *  递归方法要显式声明方法的返回类型
         * 
         */
    //    def fun(num:Int):Int = {
    //      if(num==1){
    //        num
    //      }else{
    //        num*fun(num-1)
    //      }
    //    }
    //    
    //    println(fun(5))
        
        /**
         * 3.参数有默认值的函数
         */
    //    def fun(a:Int=100,b:Int=200) = {
    //      a+b
    //    }
    //    
    //    println(fun(b=3))
        
        /**
         * 4.可变长参数的函数
         */
    //    def fun(s:String*) = {
    //      s.foreach((str:String)=>{
    //        println(str)
    //      })
    //    }
    //    def fun(ss:String*) = {
    //    		ss.foreach(println)
    //    }
    //    
    //    fun("abc","hello","world","yyy")
        
        /**
         * 5.匿名函数
         */
    //    val f = ()=>{
    //      println("hello world~")
    //    }
        
    //    val f = (a:Int,b:Int)=>{
    //      a+b
    //    }
    //   println(f(100,200))
        
        
        /**
         * 6.偏应用函数
         */
    //    def showLog(date:Date,log:String) = {
    //      println("date is "+date +" ,log is "+log)
    //    }
    //    val date = new Date()
    //    showLog(date,"a")
    //    showLog(date,"b")
    //    showLog(date,"c")
    //    
    //    val fun = showLog(date,_:String)
    //    fun("aaa")
    //    fun("bbb")
    //    fun("ccc")
        
        /**
         * 7.嵌套函数
         */
    //    def fun(i:Int)={
    //      
    //      def fun1(a:Int):Int={
    //        if(a==1){
    //          1
    //        }else{
    //          a*fun1(a-1)
    //        }
    //      }
    //      
    //      fun1(i)
    //      
    //    }
    //    
    //    println(fun(5))
        
        /**
         * 8.高阶函数
         * 	函数的参数是函数
         *  函数的返回是函数
         *  函数的参数和返回都是函数
         */
        //函数的参数是函数
        
    //    def fun1(f:(Int,Int)=>Int,c:String) = {
    //      val result = f(100,200)
    //      result+"~"+c
    //    }
    //    val end = fun1((a:Int,b:Int)=>{
    //      a*b
    //    },"hello")
    //    
    //    println(end)
        
        //函数的返回是函数 -----要显示的声明函数的返回类型
    //     def fun(a:Int,b:Int) :(Int,Int)=>Int= {
    //       val result = a+b
    //       def fun1(i:Int,j:Int):Int={
    //         i*j+result
    //       }
    //       fun1
    //     }
    //    
    //    println(fun(1,2)(100,200))
        
        // 函数的参数和返回都是函数
        
    //    def fun(f:(Int,Int)=>Int,a:Int):(String,String)=>String ={
    //      val result = f(1,2)+a
    //      def fun1(s1:String,s2:String):String = {
    //        s1+"@"+s2+"$"+result
    //      }
    //     fun1 
    //    }
    //    
    //    println(fun((a:Int,b:Int)=>{a*b},100)("hello","world"))//hello@world$102
        
        /**
         * 9.柯里化函数
         *   高阶函数简化版
         */
    //    def fun(a:Int,b:Int)(c:Int,d:Int) = {
    //      a+b+c+d
    //    }
    //    
    //    println(fun(1,2)(3,4))
      }
    }
    

      

  • 相关阅读:
    Windows Azure Platform Introduction (6) Windows Azure应用程序运行环境
    Windows Azure Platform Introduction (2) 云计算的分类和服务层次
    【转载】修改oracle的最大连接数 以及 object is too large to allocate on this o/s
    Windows Azure Platform Introduction (3) 云计算的特点
    Windows Azure Platform Introduction (8) Windows Azure 账户管理
    XML手册地址
    用dataset方式取值
    xml dataset的发布
    虚惊一场
    XML的一些特点
  • 原文地址:https://www.cnblogs.com/huiandong/p/9278196.html
Copyright © 2011-2022 走看看