当前位置:网站首页>[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?
[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?
2022-07-03 09:22:00 【Android old monkey】
stay Kotlin Class overloads an arithmetic operator , And declare the operator as an extension function ?
example
Call at this time ’+' Operator
An error is as follows
Interpret errors Because the receiver type cannot be matched, the applicable... Cannot be found from the candidate methods
Analyze errors
Analyze three different ways of overloading plus function
The first one is : Overload in the form of standard extension functions plus function
hold kotlin Decompile the code into java Code
After decompilation, we can find that plus Function in PointKt Class , This is also in line with what we said in the previous chapter “ Extension functions are not part of a class , It's declared outside the class ”, stay java In, it declares this function in a class with similar class names , And it's a static function . As an extension function plus There are two parameters ,kotlin Calling ’+' At the time java Is compiled into the following figure
The second kind : Overloaded in the form of standard member functions plus function
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-1Pu5hK0N-1654782243961)(https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/933eddfdc1b446388a393590ce56407d~tplv-k3u1fbpfcp-zoom-in-crop-mark:1956:0:0:0.image?)]
hold kotlin Decompile the code into java Code
After decompilation, we can find that as a member function plus Now there is only one parameter , and kotlin Calling ’+' At the time java Is compiled into the following figure
The third kind of : The member function form is mixed with the extension function form
hold kotlin Decompile the code into java Code
General analysis
The first two forms are no problem , Let's look at the third usage . Put the third kind of java The code is the same as the first two java Combined with the code, we can find out some connections and differences between them
Connection and difference
It's the same as the second one plus The same function is Point Class member functions , But there are two parameters It's the same as the first one plus Like a function, it has two parameters , But none of the first plus Functional static Statement
The third compiled function is like stitching the writing methods of the first two functions . Previously, we have given the first and second methods in calling plus What a function looks like , And we can also infer the appearance of the third call :point.plus(?,new Point(3,4))
. At this point, I was thinking that since the error prompts us that the receiver type cannot match , So why can't the compiler pass itself into the function as an argument ? I can try to write an operator overloaded with two parameters plus Function? ?
give the result as follows
stay kotlin This arithmetic operator can only have one parameter
I guess kotlin When calling arithmetic operators in the compiler , If it is a member function, it will only provide the writing method of one parameter , If it is an extension function, use the writing method of providing two parameters . The third way of writing plus Function is both a member function and requires two parameters , This may be the reason why the third writing method cannot run successfully
from equals Method we can see that it inherits from Any Class implementation always takes precedence over extension functions , And our situation, though not inherited from Any class , But is there a priority difference ?
The above is just my own idea , If you have different opinions, you can discuss , If there are errors, please also point out
边栏推荐
- Bert install no package metadata was found for the 'sacraments' distribution
- Just graduate student reading thesis
- Go language - JSON processing
- LeetCode 535. Encryption and decryption of tinyurl
- Navicat, MySQL export Er graph, er graph
- LeetCode 57. 插入区间
- 【点云处理之论文狂读经典版8】—— O-CNN: Octree-based Convolutional Neural Networks for 3D Shape Analysis
- LeetCode 508. The most frequent subtree elements and
- Notes on numerical analysis (II): numerical solution of linear equations
- [point cloud processing paper crazy reading classic version 11] - mining point cloud local structures by kernel correlation and graph pooling
猜你喜欢
Spark 结构化流写入Hudi 实践
教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?
[point cloud processing paper crazy reading classic version 12] - foldingnet: point cloud auto encoder via deep grid deformation
AcWing 786. 第k个数
Recommend a low code open source project of yyds
浅谈企业信息化建设
With low code prospect, jnpf is flexible and easy to use, and uses intelligence to define a new office mode
[point cloud processing paper crazy reading classic version 10] - pointcnn: revolution on x-transformed points
常见渗透测试靶场
AcWing 786. Number k
随机推荐
2022-2-14 learning the imitation Niuke project - send email
2022-2-14 learning xiangniuke project - Session Management
拯救剧荒,程序员最爱看的高分美剧TOP10
Pic16f648a-e/ss PIC16 8-bit microcontroller, 7KB (4kx14)
On a un nom en commun, maître XX.
Windows安装Redis详细步骤
[point cloud processing paper crazy reading classic version 10] - pointcnn: revolution on x-transformed points
IDEA 中使用 Hudi
Redis learning (I)
【点云处理之论文狂读经典版13】—— Adaptive Graph Convolutional Neural Networks
Hudi 快速体验使用(含操作详细步骤及截图)
LeetCode 57. 插入区间
Solve POM in idea Comment top line problem in XML file
How to check whether the disk is in guid format (GPT) or MBR format? Judge whether UEFI mode starts or legacy mode starts?
【Kotlin学习】类、对象和接口——定义类继承结构
Bert install no package metadata was found for the 'sacraments' distribution
Crawler career from scratch (V): detailed explanation of re regular expression
Derivation of Fourier transform
Severity code description the project file line prohibits the display of status error c2440 "initialization": unable to convert from "const char [31]" to "char *"
推荐一个 yyds 的低代码开源项目