To look at Scala from an object-oriented perspective, Scala can be said to be a purely object-oriented programming language, because everything in Scala can be viewed as an object. This is different from Java, which distinguishes between basic data types and reference types. Numeric objects
5210000 + 1 *1024/1
This is the basic operation in Java, but Scala thinks everything is an object, so each number can be treated as an object and can be written in the following form:
(5210000). + ((1). * (1024))./(1))
Function object
This is also a great highlight of Scala's functional programming. Functions can be passed as arguments, can be stored with variables, and functions can be used to return a function.
Object Functiondemo extends App {
def printresult (fun: (int, int) => Int): unit = println (Fun (4, 5))
def add (x: int, y:int): int = x + y
printresult (Add (_: int, _: int)
}
Two functions are defined in the code, the Printresult function, the Add function. The parameter of the Printresult function is to accept a function and return to unit. The Add function has an argument of two int types and returns an int type.
The last line of code function calls, printing out the sum result.
anonymous functions
In the above example, the Add function was invoked only once, in which case we could simply call out the first corresponding function in the form of a single, but not just to define it, in Scala we call it an anonymous function (anonymous functions, a popular explanation: a function without a name). )
The code following the use of anonymous functions is as follows:
Object Functiondemo extends App {
def printresult (fun: (int, int) => Int): unit = println (Fun (4, 5))
Printresul T ((x, y) => x + y)
}
(x, y) => x + y is an anonymous function where the type can be inferred automatically. If you define an anonymous parameter individually or you need to specify a parameter type.
Such as:
(X:int,y:int) =>x+y
=> to the left is the function argument list,=> the right side is the function body.