当前位置:网站首页>[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
边栏推荐
- Temper cattle ranking problem
- [untitled] use of cmake
- [point cloud processing paper crazy reading frontier version 8] - pointview gcn: 3D shape classification with multi view point clouds
- 【点云处理之论文狂读前沿版10】—— MVTN: Multi-View Transformation Network for 3D Shape Recognition
- 2022-2-13 learn the imitation Niuke project - Project debugging skills
- LeetCode 515. 在每个树行中找最大值
- 网络安全必会的基础知识
- Common penetration test range
- 【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
- [set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
猜你喜欢

Solve POM in idea Comment top line problem in XML file

LeetCode 508. The most frequent subtree elements and
![[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
![[point cloud processing paper crazy reading classic version 14] - dynamic graph CNN for learning on point clouds](/img/7d/b66545284d6baea2763fd8d8555e1d.png)
[point cloud processing paper crazy reading classic version 14] - dynamic graph CNN for learning on point clouds

Move anaconda, pycharm and jupyter notebook to mobile hard disk

excel一小时不如JNPF表单3分钟,这样做报表,领导都得点赞!

Digital management medium + low code, jnpf opens a new engine for enterprise digital transformation
![[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis](/img/c6/5f723d9021cf684dcfb662ed3acaec.png)
[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis
![[kotlin learning] classes, objects and interfaces - define class inheritance structure](/img/66/34396e51c59504ebbc6b6eb9831209.png)
[kotlin learning] classes, objects and interfaces - define class inheritance structure

With low code prospect, jnpf is flexible and easy to use, and uses intelligence to define a new office mode
随机推荐
网络安全必会的基础知识
AcWing 787. Merge sort (template)
Move anaconda, pycharm and jupyter notebook to mobile hard disk
[advanced feature learning on point clouds using multi resolution features and learning]
Redis learning (I)
Crawler career from scratch (3): crawl the photos of my little sister ③ (the website has been disabled)
【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
Common penetration test range
Sword finger offer II 091 Paint the house
What is an excellent fast development framework like?
[point cloud processing paper crazy reading classic version 7] - dynamic edge conditioned filters in revolutionary neural networks on Graphs
Notes on numerical analysis (II): numerical solution of linear equations
LeetCode 508. The most frequent subtree elements and
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
2022-2-13 learning xiangniuke project - version control
Jenkins learning (III) -- setting scheduled tasks
Problems in the implementation of lenet
Instant messaging IM is the countercurrent of the progress of the times? See what jnpf says
2022-2-14 learning xiangniuke project - Session Management
【点云处理之论文狂读经典版11】—— Mining Point Cloud Local Structures by Kernel Correlation and Graph Pooling