kotlin方法重载_Kotlin程序| 方法重载的例子

kotlin方法重载

方法重载 (Method Overloading)

Function overloading or method overloading is the ability to create multiple functions of the same name with different implementations.

函数重载或方法重载是使用不同的实现创建具有相同名称的多个函数的能力。

Kotlin中方法重载的程序 (Program for method overloading in Kotlin)

package com.includehelp

//Declare class
class Operation{
    //Member function with two Int type Argument
    fun sum(a:Int, b:Int){
        println("Sum($a,$b) = ${a+b}")
    }

    //Overloaded Member function 
    //with three Int type Argument
    fun sum(a:Int, b:Int,c:Int){
        println("Sum($a,$b,$c) = ${a+b+c}")
    }

    //Overloaded Member function with 
    //four Int type Argument
    fun sum(a:Int, b:Int,c:Int,d:Int){
        println("Sum($a,$b,$c,$d) = ${a+b+c+d}")
    }
}

//Main function, entry Point of Program
fun main(args:Array<String>){
    //Create Instance of Operation class
    val operation = Operation()

    //Called function with two arguments
    operation.sum(10,20)

    //Called function with three arguments
    operation.sum(a=2,b=3,c=5)

    //Called function with four arguments
    operation.sum(5,8,2,12)
}

Output:

输出:

Sum(10,20) = 30
Sum(2,3,5) = 10
Sum(5,8,2,12) = 27


翻译自: https://www.includehelp.com/kotlin/example-of-method-overloading.aspx

kotlin方法重载

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值