中缀表示法
发表于|更新于
|浏览量:
无意苦争春,一任群芳妒。零落成泥碾作尘,只有香如故。——陆游
中缀表示法能让我们定义一些“关键字”
标有 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 |
效果:

相关推荐
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>
2022-09-06
kotlin相关依赖
朋友间保持一定的距离,而使友谊永存——查尔卡 第一次用kt写东西的时候,没引入GAV都可以用…应该是idea集成了kt环境,到线上打包就没有了 新建了一个kotlin项目,发现需要GAV才能打包进去 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253<properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <kotlin.version>1.7.10</kotlin.version></properties...
2023-04-28
mvc导出excel报错No converter
无翼而飞者,声也。——佚名 今天导出excel报错 1No converter for [class org.springframework.core.io.ByteArrayResource] with preset Content-Type 'application/vnd.ms-excel' 代码如下: 123456789@PostMapping("exportSettle")fun export(dto: CommonDTO) { val writer = ExcelUtil.getWriter() writer.writeRow(Maps.of("key", "value"), true) response.addHeader("Content-Disposition", writer.getDisposition("test.xlsx", CharsetUtil.CHARSET_UTF_8)) response.contentTyp...
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-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 = """...
2022-08-17
aop注解在整个类生效
志向是天才的幼苗,经过热爱劳动的双手培育,在肥田沃土里将成长为粗壮的大树。——苏霍姆林斯基 之前写过自定义注解和AOP,但其是作用于方法上 今天用kotlin写一个作用在类上的:主要是@annotation换成@within 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647package com.ruben.simplebootimport org.aspectj.lang.ProceedingJoinPointimport org.aspectj.lang.annotation.Aroundimport org.aspectj.lang.annotation.Aspectimport org.springframework.boot.autoconfigure.SpringBootApplicationimport org.springframework.boot.runApplicationimport org.springframework.ster...
