8.15. Scala method and function

发布时间 : 2025-10-25 13:35:13 UTC      

Page Views: 9 views

Scala has methods and functions, and there is little semantic difference between the two. The Scala method is part of a class, and a function is an object that can be assigned to a variable. In other words, a function defined in a class is a method.

The methods in Scala are similar to those in Java, and they are part of the constituent class.

The function in Scala is a complete object, and the function in Scala is actually the object that inherits the class of Trait.

Used in Scala val Statement can define a function def statement defines the method.

class Test{ def m(x: Int) = x + 3 val f = (x: Int) => x + 3 } 

Note: there is no difference between function and method in some translations.

8.15.1. Method declaration #

The format of the Scala method declaration is as follows:

def functionName ([parameter list]) : [return type] 

If you don’t write the equals sign and the method body, the method is implicitly declared as abstract, and the type that contains it is also an abstract type.

8.15.2. Method definition #

The method definition is defined by a def keyword, followed by an optional list of arguments, a colon: and the return type of the method, an equal sign =, and finally the body of the method.

The definition format of the Scala method is as follows:

def functionName ([parameter list]) : [return type] = { function body return [expr] } 

In the above code return type can be any legal Scala data type. Parameters in the parameter list can be separated by commas.

The function of the following method is to add and sum the two incoming parameters:

Example #

object add{ def addInt( a:Int, b:Int ) : Int = { var sum:Int = 0 sum = a + b return sum } } 

If the method does not return a value, it can be returned as Unit , this is similar to that of Java void the example is as follows:

Example #

object Hello{ def printMe( ) : Unit = { println("Hello, Scala!") } } 

8.15.3. Method call #

Scala provides several different method invocation methods:

The following is the standard format for calling methods:

functionName( parameter list ) 

If the method is called using the object of the instance, we can use a format similar to java (using the . Number):

[instance.]functionName( parameter list ) 

The above example shows an example of defining and invoking methods:

Example #

object Test { def main(args: Array[String]) { println( "Returned Value : " + addInt(5,7) ); } def addInt( a:Int, b:Int ) : Int = { var sum:Int = 0 sum = a + b return sum } } 

Execute the above code, and the output is as follows:

$ scalac Test.scala $ scala Test Returned Value : 12 

Scala is also a functional language, so functions are the core of the Scala language. The following function concepts help us better understand Scala programming:

A case study of function concept Analysis

Function Call-by-Name

Specify the function parameter name

Function-variable argument

Recursive function

Default parameter value

Higher order function

Embedded function

Anonymous function

Partial application function

Function Currying

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。