中缀表示法
发表于|更新于
|浏览量:
无意苦争春,一任群芳妒。零落成泥碾作尘,只有香如故。——陆游
中缀表示法能让我们定义一些“关键字”
标有 infix 关键字的函数也可以使用中缀表示法(忽略该调用的点与圆括号)调用。中缀函数必须满足以下要求:
1
2
3
4
5
6
7 infix fun Int.shl(x: Int): Int { …… }
// 用中缀表示法调用该函数
1 shl 2
// 等同于这样
1.shl(2)
代码如下:
1 | infix fun Int?.default(x: Int): Int = this ?: x |
效果:

相关推荐
2022-08-26
kotlin字符串模板
超乎一切之上的一件事,就是保持青春朝气。——莎士比亚 中文文档:https://www.kotlincn.net/docs/reference/basic-syntax.html#using-string-templates 1234567var a = 1// 模板中的简单名称:val s1 = "a is $a" a = 2// 模板中的任意表达式:val s2 = "${s1.replace("is", "was")}, but now is $a" 得到s2结果: a was 1, but now is 2 如果我们需要使用$符号,则可以参考: https://www.kotlincn.net/docs/reference/basic-types.html#%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%A8%A1%E6%9D%BF 使用${'$'} 例如下面的多行文本: 123val price = """...
2023-04-20
kotlin的KProperty
浪子挥霍的是他的祖业,财迷葬送的却是他自己。——托·富勒 在mp中使用KtQueryWrapper时,需要如下使用: 1KtQueryWrapper(User::class.java).eq(User::name, "sss").eq(User::roleId, "sss2") 这里的User::name是一个KProperty,其文档:https://kotlinlang.org/docs/reflection.html 可以使用KProperty的name属性获取到其属性名 1User::name.name 当然其还有很丰富的其他属性、函数 在项目中使用其,需要先引入kotlin-reflect依赖 1234<dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-reflect</artifactId></dependency>
2023-01-05
kotlin获取属性注解
微笑具有一种挽救力,它可以点亮天空,可以振作精神,可以改变你周围的气氛,更可以改变你——乔·吉拉德 kotlin里获取属性注解首先引入反射依赖: 1234<dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-reflect</artifactId></dependency> 完整: 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmln...
2022-09-19
操作符重载
游客是你,风景是我,无法避免,让你经过。——《稀客》 中文文档 kotlin里我们可以进行操作符重载,以达到对象+对象、-对象这样的操作: 12345678910111213141516171819// 定义一个类data class Point(val x: Int, val y: Int)// 对其进行操作符重载,让其能够使用-Point()语法operator fun Point.unaryMinus() = Point(-x, -y)val point = Point(10, 20)println(-point) // 输出“Point(x=-10, y=-20)”// 对String进行操作符重载,让其能够使用 +"" 语法operator fun String.unaryPlus() = this + thisprintln(+"x")// 对其进行二元操作符重载,使其可以使用 Point() + Point() 语法operator fun Point.plus(s: Point) = Point(this.x + s....
2023-07-19
kotlin的..<运算符
此处果有可乐,我即别无所思。——林语堂 今天升级了kotlin版本1.9.0 发现了个新特性 https://kotlinlang.org/docs/whatsnew19.html#stable-operator-for-open-ended-ranges 这个是用来取代以前的until函数的 12345678fun main() { for (number in 2 until 10) { if (number % 2 == 0) { print("$number ") } } // 2 4 6 8} 现在: 12345678fun main() { for (number in 2..<10) { if (number % 2 == 0) { print("$number ") } } ...
2022-08-27
kotlin延迟属性
得饶人处且饶人——曹雪芹 kotlin习惯用法见:https://www.kotlincn.net/docs/reference/idioms.html 这里试试延迟属性: https://www.kotlincn.net/docs/reference/delegated-properties.html#%E5%BB%B6%E8%BF%9F%E5%B1%9E%E6%80%A7-lazy 1234567val lazyValue: String by lazy { println("computed!") "Hello"}println(lazyValue)println(lazyValue) 运行结果: 123computed!HelloHello

阿超
我的名字叫阿超 年龄25岁 家在北京市 职业是软件开发 每天最晚也会在八点前回家 不抽烟 酒浅尝辄止 晚上十二点上床 保证睡足八个小时 睡前写一篇博客 再做二十分钟俯卧撑暖身 然后再睡觉 基本能熟睡到天亮 像婴儿一样不留下任何疲劳和压力 就这样迎来第二天的早晨 健康检查结果也显示我很正常 我想说明我是一个不论何时都追求内心平稳的人 不拘泥于胜负 不纠结于烦恼 不树立使我夜不能寐的敌人 这就是我在这社会的生活态度
Follow Me公告
This is my Blog