本文共 1641 字,大约阅读时间需要 5 分钟。
scala中命名参数函数
A function is Scala can take multiple arguments. These arguments are traditionally called in sequence while calling a function. But in Scala programming, the program is given the power to alter the traditional sequence of arguments. Scala provides its users named arguments these are used to change the order of using arguments at call.
一个函数是Scala可以接受多个参数。 这些参数通常在调用函数时按顺序调用。 但是在Scala编程中,程序有权更改传统的参数顺序。 Scala为用户提供了命名参数,这些参数用于更改调用时使用参数的顺序。
Suppose a function that has two variables var1 and var2. If we want to initialize var2 first then the following syntax is used.
假设一个函数具有两个变量var1和var2 。 如果我们要首先初始化var2,则使用以下语法。
Syntax:
句法:
functionName ( var2 = value2, var2 = value1 );
Explanation:
说明:
This will pass the value2 to the second argument in the list. And value1 in the first argument in the list.
这会将value2传递给列表中的第二个参数。 并在列表中的第一个参数中使用value1。
Example:
例:
object Demo { def sub( a:Int, b:Int ) = { println("Substraction = " + (a-b) ); } def main(args: Array[String]) { println("The fucntion is called using named function call") sub(b = 5, a = 7); } }
Output
输出量
The fucntion is called using named function callSubstraction = 2
Explanation:
说明:
This code displays how to use named arguments in Scala? The code initializes a function named sub(), it expects two arguments and substracts second from first. At function call, the arguments are filled using the names that initialize them in the order the programmer wants.
此代码显示如何在Scala中使用命名参数? 该代码初始化了一个名为sub()的函数,它需要两个参数并从第一个减去第二个。 在函数调用时,将使用按程序员想要的顺序对其进行初始化的名称来填充参数。
翻译自:
scala中命名参数函数
转载地址:http://drxzd.baihongyu.com/