当前位置:网站首页>【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
2022-07-03 08:59:00 【安卓老猴子】
在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
实例

此时调用’+'运算符

报错如下

对错误进行解读 由于接收者类型无法匹配导致无法从候选方法中找到可以应用的
对错误进行分析
分析三种不同方式重载的plus函数
第一种:以标准的扩展函数形式重载plus函数

把kotlin代码反编译成java代码
通过反编译后我们可以发现此时plus函数处于PointKt类中,这也符合了我们之前篇章中说到“扩展函数并不是类的一部分,而是声明在类之外的”,在java中它把这个函数声明在了有着相似类名的类中,并且是一个静态函数。作为拓展函数的plus有着两个参数,kotlin在调用’+'时在java中被编译成下图
第二种:以标准的成员函数形式重载plus函数
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1Pu5hK0N-1654782243961)(https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/933eddfdc1b446388a393590ce56407d~tplv-k3u1fbpfcp-zoom-in-crop-mark:1956:0:0:0.image?)]
把kotlin代码反编译成java代码

反编译后我们可以发现作为成员函数的plus现在只有一个参数,而kotlin在调用’+'时在java中被编译成下图

第三种:成员函数形式与扩展函数形式混合使用

把kotlin代码反编译成java代码

总分析
前两种形式是没有问题的写法,让我们着眼第三种的用法。把第三种的java代码与前两种的java代码相结合我们可以找出它们之间的一些联系与区别
联系与区别
它和第二种中的plus函数一样是Point类中的成员函数,但却有着两个参数 它和第一种中的plus函数一样有着两个参数,但没有第一种中plus函数的static声明
第三种编译出来的函数就像是缝合了前两种函数的写法一般。前面我们给出了第一种和第二种在调用plus函数的样子,而我们也可以据此推断出第三种调用时的样子:point.plus(?,new Point(3,4))。此时我就在想既然错误提示我们接收者类型不能匹配上,那为什么编译器不能把本身作为参数传入函数中呢?我可以尝试自己写一个带有两个参数的并且是运算符重载的plus函数吗?
结果如下
在kotlin层面重载该算术运算符只能存在一个参数
我猜想在kotlin编译器中在调用算术运算符时,若是成员函数只会提供一个参数的写法,若是扩展函数则使用提供两个参数的写法。第三种写法的plus函数既是成员函数又需要两个参数,可能就是如此才导致第三种写法无法成功运行
从equals方法我们可以看出继承自Any类的实现始终优先于扩展函数,而我们这种情况虽然不是继承自Any类,但是否也有着优先级的区别呢?
以上仅为我自己的想法,若有不同见解可以讨论,若有错误也请指出
边栏推荐
- [advanced feature learning on point clouds using multi resolution features and learning]
- [point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis
- Basic knowledge of network security
- Using variables in sed command
- [point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
- Method of intercepting string in shell
- LeetCode 30. Concatenate substrings of all words
- AcWing 788. Number of pairs in reverse order
- 剑指 Offer II 029. 排序的循环链表
- 状态压缩DP AcWing 291. 蒙德里安的梦想
猜你喜欢

Debug debugging - Visual Studio 2022

State compression DP acwing 291 Mondrian's dream

How to check whether the disk is in guid format (GPT) or MBR format? Judge whether UEFI mode starts or legacy mode starts?

传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!

2022-2-14 learning xiangniuke project - generate verification code

【点云处理之论文狂读经典版11】—— Mining Point Cloud Local Structures by Kernel Correlation and Graph Pooling
![[point cloud processing paper crazy reading classic version 9] - pointwise revolutionary neural networks](/img/ea/2c4336ee929c26c16627e5c0955704.png)
[point cloud processing paper crazy reading classic version 9] - pointwise revolutionary neural networks

LeetCode 30. Concatenate substrings of all words

【点云处理之论文狂读前沿版13】—— GAPNet: Graph Attention based Point Neural Network for Exploiting Local Feature

LeetCode 871. Minimum refueling times
随机推荐
Go language - JSON processing
【点云处理之论文狂读前沿版11】—— Unsupervised Point Cloud Pre-training via Occlusion Completion
[untitled] use of cmake
C language programming specification
剑指 Offer II 029. 排序的循环链表
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
【毕业季|进击的技术er】又到一年毕业季,一毕业就转行,从动物科学到程序员,10年程序员有话说
Internet Protocol learning record
MySQL installation and configuration (command line version)
Vs2019 configuration opencv3 detailed graphic tutorial and implementation of test code
STM32F103 can learning record
Instant messaging IM is the countercurrent of the progress of the times? See what jnpf says
Data mining 2021-4-27 class notes
2022-2-14 learning xiangniuke project - Session Management
LeetCode 438. 找到字符串中所有字母异位词
Complex character + number pyramid
状态压缩DP AcWing 291. 蒙德里安的梦想
【点云处理之论文狂读经典版14】—— Dynamic Graph CNN for Learning on Point Clouds
Jenkins learning (III) -- setting scheduled tasks
[point cloud processing paper crazy reading classic version 7] - dynamic edge conditioned filters in revolutionary neural networks on Graphs