先看一个这样的案例,【案例一】:
package cn.kotlin.kotlin_base05 fun showAction1(country: String, volk: String) { println("我的祖国是${country}, 我的民族是$volk") } fun showAction2(country: String, volk: String) { println("我的祖国是${country}, 我的民族是$volk") } fun showAction3(country: String, volk: String) { println("我的祖国是${country}, 我的民族是$volk") } fun showAction4(country: String, volk: String) { println("我的祖国是${country}, 我的民族是$volk") } /** * main测试方法 */ fun main(args: Array<String>) { showAction1("中国", "汉族") showAction2("中国", "藏族") showAction3("中国", "龙族") showAction4("中国", "大族") }
执行结果:
具名参数的案例【案例二】
package cn.kotlin.kotlin_base05 /** * 既然祖国都是中国,就定义常量 */ val COUNTRY = "中国" /** * country: String = COUNTRY 可以给country变量设置默认参数 */ fun showAction1(country: String = COUNTRY, volk: String) { println("我的祖国是${country}, 我的民族是$volk") } /** * country: String = COUNTRY 可以给country变量设置默认参数 */ fun showAction2(country: String = COUNTRY, volk: String) { println("我的祖国是${country}, 我的民族是$volk") } /** * country: String = COUNTRY 可以给country变量设置默认参数 */ fun showAction3(country: String = COUNTRY, volk: String) { println("我的祖国是${country}, 我的民族是$volk") } /** * country: String = COUNTRY 可以给country变量设置默认参数 */ fun showAction4(country: String = COUNTRY, volk: String) { println("我的祖国是${country}, 我的民族是$volk") } /** * main测试方法 具名参数 */ fun main(args: Array<String>) { /** * 既然设置了 参数一 为 = COUNTRY * 参数一 参数二 * fun showAction1(country: String = COUNTRY, volk: String) * * 可以给参数一设置参数,也不给参数一设置参数 * * 不给参数一 设置参数, showAction1(参数二的名称 = 参数二) */ showAction1(volk = "汉族") showAction2(volk = "藏族") showAction3(volk = "龙族") showAction4(volk = "大族") }
执行结果: