美文网首页
kotlin函数用法

kotlin函数用法

作者: Bfmall | 来源:发表于2022-10-26 15:04 被阅读0次

一、函数用法

/**
 * DESC   : kotlin中的函数用法
 */
const val TAG = "KtBaseFunTest01"
class KtBaseFunTest01 {

    /**
     * 函数默认为public类型
     * 其实kotlin的函数更规范,先有输入,再有输出
     *
     * 修饰类型   函数标识    函数名          参数                返回值类型
     * private   fun       method1(name: String, age : Int) : Int {
     */
    public fun method1(name: String, age : Int) : Int {
        Log.d(TAG, "result=${name} , age=${age}")
        return 0
    }

    /**
     * 上面的kt method1方法编译后会变成java代码
     */
    /*public static final int method1(String name, int age) {
        Log.d(TAG, "result=$name , age=$age")
        return 0;
    }*/


    /**
     * 函数的默认参数,有默认参数时,可传参也可不传参
     *
     * ktBaseFunTest01.default01("zhangsan", 10)
     * ktBaseFunTest01.default02("lisi")
     * ktBaseFunTest01.default03()
     */
    fun default01(name: String, age : Int) {
        Log.d(TAG, "defaultMethod==>result=${name} , age=${age}")
    }

    fun default02(name: String, age : Int = 30) {
        Log.d(TAG, "defaultMethod==>result=${name} , age=${age}")
    }

    fun default03(name: String = "lanny", age : Int = 23) {
        Log.d(TAG, "defaultMethod==>result=${name} , age=${age}")
    }

    /**
     * 具名函数参数
     *
     * 传入具体的参数
     * ktBaseFunTest01.test01("zhangsna", 10)
     */
    fun test01(name: String, age : Int) {
        Log.d(TAG, "defaultMethod==>result=${name} , age=${age}")
    }


    /**
     * Unit函数特点
     *
     * java中的void是关键字,(无参数返回,忽略类型),
     *
     * kotlin中的Unit是无返回值类型,可写可省略,默认是有的,(忽略类型==Unit类型)
     */
    fun testUnit01() {
        println("hahah")
    }

    fun testUnit02() : Unit {
        println("hahah")
    }

    /**
     * Nothing类型特点: TODO会终止程序
     *
     * 传入参数-1, 终止程序
     * 2022-08-19 14:10:05.650 5808-5808/com.xyaty.kotlinbasedemo E/AndroidRuntime: FATAL EXCEPTION: main
     * Process: com.xyaty.kotlinbasedemo, PID: 5808
     * kotlin.NotImplementedError: An operation is not implemented: 数字不对,为Nothing类型
     */
    fun testNothing01(num : Int) {
        val result = when(num) {
            -1 -> TODO("数字不对,为Nothing类型")
            1 -> "正确"
            0 -> "错误"
            else -> {
                "更离谱"
            }
        }
        Log.d(TAG, "testNothing01==>result="+result)
    }

    interface A {
        fun methodA()
    }

    class B : A {
        override fun methodA() {
            /**
             * 下面这句TODO,不是注释,会终止程序的
             */
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

    }


    /**
     * 测试反引号函数名特点
     *
     * 第一种情况
     * 调用:
     * ktBaseFunTest01.测试反引号函数名特点("zhangsa", 11)
     */
    fun `测试反引号函数名特点`(name : String ,age:Int) {
        Log.d(TAG, "测试反引号函数名特点=>name="+name+", age="+age)
    }

    /**
     * 第二种情况
     *
     * is 和 in 在kotlin中是关键字,如果有这样的方法,无法直接使用,可以加入反引号来进行调用
     *
     * 调用
     * ktBaseFunTest01.`in`("haha", 22)
     */
    fun `in`(name:String, age:Int) {
        Log.d(TAG, "in在kotlin中是关键字,使用反引号调用这样的方法=>name="+name+", age="+age)
    }

    /**
     * ktBaseFunTest01.`is`("hahahah", 21)
     */
    fun `is`(name:String, age:Int) {
        Log.d(TAG, "in在kotlin中是关键字,使用反引号调用这样的方法=>name="+name+", age="+age)
    }
}

相关文章

网友评论

      本文标题:kotlin函数用法

      本文链接:https://www.haomeiwen.com/subject/luiazrtx.html