当前位置:网站首页>[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
边栏推荐
- Severity code description the project file line prohibits the display of status error c2440 "initialization": unable to convert from "const char [31]" to "char *"
- 【点云处理之论文狂读前沿版8】—— Pointview-GCN: 3D Shape Classification With Multi-View Point Clouds
- Just graduate student reading thesis
- 传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!
- 网络安全必会的基础知识
- 传统企业数字化转型需要经过哪几个阶段?
- Basic knowledge of network security
- [kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
- LeetCode 57. 插入区间
- AcWing 787. 归并排序(模板)
猜你喜欢

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

Vs2019 configuration opencv3 detailed graphic tutorial and implementation of test code

Discussion on enterprise informatization construction

Install third-party libraries such as Jieba under Anaconda pytorch

网络安全必会的基础知识
![[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords](/img/ee/d982fd9e1f2283e09ad1a81d0b61b5.png)
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords

AcWing 786. Number k

LeetCode 1089. Duplicate zero

Hudi 快速体验使用(含操作详细步骤及截图)
【毕业季|进击的技术er】又到一年毕业季,一毕业就转行,从动物科学到程序员,10年程序员有话说
随机推荐
数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
Excel is not as good as jnpf form for 3 minutes in an hour. Leaders must praise it when making reports like this!
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
Low code momentum, this information management system development artifact, you deserve it!
Crawler career from scratch (IV): climb the bullet curtain of station B through API
What is the difference between sudo apt install and sudo apt -get install?
C language programming specification
【Kotlin学习】类、对象和接口——定义类继承结构
传统企业数字化转型需要经过哪几个阶段?
【点云处理之论文狂读经典版10】—— PointCNN: Convolution On X-Transformed Points
Discussion on enterprise informatization construction
Jenkins learning (III) -- setting scheduled tasks
Go language - Reflection
Vs2019 configuration opencv3 detailed graphic tutorial and implementation of test code
The "booster" of traditional office mode, Building OA office system, was so simple!
[point cloud processing paper crazy reading classic version 11] - mining point cloud local structures by kernel correlation and graph pooling
常见渗透测试靶场
LeetCode 508. The most frequent subtree elements and
LeetCode 1089. 复写零
LeetCode 57. 插入区间