美文网首页
kotlin 解构函数声明和运算符重载operator

kotlin 解构函数声明和运算符重载operator

作者: Bfmall | 来源:发表于2022-11-12 21:17 被阅读0次

kt的解构函数声明和运算符重载operator学习笔记###

/**
 * DESC   : kt的解构函数声明和运算符重载operator
 */
const val KtBaseObjectTest07_TAG = "KtBaseObjectTest07"

class TeacherBean(var name: String, var age : Int, var info : String) {

    /**
     * 注意:component的顺序必须和参数一一对应,从component1开始到2,3.......
     * 函数声明为operator类型
     */
    operator fun component1() = name
    operator fun component2() = age
    operator fun component3() = info
}

data class TeacherBean2(var name: String, var age : Int, var info : String)


/**
 * 为了方便测试,使用data class, 使用toString方法打印
 * 测试运算符重载
 */
data class ProductInfo(val price1: Int, val price2: Int) {
    operator fun plus(otherProductInfo: ProductInfo) : ProductInfo {
        return ProductInfo(price1 + otherProductInfo.price1, price2 + otherProductInfo.price2)
    }
}


class KtBaseObjectTest07 {

    fun testComponent01() {

        //testComponent01==>com.xyaty.kotlinbasedemo.base05.TeacherBean@9381bfb
        Log.d(KtBaseObjectTest07_TAG, "testComponent01==>"+TeacherBean("张三", 30, "english"))

        //testComponent01==>TeacherBean2(name=李四, age=31, info=china)
        Log.d(KtBaseObjectTest07_TAG, "testComponent01==>"+TeacherBean2("李四", 31, "china"))

        /**
         * 如果不加operator,使用下面的解构代码会出错
         * operator fun component1() = name
         */
        val(name, age, info) = TeacherBean("laowang", 50, "游民")
        //testComponent01==>解构...name=laowang, age=50, info=游民
        Log.d(KtBaseObjectTest07_TAG, "testComponent01==>解构...name="+name
            +", age="+age+", info="+info)

        val(name2, age2, _) = TeacherBean2("laowang", 50, "老王信息不接收")
        //testComponent01==>解构...name2=laowang, age2=50
        Log.d(KtBaseObjectTest07_TAG, "testComponent01==>解构...name2="+name2
                +", age2="+age2)
    }

    /**
     * 运算符重载
     */
    fun testOperator01() {
        val p1  = ProductInfo(20, 200)
        val p2 = ProductInfo(30, 300)

        val p3 = p1.plus(p2)
        //testOperator01==>ProductInfo(price1=50, price2=500)
        Log.d(KtBaseObjectTest07_TAG, "testOperator01==>"+p3)

    }
}

相关文章

  • kotlin 解构函数声明和运算符重载operator

    kt的解构函数声明和运算符重载operator学习笔记###

  • kotlin-第四章(高级特性)

    解构 循环和操作符 运算符重载 作用域函数 中缀表达式 SDL 13:解构 operator:将一个函数标记为重载...

  • 2017.9.22

    今天上课主要讲了运算符重载。运算符重载格式:类型名operator运算符(形参表){函数体}operator运算符...

  • 9月22日C++学习总结

    1.运算符重载:运算符重载的格式为:类型名 operator 运算符(形参表){函数体},operator是关键字...

  • 运算符重载

    运算符重载 运算符重载的本质是函数重载。 语法格式 operator 运算符名称 在一起构成了新的函数名。比如对于...

  • C++ 重载运算符和重载函数(2)

    重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一...

  • 12.运算符重载

    输出结果如下: kotlin使用operator来重载运算符,并且可以自定义类型。

  • 运算符重载

    基本概念 运算符重载的实质是函数重载,我们使用operator去标记需要重载的运算符。 对已有对运算符赋予多重对含...

  • C++ - 自加/自减运算符的重载

    ++/-- 运算符有前置/后置之分 前置运算符作为一元运算符重载重载为成员函数:T operator++();T ...

  • [Flutter]flutter基础之Dart语言基础(四)

    一、运算符重载 Dart 支持运算符重载,使用 operator 关键字,语法格式为:operator 运算符 ,...

网友评论

      本文标题:kotlin 解构函数声明和运算符重载operator

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