scala编程语言将函数式编程和面向对象编程完美地融合在一起了。
- 面向对象编程
分解对象,行为,属性,然后通过对象的关系以及行为的调用来解决问题- 函数式编程
将问题分解成一个一个的步骤,将每个步骤进行封装(函数),通过调用这些封装好的功能按照指定的步骤,解决问题。
[修饰符] def 函数名 ( 参数列表 ) [:返回值类型] = {
函数体
}
private def test( s : String ) : Unit = {
println(s)
}
方法
与函数
两个不同的概念,二者在语义上的区别很小。scala 方法是类的一部分,而函数是一个对象,可以赋值给一个变量。换句话来说在类中定义的函数即是方法。scala 中的方法跟 Java 的类似,方法是组成类的一部分。scala 中的函数则是一个完整的对象。object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun1(): Unit = {
println("函数体")
}
fun1()
}
}
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun2(): String = {
"zhangsan"
}
println( fun2() )
}
}
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun3( name:String ): Unit = {
println( name )
}
fun3("zhangsan")
}
}
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun4(name:String): String = {
"Hello " + name
}
println( fun4("zhangsan") )
}
}
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun5(hello:String, name:String): Unit = {
println( hello + " " + name )
}
fun5("Hello", "zhangsan")
}
}
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun6(hello:String, name:String): String = {
hello + " " + name
}
println( fun6("Hello", "zhangsan"))
}
}
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun7(names:String*): Unit = {
println(names)
}
fun7()
fun7( "zhangsan" )
fun7( "zhangsan", "lisi" )
}
}
可变参数不能放置在参数列表的前面,一般放置在参数列表的最后
oobject ScalaFunction {
def main(args: Array[String]): Unit = {
// Error
//def fun77(names:String*, name:String): Unit = {
//}
def fun777( name:String, names:String* ): Unit = {
println( name )
println( names )
}
}
}
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun8( name:String, password:String = "000000" ): Unit = {
println( name + "," + password )
}
fun8("zhangsan", "123123")
fun8("zhangsan")
}
}
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun9( password:String = "000000", name:String ): Unit = {
println( name + "," + password )
}
fun9("123123", "zhangsan" )
fun9(name="zhangsan")
}
}
所谓的至简原则,其实就是Scala的作者为了开发人员能够大幅度提高开发效率。通过编译器的动态判定功能,帮助我们将函数声明中能简化的地方全部都进行了简化。也就是说将函数声明中那些能省的地方全部都省掉。所以这里的至简原则,简单来说就是:能省则省。
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun1(): String = {
return "zhangsan"
}
def fun11(): String = {
"zhangsan"
}
}
}
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun2(): String = "zhangsan"
}
}
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun3() = "zhangsan"
}
}
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun4 = "zhangsan"
fun4// OK
fun4()//(ERROR)
}
}
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun5(): String = {
return "zhangsan"
}
println(fun5())
}
}
如果函数体返回值类型明确为Unit, 那么函数体中即使有return关键字也不起作用
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun5(): Unit = {
return "zhangsan"
}
println(fun5())
}
}
如果函数体返回值类型声明为Unit, 但是又想省略,那么此时就必须连同等号一起省略
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun5() {
return "zhangsan"
}
println(fun5())
}
}
object ScalaFunction {
def main(args: Array[String]): Unit = {
() => {
println("zhangsan")
}
}
}
所谓的高阶函数,其实就是将函数当成一个类型来使用,而不是当成特定的语法结构。
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun1(): String = {
"zhangsan"
}
val a = fun1
val b = fun1 _
val c : ()=>Unit = fun1
println(a)
println(b)
}
}
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun2( i:Int ): Int = {
i * 2
}
def fun22( f : Int => Int ): Int = {
f(10)
}
println(fun22(fun2))
}
}
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun3( i:Int ): Int = {
i * 2
}
def fun33( ) = {
fun3 _
}
println(fun33()(10))
}
}
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun4( f:Int => Int ): Int = {
f(10)
}
println(fun4((x:Int)=>{x * 20}))
println(fun4((x)=>{x * 20}))
println(fun4((x)=>x * 20))
println(fun4(x=>x * 20))
println(fun4(_ * 20))
}
}
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun5() = {
val i = 20
def fun55() = {
i * 2
}
fun55 _
}
fun5()()
}
}
参考:https://www.jianshu.com/p/8f24150fad2a
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun7(op: => Unit) = {
op
}
fun7{
println("xx")
}
}
}
参考:https://blog.csdn.net/qq_38038143/article/details/89172749
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun6(i:Int)(j:Int) = {
i * j
}
}
}
参考:https://blog.csdn.net/bitcarmanlee/article/details/77994633
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun8(j:Int):Int = {
if ( j <= 1 ) {
1
} else {
j * fun8(j-1)
}
}
println(fun8(5))
}
}
当函数返回值被声明为lazy时,函数的执行将被推迟,直到我们首次对此取值,该函数才会执行。这种函数我们称之为惰性函数。
object ScalaFunction {
def main(args: Array[String]): Unit = {
def fun9(): String = {
println("function...")
"zhangsan"
}
lazy val a = fun9()
println("----------")
println(a)
}
}