函数和方法
package com. zishi. scala. a02. okk05 object Test01_FunctionAndMethod { def main( args: Array[ String ] ) : Unit = { def sayHi( name: String ) : Unit = { println( "hi, " + name) } sayHi( "alice" ) Test01_FunctionAndMethod. sayHi( "bob" ) val result = Test01_FunctionAndMethod. sayHello( "cary" ) println( result) } def sayHi( name: String ) : Unit = { println( "Hi, " + name) } def sayHello( name: String ) : String = { println( "Hello, " + name) return "Hello" }
}
函数的定义
package com. zishi. scala. a02. okk05 object Test02_FunctionDefine01 { def main( args: Array[ String ] ) : Unit = { def f1( ) : Unit = { println( "1. 无参,无返回值" ) } f1( ) println( f1( ) ) println( "=========================" ) def f2( ) : Int = { println( "2. 无参,有返回值" ) return 12 } println( f2( ) ) println( "=========================" ) def f3( name: String ) : Unit = { println( "3:有参,无返回值 " + name) } println( f3( "alice" ) ) println( "=========================" ) def f4( name: String ) : String = { println( "4:有参,有返回值 " + name) return "hi, " + name} println( f4( "alice" ) ) println( "=========================" ) def f5( name1: String , name2: String ) : Unit = { println( "5:多参,无返回值" ) println( s " ${ name1 } 和 ${ name2 } 都是我的好朋友" ) } f5( "alice" , "bob" ) println( "=========================" ) def f6( a: Int , b: Int ) : Int = { println( "6:多参,有返回值" ) return a + b} println( f6( 12 , 37 ) ) }
}
函数的可变参数
package com. zishi. scala. a02. okk05 object Test03_FunctionParameter { def main( args: Array[ String ] ) : Unit = { def f1( str: String * ) : Unit = { println( str) } f1( "alice" ) f1( "aaa" , "bbb" , "ccc" ) def f2( str1: String , str2: String * ) : Unit = { println( "str1: " + str1 + ",str2: " + str2) } f2( "alice" ) f2( "aaa" , "bbb" , "ccc" ) def f3( name: String = "eeeee" ) : Unit = { println( "My school is " + name) } f3( "school" ) f3( ) def f4( name: String = "eeeee" , age: Int ) : Unit = { println( s " ${ age } 岁的 ${ name } 在smmmca学习" ) } f4( "alice" , 20 ) f4( age = 23 , name = "bob" ) f4( age = 21 ) }
}
函数的至简原则
package com. zishi. scala. a02. okk05
object Test04_Simplify { def main( args: Array[ String ] ) : Unit = { def f0( name: String ) : String = { return name} println( f0( "erfearfere" ) ) println( "==========================" ) def f1( name: String ) : String = { name} println( f1( "erfearfere" ) ) println( "==========================" ) def f2( name: String ) : String = nameprintln( f2( "erfearfere" ) ) println( "==========================" ) def f3( name: String ) = nameprintln( f3( "erfearfere" ) ) println( "==========================" ) println( "==========================" ) def f5( name: String ) : Unit = { return name} println( f5( "f5 erfearfere" ) ) println( "==========================" ) def f6( name: String ) { println( name) } println( f6( "f6 erfearfere" ) ) println( "==========================" ) def f7( ) : Unit = { println( "f7" ) } f7( ) f7println( "==========================" ) def f8: Unit = { println( "f8 f8" ) } def f82 = println( "f8 f8" ) f82f8println( "==========================" ) def f9( name: String ) : Unit = { println( name) } ( ) => { println( "Hello lambda" ) } ( name: String ) => { println( s "aaa ${ name } " , name) } println( "-------------------" ) }
}
函数也是对象
package com. zishi. scala. a02. okk05
object Test04_Simplify02 { def main( args: Array[ String ] ) : Unit = { def test( ) = { println( "111" ) } testprintln( test) println( test _) val f2: ( ) => Unit = test _println( f2) println( "..........." ) f2( ) def test1( age: Int ) : String = { "hello:" + age} val f3: Function1[ Int , String ] = test1val f4: ( Int ) => String = test1println( f3) println( f4) f4( 4 ) }
}