当前位置: 首页 > news >正文

Kotlin 运算符重载

在Kotlin中,常用的运算符重载函数名如下:

1.算术操作符:
加法:plus
减法:minus
乘法:times
除法:div
取模:rem 或 mod
整数除法:floorDiv
求幂:pow
自增:inc
自减:dec

2.比较操作符:
等于:equals 或 ==
不等于:notEquals 或 !=
大于:greater 或 >
小于:less 或 <
大于等于:greaterEquals 或 >=
小于等于:lessEquals 或 <=

3.索引操作符:
获取元素:get
设置元素:set

4.调用操作符:
调用:invoke

5.包含操作符:
包含:contains

6.类型转换操作符:
类型转换:as

7.赋值操作符:
赋值:set

8.一元操作符:
正号:unaryPlus
负号:unaryMinus
逻辑非:not

以上是一些常用的运算符重载函数名。需要注意的是,这些函数名是Kotlin语言规范的一部分,开发者应该遵循这些规则来定义运算符的行为。
下先举个简单的例子:

  1. 算术操作符
    我们可以为自定义类定义加法、减法、乘法和除法操作符的行为。
data class Point(val x: Int, val y: Int) {
    operator fun plus(other: Point): Point {
        return Point(x + other.x, y + other.y)
    }

    operator fun minus(other: Point): Point {
        return Point(x - other.x, y - other.y)
    }

    operator fun times(other: Int): Point {
        return Point(x * other, y * other)
    }

    operator fun div(other: Int): Point {
        return Point(x / other, y / other)
    }
}

fun main() {
    val p1 = Point(1, 2)
    val p2 = Point(3, 4)
    val p3 = p1 + p2 // 相当于 p1.plus(p2)
    val p4 = p1 - p2 // 相当于 p1.minus(p2)
    val p5 = p1 * 2 // 相当于 p1.times(2)
    val p6 = p1 / 2 // 相当于 p1.div(2)
    println(p3) // 输出: Point(x=4, y=6)
    println(p4) // 输出: Point(x=-2, y=-2)
    println(p5) // 输出: Point(x=2, y=4)
    println(p6) // 输出: Point(x=0, y=1)
}

2.索引操作符
我们可以为自定义类定义索引操作符[]的行为。

class MyList<T>(vararg elements: T) {
    private val list = elements.toMutableList()

    operator fun get(index: Int): T {
        return list[index]
    }

    operator fun set(index: Int, value: T) {
        list[index] = value
    }
}

fun main() {
    val myList = MyList(1, 2, 3)
    println(myList[1]) // 输出: 2
    myList[1] = 4
    println(myList[1]) // 输出: 4
}
  1. 调用操作符
    我们可以为自定义类定义调用操作符()的行为。
class Greeter(val name: String) {
    operator fun invoke(greeting: String): String {
        return "$greeting, $name!"
    }
}

fun main() {
    val greeter = Greeter("Twg")
    println(greeter("Hello")) // 输出: Hello, Twg!
}

4.包含操作符
我们可以为自定义类定义包含操作符in和!in的行为。

class Range(val start: Int, val end: Int) {
    operator fun contains(value: Int): Boolean {
        return value in start..end
    }
}

fun main() {
    val range = Range(1, 10)
    println(8 in range) // 输出: true
    println(18 !in range) // 输出: true
}

以上就是在Kotlin中运算符重载的一些示例。通过运算符重载,我们可以使代码更加简洁和易读。

http://www.dtcms.com/a/47027.html

相关文章:

  • Python 爬虫 – BeautifulSoup
  • 在Linux系统上使用nmcli命令配置各种网络(有线、无线、vlan、vxlan、路由、网桥等)
  • MySQL中的共享锁和排他锁
  • Qwen2-Audio系列学习笔记
  • 事件循环_经典面试题
  • 【软件测试】论坛系统功能测试报告
  • 【HCIE实验1】模拟 DHCPv6 服务器及 PD 服务器分配 IPv6 地址和前缀的网络环境。
  • [代码规范]接口设计规范
  • 汽车控制应用对芯片的特殊要求
  • 在分布式系统中,解决因锁持有者故障导致锁无法释放的问题
  • Yocto + 树莓派摄像头驱动完整指南
  • spring boot整合flyway实现数据的动态维护
  • 如何在优云智算平台上面使用deepseek进行深度学习
  • 命名实体识别与文本生成算法
  • 迷你世界脚本组队接口:Team
  • Git学习
  • centos7使用rpm包安装mysql5.6和mysql8.0
  • 如何在docker中的mysql容器内执行命令与执行SQL文件
  • Linux:动静态库
  • (贪心 合并区间)leetcode 56
  • 接口性能优化?
  • 介绍 torch-mlir 从 pytorch 生态到 mlir 生态
  • 《Python实战进阶》No 10:基于Flask案例的Web 安全性:防止 SQL 注入、XSS 和 CSRF 攻击
  • C# 上位机---INI 文件
  • Ubuntu24.04 安装ssh开启22端口及允许root用户远程登录
  • 嵌入式软件数据结构(二)数组知识点专栏 附源码 附原理
  • Redis数据结构-List列表
  • 地基简识Spring MVC 组件
  • 制造业中的“大数据”:如何实现精准决策?
  • Gorm中的First()、Create()、Update()、Delete()的错误处理