scala形参数组参数_Scala中的重复方法参数

scala形参数组参数

Scala重复方法参数 (Scala Repeated Method Parameters)

The repeated parameter is the concept of defining parameters in which parameters of the same data type can be passed to a method n number of times, where n belongs to 0 to infinity i.e. method can accept any parameters.

重复参数是定义参数的概念,在该参数中,相同数据类型的参数可以传递给方法n次,其中n属于0到无穷大,即方法可以接受任何参数。

Scala programming language supports repeated method parameter, which is of great help when the number of parameters passed is not defined at compile time. Using repeated method parameter, the program can accept an unlimited number of parameters.

Scala编程语言支持重复的方法parameter ,这在编译时未定义传递的参数数量时非常有用。 使用重复的方法参数,程序可以接受无限数量的参数。

重复方法参数的属性 (Properties of repeated method parameter)

  • There can be only one repeated parameter allowed in a method.

    方法中只能有一个重复的参数。

  • The data type of all repeated parameters is the same and is defined at once with the same variable name.

    所有重复参数的数据类型是相同的,并使用相同的变量名称立即定义。

  • The repeated method parameter should always be the last defined parameter of the method.

    重复的方法参数应始终是方法的最后定义的参数。

Example 1:

范例1:

This program displays the property of the program that repeated method parameter should be only single and data.

该程序显示该程序的属性,即重复的方法参数应仅为单个和数据。

object MyClass {
    def adder(x:Int*){
        var sum = x.fold(0)(_+_) 
        printf("The sum is " + sum)
    }
    
    def main(args: Array[String]) {
        adder(1,5,6,67,8,2)
    }
}

Output

输出量

The sum is 89

Example 2:

范例2:

Property displayed in the program is that if there are multiple parameters passed to a method, then the last one should be repeated parameter passed to the method.

程序中显示的属性是,如果有多个参数传递给方法,则最后一个参数应该是重复的参数传递给方法。

object MyClass {
    def salary(str:String , x:Int*){
       var sal = x.product
       printf("The salary of the intern at " + str + " is " + sal)
    }
    
    def main(args: Array[String]) {
        salary("Include Help",90, 100)
    }
}

Output

输出量

The salary of the intern at Include Help is 9000


翻译自: https://www.includehelp.com/scala/repeated-method-parameters.aspx

scala形参数组参数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值