详解 Scala 的函数式编程

一、函数基础

函数式是基于数学理论的函数概念,类似于 y = f(x)

1. 函数定义

1.1 语法
/*  函数结构:
    def funcName(param1 : type1, param2 : type2,...): type = { statement }
 */
def sum(x : Int, y : Int): Int = {
   
    return x + y
}
1.2 案例
object TestFunctionDefine {
   
    def main(args: Array[String]): Unit = {
   
        
        //  函数 1:无参,无返回值
        def test1(): Unit = {
   
        	println("1:无参,无返回值")
        }
        test1()
        
        //  函数 2:无参,有返回值
        def test2(): String = {
   
        	return "2:无参,有返回值"
        }
        println(test2())
        
        //  函数 3:有参,无返回值
        def test3(s: String): Unit = {
   
        	println("3:有参,无返回值" + s)
        }
        test3("Hi")
        
        //  函数 4:有参,有返回值
        def test4(s: String): String = {
   
        	return s + "4:有参,有返回值"
        }
        println(test4("hello "))
        
        //  函数 5:多参,无返回值
        def test5(name: String, age: Int): Unit = {
   
        	println(s"${
     name}今年${
     age}岁")
        }
        test5("阿豪", 18)
        
        //  函数 6:多参,有返回值
        def test6(a: Int, b: Int): Int = {
   
            return a + b
        }
        println(test6(10, 20))
        
    }
}

2. 函数VS方法

  • 概念:

    • 函数:为完成某一功能的程序语句的集合 (区别于函数式编程的函数)
    • 方法:类中的函数
    object TestFunctionAndMethod {
         
        def main(args: Array[String]): Unit = {
         
            // 定义函数
            def sayHi(name : String): Unit = {
         
                println("hi," + name)
            }
            
            // 函数调用
            sayHi("张三")
            
            // 方法调用
            TestFunctionAndMethod.sayHi("李四")
            
            // 获取方法返回值
            val result = TestFunctionAndMethod.sayHello("王五")
            println(result)
            
        }
        
        // 定义方法
        def sayHi(name : String): Unit = {
         
            println("Hi," + name)
        }
        
        // 定义有返回值的方法
        def sayHello(name : String): String = {
         
            return "Hello," + name
        }
    }
    
  • 语法:

    • Scala 语言可以在任何的语法结构中声明任何的语法
    • 函数没有重载和重写的概念;方法可以进行重载和重写
    • Scala 中函数可以嵌套定义
    object TestFunctionAndMethod {
         
        // 方法可以进行重载和重写,程序可以执行
        def main(): Unit = {
         
            
        }
        
        def main(args: Array[String]): Unit = {
         
            // Scala 语言可以在任何的语法结构中声明任何的语法
            import java.util.Date
            new Date()
            
            // 函数没有重载和重写的概念,程序会报错
            def test(): Unit = {
         
            	println("无参,无返回值")
            }
            test()
            def test(name:String): Unit = {
         
            	println()
            }
            
            // Scala 中函数可以嵌套定义
            def test2(): Unit = {
         
                def test3(name:String): Unit = {
         
                	println("函数可以嵌套定义")
                }
            }
        }
    }
    

3. 函数参数

object TestFunctionParam {
   
    def main(args: Array[String]): Unit = {
   
        // 1.可变参数,且可变参数一般放在参数列表的最后
        def f1(s: String*): Unit = {
    // 可变参数底层是 ArrayBuffer[String]
            println(s)
        }
        f1() // WrappedArray()
        f1("tom") // WrappedArray(tom)
        f1("tom", "jerry") // WrappedArray(tom, jerry)
        
        def f2(s1: String, s2: String*): Unit = {
   
            println("s1:" + s1 + ", s2:" + s2)
        }
        f2("jack")
        f2("jack", "rose", "james")
        
        // 2.参数默认值,有默认值的参数放

你可能感兴趣的:(Scala,scala,开发语言,后端,大数据)