1, Characteristics of Scala programming
Scala is developed based on Java, also has object-oriented characteristics, and is more object-oriented than Java. At the same time, it also draws lessons from some characteristics of functional programming language. In Scala, functional programming and object-oriented programming are perfectly integrated.
1. Object oriented programming
Solve the problem, decompose the object, behavior and attribute, and then solve the problem through the relationship of the object and the call of behavior.
Objects: users
Behavior: login, connect to JDBC, read database
Attributes: user name, password
Scala is a completely object-oriented programming language. Everything is object
The nature of objects: an encapsulation of data and behavior
2. Functional programming
When solving a problem, divide the problem into steps one by one, encapsulate each step (function), and solve the problem by calling these encapsulated steps.
For example: request - > user name, password - > connect to JDBC - > read database
Scala is a fully functional programming language. Everything is a function.
The essence of function: a function can be passed as a value
2, Fundamentals of functional programming
1. Basic function syntax
2. Differences between functions and methods
(1) Core concept
Function: a collection of program statements that complete a function, called a function.
Methods: functions in a class are called methods.
(2) Differences between functions and methods
1.Scala language can declare functions inside functions. In order to distinguish, functions in classes are generally called methods, which are functions in a broad sense, and functions in methods are called functions in a narrow sense
2. The function has no concept of overloading and rewriting; Methods can be overloaded and overridden
3. Function parameters
(1) Variable parameter, parameter type followed by * indicates variable parameter
(2) If there are multiple parameters in the parameter list, variable parameters are generally placed last
(3) Parameter default value. Generally, parameters with default values are placed behind the parameter list
(4) Named parameters. When calling a function, the named parameters are directly used for assignment, so there is no need to pass the parameters in order
object TestFunction { def main(args: Array[String]): Unit = { // (1) The * sign indicates a variable parameter def test( s : String* ): Unit = { println(s) } // There are input parameters: output Array test("Hello", "Scala") // No input parameters: output List() test() // (2) If there are multiple parameters in the parameter list, variable parameters are generally placed last def test2( name : String, s: String* ): Unit = { println(name + "," + s) } test2("jinlian", "dalang") // (3) Parameter defaults def test3( name : String, age : Int = 30 ): Unit = { println(s"$name, $age") } // If the parameter passes a value, the default value is overridden test3("jinlian", 20) // If the parameter has a default value, you can omit this parameter when calling test3("dalang") // Generally, parameters with default values are placed after the parameter list def test4( sex : String = "male", name : String ): Unit = { println(s"$name, $sex") } // In Scala functions, parameters are passed from left to right //test4("wusong") //(4) Named parameter test4(name="ximenqing") } }
4. Principle of simplifying functions (key points)
// Function simplification principle object Test04_Simplify { def main(args: Array[String]): Unit = { def f0(name: String): String = { return name } println(f0("atguigu")) println("==========================") //(1)return can be omitted. Scala will use the last line of the function body as the return value def f1(name: String): String = { name } println(f1("atguigu")) println("==========================") //(2) If the function body has only one line of code, you can omit curly braces def f2(name: String): String = name println(f2("atguigu")) println("==========================") //(3) If the return value type can be inferred, it can be omitted (: omitted together with the return value type) def f3(name: String) = name println(f3("atguigu")) println("==========================") //(4) If there is a return, the return value type cannot be omitted and must be specified // def f4(name: String) = { // return name // } // // println(f4("atguigu")) println("==========================") //(5) If the function explicitly declares unit, even if the return keyword is used in the function body, it will not work def f5(name: String): Unit = { return name } println(f5("atguigu")) println("==========================") //(6)Scala can omit the equal sign if it expects a type with no return value def f6(name: String) { println(name) } println(f6("atguigu")) println("==========================") //(7) If the function has no parameters but declares a parameter list, parentheses can be added or not when calling def f7(): Unit = { println("atguigu") } f7() f7 println("==========================") //(8) If the function does not have a parameter list, the parentheses can be omitted and must be omitted when calling def f8: Unit = { println("atguigu") } // f8() f8 println("==========================") //(9) If you don't care about the name and only care about logical processing, the function name (def) can be omitted def f9(name: String): Unit = { println(name) } // Anonymous function, lambda expression (name: String) => { println(name) } println("==========================") } }