WTF Solidity极简入门第十六讲:函数重载

This article is not available in the current language yet. Showing the original version.
本文介绍了solidity中函数重载的基本用法。

原文:《Solidity 极简入门: 16. 函数重载》

我最近在重新学 solidity,巩固一下细节,也写一个「Solidity 极简入门」,供小白们使用(编程大佬可以另找教程),每周更新 1-3 讲。

所有代码和教程开源在 github: github.com/AmazingAng/WTFSolidity

重载

solidity 中允许函数进行重载(overloading),即名字相同但输入参数类型不同的函数可以同时存在,他们被视为不同的函数。注意,solidity 不允许修饰器(modifier)重载。

函数重载

举个例子,我们可以定义两个都叫 saySomething() 的函数,一个没有任何参数,输出"Nothing";另一个接收一个 string 参数,输出这个 string。

WTF Solidity极简入门第十六讲:函数重载

实参匹配(Argument Matching)

在调用重载函数时,会把输入的实际参数和函数参数的变量类型做匹配。

如果出现多个匹配的重载函数,solidity 文档上说会报错。它给的例子是两个叫 f() 的函数,一个参数为 uint8,另一个为 uint256。文档说如果输入 50,既可以被转换为 uint8,也可以被转换为 uint256,因此会报错。但是我没遇到:

WTF Solidity极简入门第十六讲:函数重载

总结

这一讲,我们介绍了 solidity 中函数重载的基本用法:名字相同但输入参数类型不同的函数可以同时存在,他们被视为不同的函数。

推特:@0xAA_Science

社区:Discord微信群官网 wtf.academy

所有代码和教程开源在github: github.com/AmazingAng/WTFSolidity

Share to:

Author: 0xAA

Opinions belong to the column author and do not represent PANews.

This content is not investment advice.

Image source: 0xAA. If there is any infringement, please contact the author for removal.

Follow PANews official accounts, navigate bull and bear markets together
PANews APP
It is suspected that the Dragonfly address transferred 137 million SKY tokens to Coinbase, a decrease in value of more than half compared to when the tokens were withdrawn.
PANews Newsflash