zoukankan      html  css  js  c++  java
  • Android-Kotlin-具名参数

    先看一个这样的案例,【案例一】:

    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 = "大族")
    
    }

    执行结果:

     

  • 相关阅读:
    如何获取下拉框中的值
    如何建立主从服务器
    hibernate
    Python基础(一)
    python2 与 python3 区别
    canvas简述(二)游戏实战思路
    canvas简述(一)
    C简述(二)
    C语言的基本简述
    Js基础(三) WebAPI
  • 原文地址:https://www.cnblogs.com/android-deli/p/10319069.html
Copyright © 2011-2022 走看看