当前位置:网站首页>【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类,但是否也有着优先级的区别呢?
以上仅为我自己的想法,若有不同见解可以讨论,若有错误也请指出
边栏推荐
- 【点云处理之论文狂读前沿版13】—— GAPNet: Graph Attention based Point Neural Network for Exploiting Local Feature
- 数位统计DP AcWing 338. 计数问题
- [point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion
- Liteide is easy to use
- 【点云处理之论文狂读经典版7】—— Dynamic Edge-Conditioned Filters in Convolutional Neural Networks on Graphs
- Gaussian elimination acwing 883 Gauss elimination for solving linear equations
- 剑指 Offer II 029. 排序的循环链表
- LeetCode 715. Range 模块
- 【点云处理之论文狂读经典版12】—— FoldingNet: Point Cloud Auto-encoder via Deep Grid Deformation
- [graduation season | advanced technology Er] another graduation season, I change my career as soon as I graduate, from animal science to programmer. Programmers have something to say in 10 years
猜你喜欢

【点云处理之论文狂读经典版10】—— PointCNN: Convolution On X-Transformed Points

With low code prospect, jnpf is flexible and easy to use, and uses intelligence to define a new office mode

Liteide is easy to use

一个优秀速开发框架是什么样的?

LeetCode 532. 数组中的 k-diff 数对

Vs2019 configuration opencv3 detailed graphic tutorial and implementation of test code

The "booster" of traditional office mode, Building OA office system, was so simple!

AcWing 788. 逆序对的数量

拯救剧荒,程序员最爱看的高分美剧TOP10

Excel is not as good as jnpf form for 3 minutes in an hour. Leaders must praise it when making reports like this!
随机推荐
【点云处理之论文狂读经典版7】—— Dynamic Edge-Conditioned Filters in Convolutional Neural Networks on Graphs
低代码前景可期,JNPF灵活易用,用智能定义新型办公模式
LeetCode 438. 找到字符串中所有字母异位词
Noip 2002 popularity group selection number
Just graduate student reading thesis
C language programming specification
Internet Protocol learning record
Digital management medium + low code, jnpf opens a new engine for enterprise digital transformation
[point cloud processing paper crazy reading classic version 9] - pointwise revolutionary neural networks
Discussion on enterprise informatization construction
网络安全必会的基础知识
Arbre DP acwing 285. Un bal sans patron.
LeetCode 438. Find all letter ectopic words in the string
精彩回顾|I/O Extended 2022 活动干货分享
传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!
[point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis
Install third-party libraries such as Jieba under Anaconda pytorch
Memory search acwing 901 skiing
Solve POM in idea Comment top line problem in XML file
LeetCode 57. 插入区间