<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Frank Kong - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ freeCodeCamp 是一个免费学习编程的开发者社区，涵盖 Python、HTML、CSS、React、Vue、BootStrap、JSON 教程等，还有活跃的技术论坛和丰富的社区活动，在你学习编程和找工作时为你提供建议和帮助。 ]]>
        </description>
        <link>https://www.freecodecamp.org/chinese/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Frank Kong - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/chinese/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sat, 09 May 2026 13:42:15 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/chinese/news/author/frank/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ 学习 Solidity——智能合约开发手册 ]]>
                </title>
                <description>
                    <![CDATA[ 当我在 2018 年从律师转行到软件工程师 [https://www.freecodecamp.org/news/from-lawyer-to-google-engineer/] 时，我从未想过我会像现在这样喜欢做开发工作。我也从没想过我最终会为谷歌 [https://www.freecodecamp.org/chinese/news/coding-interview-prep-for-big-tech/]和 Chainlink Labs 优秀的公司工作。 在从事法律和其他工作 15 年之后，我经历了许多工作、国家、公司和职业道路。它们都无法与我从编码工作中获得的快乐和兴奋相提并论。 不足之处？掌握新的编码技能可能会令人困惑、沮丧且耗时。而且很容易忘记一些微小但重要的细节。 所以我写了这本手册。它旨在让您尽快开始编写 Solidity 代码。它遵循帕累托法则（又名 80/20 法则），手册将会专注于 20% 的信息，而这 20% 的信息将满足你 80% 需求。 作为我在 Chainlink Labs [http://chain.link]  工作的一部分，我在学习 Solidi ]]>
                </description>
                <link>https://www.freecodecamp.org/chinese/news/learn-solidity-handbook/</link>
                <guid isPermaLink="false">63f4d73fcf34b8063af8925d</guid>
                
                    <category>
                        <![CDATA[ Solidity ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Frank Kong ]]>
                </dc:creator>
                <pubDate>Wed, 22 Feb 2023 03:16:00 +0000</pubDate>
                <media:content url="https://chinese.freecodecamp.org/news/content/images/2023/02/pexels-pixabay-417173--2-.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p data-test-label="translation-intro">
        <strong>原文：</strong> <a href="https://www.freecodecamp.org/news/learn-solidity-handbook/" target="_blank" rel="noopener noreferrer" data-test-label="original-article-link">Learn Solidity – A Handbook for Smart Contract Development</a>
      </p><!--kg-card-begin: markdown--><p>当我在 2018 年<a href="https://www.freecodecamp.org/news/from-lawyer-to-google-engineer/">从律师转行到软件工程师</a>时，我从未想过我会像现在这样喜欢做开发工作。我也从没想过我最终会为<a href="https://www.freecodecamp.org/chinese/news/coding-interview-prep-for-big-tech/">谷歌</a>和 Chainlink Labs 优秀的公司工作。</p>
<p>在从事法律和其他工作 15 年之后，我经历了许多工作、国家、公司和职业道路。它们都无法与我从编码工作中获得的快乐和兴奋相提并论。</p>
<p>不足之处？掌握新的编码技能可能会令人困惑、沮丧且耗时。而且很容易忘记一些微小但重要的细节。</p>
<p>所以我写了这本手册。它旨在让您尽快开始编写 Solidity 代码。它遵循帕累托法则（又名 80/20 法则），手册将会专注于 20% 的信息，而这 20% 的信息将满足你 80% 需求。</p>
<p>作为我在 <a href="http://chain.link">Chainlink Labs</a> 工作的一部分，我在学习 Solidity 时就开始了解这些概念，并且把它们总结在一起。我在 38 岁的时候转为程序员，这次我也使用了许多当时的自学方法。</p>
<p>这个手册是我经常会用到的参考文件。它旨在为初学者和中级开发人员提供参考，以便快速解答在你深入学习该语言时的问题。</p>
<p>我会不断更新这本手册，但我真的需要你的帮助！如果我需要更新这本手册，请发推给我 <a href="https://twitter.com/zubinpratap">@ZubinPratap</a> 告诉我。</p>
<p>我要感谢我出色的同事 <a href="https://twitter.com/@dalteco">Kevin Ryu</a>、<a href="https://twitter.com/@@andrej_dev">Andrej Rakic</a>、<a href="https://twitter.com/PatrickAlphaC">Patrick Collins</a> 和 <a href="https://twitter.com/RGottleber">Richard Gottleiber</a>，他们为本手册提供了宝贵的指导和意见。</p>
<p>（译者注：感谢 <a href="https://www.freecodecamp.org/chinese/news/author/luojiyin/">luojiyin</a> 参与翻译本手册，并提供详细的校对建议。）</p>
<h2 id="">目录</h2>
<ul>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#who-is-this-handbook-for">这本手册是为谁而写的</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#essential-prior-knowledge">必要的前置知识</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#what-is-solidity">什么是 Solidity</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#what-is-a-basic-smart-contract">什么是智能合约</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#how-to-declare-variables-and-functions-in-solidity">怎样在 Solidity 中声明变量和函数</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#variable-scope-in-smart-contracts">智能合约中的变量作用域</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#how-visibility-specifiers-work">如何使用可见性标识符（visibility specifier）</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#what-are-constructors">什么是构造函数</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#interfaces-and-abstract-contracts">接口和抽象合约</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#smart-contract-example-2">智能合约案例 #2</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#what-is-contract-state">什么是合约状态</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#state-mutability-keywords-modifiers-">状态可变性关键字（修饰符：modifier）</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#data-locations-storage-memory-and-stack">数据存储类型 – storage/memory/stack</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#how-typing-works">数据类型原理</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#solidity-data-types">Solidity 数据类型</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#how-to-declare-and-initialize-arrays-in-solidity">Solidity 中数组如何声明和初始化数组</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#what-are-function-modifiers">函数修饰符（function modifier）是什么</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#error-handling-in-solidity-require-assert-revert">Solidity 中的异常处理 - require/assert/revert</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#inheritance-in-solidity">Solidity 中的继承</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#inheritance-with-constructor-parameters">继承与构造函数参数</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#type-conversion-and-type-casting-in-solidity">Solidity 中的类型转换</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#how-to-work-with-floating-point-numbers-in-solidity">Solidity 中如何使用浮点数</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#hashing-abi-encoding-and-decoding">哈希、ABI 编码（encoding）和解码（decoding）</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#how-to-call-contracts-and-use-the-fallback-function">如何调用合约并且使用 fallback 函数</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#how-to-send-and-receive-ether">如何发送和接收 Ether</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#solidity-libraries">Solidity 库（library）</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#events-and-logs-in-solidity">Solidity 中的事件（events）和日志（logs）</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#time-logic-in-solidity">Solidity 中的时间逻辑</a></li>
<li><a href="https://chinese.freecodecamp.org/news/learn-solidity-handbook/#conclusion-and-further-resources">总结和更多资源</a></li>
</ul>
<h2 id="who-is-this-handbook-for">这本手册是为谁而写的</h2>
<p>本手册适用于有兴趣探索 <a href="https://chain.link/education/web3">“Web3”背后的愿景</a>，并希望学习相应的技能以实现该愿景所需的人。</p>
<p>不要死记硬背！阅读它，然后将其用作“参考文件”。当你学习任何一门新语言时，你会发现概念、习语和用法会变得有些混乱，或者你的记忆会随着时间的推移而消失。没关系！这就是本手册旨在帮助你随时查阅到所需的知识。</p>
<p>随着时间的推移，我可能会为此添加一些更高阶的内容，或者创建一个单独的教程。但就目前而言，这本手册将为你提供所需的大部分知识，以开始创建几个 Solidity DApp。</p>
<p>本手册假定你至少有几个月的编程经验，我的意思是至少你用 JavaScript 或 Python 或一些编译语言写过程序（HTML 和 CSS 实际上不是“编程”语言，所以只知道它们是不够的）。</p>
<p>唯一的其他要求是你要有好奇心、坚定不移，不要给自己限定任何的停止学习的时间点。</p>
<p>只要你有一台笔记本电脑和一个可以连接互联网的浏览器，你就可以运行 Solidity 代码。您可以在浏览器中使用 <a href="https://www.youtube.com/watch?v=JWJWT9cwFbo&amp;list=PLVP9aGDn-X0QwJVbQvuKr-zrh2_DV5M6J&amp;index=47">Remix</a> 来编写本手册中的代码。不需要其他 IDE！</p>
<h2 id="essential-prior-knowledge">必要的前置知识</h2>
<p>我还假设你了解<a href="https://blog.chain.link/what-is-blockchain/">区块链技术的基础知识</a>，尤其是<a href="https://www.youtube.com/watch?v=NsyFGzhktYA&amp;list=PLVP9aGDn-X0QwJVbQvuKr-zrh2_DV5M6J&amp;index=49">以太坊的基础知识</a>以及<a href="https://chain.link/education/smart-contracts">什么是智能合约</a>，提示：智能合约是在区块链上运行的程序，因此具备信任最小化（Trust-minimized）的优势！</p>
<p>虽然你不太可能需要它们来理解本手册，但实际上，拥有像 <a href="https://metamask.io/">Metamask</a> 这样的浏览器钱包并了解<a href="https://info.etherscan.com/understanding-ethereum-accounts/">以太坊合约账户和外部账户（EOA 账户）之间的区别</a>将帮助你充分利用这本手册。</p>
<h2 id="what-is-solidity">什么是 Solidity</h2>
<p>现在，让我们开始了解什么是 Solidity。 Solidity 是一种受 C++、JavaScript 和 Python 影响的<a href="https://www.freecodecamp.org/news/what-is-object-oriented-programming/">面向对象的编程语言</a>。</p>
<p>Solidity 旨在编译（从人类可读代码转换为机器可读代码）以太坊虚拟机 (EVM) 上运行的字节码。这是 Solidity 代码的<a href="https://www.techopedia.com/definition/5466/runtime-environment-rte">运行时环境</a>，就像你的浏览器是 JavaScript 代码的运行时环境一样。</p>
<p>所以，你通过 Solidity 编写智能合约，编译器将其转换为字节码。然后该字节码被部署并存储在以太坊（以及其他 EVM 兼容的区块链）上。</p>
<p>你可以在<a href="https://www.youtube.com/watch?v=Z7UNjk_roXI&amp;t=1052s">我制作的这个视频</a> 中找到对 EVM 和字节码的基本介绍。</p>
<h2 id="what-is-a-basic-smart-contract">什么是智能合约</h2>
<p>这是一个开箱即用的简单智能合约。它可能看起来没什么用，但你将从中了解很多 Solidity 知识！</p>
<p>请先连同每条评论一起阅读，以了解合约在做什么，然后继续学习一些关键知识。</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// SPDX-License-Identifier: MIT</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pragma solidity ^</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">0.8.8.0</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract HotFudgeSauce {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; uint public qtyCups;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 获得当前 hot fudge 的数量</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">get</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">view</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">uint</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> qtyCups;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 将 hot fudge 的数量加一的函数</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">increment</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; qtyCups += </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">1</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 与 qtyCups = qtyCups + 1; 一样</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 将 hot fudge 的数量减一的函数</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">decrement</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; qtyCups -= </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">1</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 与 qtyCups = qtyCups - 1; 一样</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 当qtyCups = 0 时函数被调用会发生什么？</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
我们将很快了解一些细节，例如 `public` 和 `view` 的含义。
<p>现在，从上面的例子中学习七个关键知识：</p>
<ol>
<li>
<p>第一个注释是机器可读行 (// SPDX-License-Identifier: MIT)，它指定了所包含的代码的许可。</p>
<p>强烈建议使用 SPDX 许可证标识符，尽管你的代码在没有它的情况下也能编译。在<a href="https://docs.soliditylang.org/en/v0.8.6/layout-of-source-files.html#spdx-license-identifier">这里</a>阅读更多。此外，你也可以添加注释或“注释掉”任何一行，方法是在其前面加上两个正斜杠“//”。</p>
</li>
<li>
<p>任意一个 Solidity 文件中，<code>pragma</code> 指令必须是在代码的第一行。 Pragma 是一个指令，它告诉编译器应该使用哪个编译器版本将人类可读的 Solidity 代码转换为机器可读的字节码。</p>
<p>Solidity 是一门新语言，更新频率很高，所以不同版本的编译器在编译代码时会产生不同的结果。当使用较新的编译器版本编译时，一些较旧的 solidity 文件会抛出错误或警告。</p>
<p>在较大的项目中，当你使用像 Hardhat 这样的工具时，可能需要指定多个编译器版本，因为导入的 solidity 文件或你依赖的库是为旧版本的 solidity 编写的。在<a href="https://docs.soliditylang.org/en/develop/layout-of-source-files.html">此处</a>阅读有关 Solidity 的 pragma 指令的更多信息。</p>
</li>
<li>
<p><code>pragma</code> 指令遵循语义化版本控制 (SemVer)，SemVer 是一个系统，其中每个数字表示该版本中包含的更改的类型和范围。如果你想要 SemVer 的实际操作解释，请查看本教程，SemVer 非常有助于理解，并且如今在开发（尤其是 Web 开发）中得到广泛使用。</p>
</li>
<li>
<p>分号在 Solidity 中是必不可少的。即使缺少一个，编译器也会失败。Remix 会提醒你！</p>
</li>
<li>
<p>关键字 <code>contract</code> 告诉编译器你正在声明一个智能合约。如果你熟悉面向对象编程，那么你可以将契约视为类。</p>
<p>如果你不熟悉 OOP，那么可以将合约视为保存数据的对象——包括变量和函数。你可以通过智能合约为区块链应用程序提供所需的功能。</p>
</li>
<li>
<p>函数是封装单个想法、特定功能、任务等的可执行代码单元。通常我们希望函数一次只做一件事。</p>
<p>尽管函数可以在智能合约代码块之外的文件中声明，当时它们通常还是出现在智能合约中。函数可以接受 0 个或多个参数，也可以返回 0 个或多个值。输入和输出是静态类型的，这是你将在本手册稍后部分了解的概念。</p>
</li>
<li>
<p>在上面的例子中，变量 <code>qtyCups</code> 被称为“状态变量”。它保存了合约的状态——这里的状态指的是程序需要跟踪运行的数据。</p>
<p>与其他程序不同，智能合约应用程序即使在程序未运行时也会保持其状态。数据与应用程序一起存储在区块链中，这意味着区块链网络中的每个节点都在本地副本维护和同步数据和智能合约。</p>
<p>状态变量就像传统应用程序中的数据库“存储”，但由于区块链需要在网络中的所有节点之间同步状态，因此使用存储可能非常昂贵！稍后会详细介绍。</p>
</li>
</ol>
<h2 id="how-to-declare-variables-and-functions-in-solidity">怎样在 Solidity 中声明变量和函数</h2>
<p>让我们分解一下 <code>HotFudgeSauce</code> 智能合约，以便我们更多地了解这个合约中的内容。</p>
<p>在 Solidity 中定义事物的基本结构/语法类似于其他静态类型语言。函数和变量都有名字。</p>
<p>但是在类型化语言中，我们还需要为创建、输入或输出返回的数据指定数据类型。如果你需要了解什么是类型化数据，可以跳到本手册的类型化数据部分。</p>
<p>下面，我们将看到如何声明“状态变量”。还可以看到如何声明函数。</p>
<p><img src="https://lh5.googleusercontent.com/P0bkGvqTA70VN-VZlV13ICNL4sxhzHCIU_8GL0JF9iHZPn6ml4vA6grWy_dKbiMa-y_ALc8oHOyIDGLFZgIIi5pU2Ep5ENuFJd0dPRL8RCPx8bksESh44loRbFqAA9f7GZ9_e5thytMuOk3EeObhmQnG791pBviHuxrv3N3HAXcGAQL0q-4TwV2LQhoA-w" alt="(Alt: The structure variable declaration and function declaration) " width="600" height="400" loading="lazy"></p>
<p>第一个部分声明了一个名为 <code>qtyCups</code> 的状态变量（我很快会解释这是什么）。这只能存储 <code>uint</code> 类型的值，这意味着无符号整数。 “整数”是指零以下（负）和零以上（正）的所有整数。</p>
<p>由于这些数字附有 + 或 - 符号，所以称为有符号整数。无符号整数始终是正整数（包括零）。</p>
<p>在第二个片段中，我们在声明函数时也看到了一个熟悉的结构。最重要的是，我们看到在函数中，必须为其返回的值指定数据类型。</p>
<p>在这个例子中，由于 <code>get()</code> 返回我们刚刚创建的存储变量的值，我们可以看到返回值必须是一个 <code>uint</code>。</p>
<p><code>public</code> 是可见性标识符。稍后会详细介绍。 <code>view</code> 是一个状态可变性修饰符，也会在后面的内容中介绍。</p>
<p>这里值得注意的是，状态变量也可以是其他类型——<code>constant</code> 和 <code>immutable</code>。它们是这样的：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">string constant TEXT = </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"abc"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">address immutable owner = </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span></p></td></tr></tbody></table>
constant 和 immutable 变量的值可以并且仅可以被赋予一次。在赋予了第一个值后，不能再给它们赋另一个值。
<p>因此，如果我们将 <code>qtyCups</code> 状态变量设为 constant 或 immutable，我们将无法再对其调用 <code>increment()</code> 或 <code>decrement()</code> 函数（事实上，调用的话代码将无法编译！）。</p>
<p>constant 的值必须在代码本身中硬编码（hardcode），而immutable 变量可以将它们的值设置一次，通常是通过构造函数中的赋值（我们很快就会讨论构造函数）。你可以在<a href="https://docs.soliditylang.org/en/v0.8.16/contracts.html#constant-and-immutable-state-variables">此处的文档</a>中阅读更多内容。</p>
<h2 id="variable-scope-in-smart-contracts">智能合约中的变量作用域</h2>
<p>智能合约中的变量有 3 个作用域：</p>
<ol>
<li>状态变量：通过将值记录在区块链上，在智能合约中存储永久数据（称为持久状态）。</li>
<li>局部变量：这些是“暂时性”数据，在运行计算时会在短时间内保存信息。这些值不会永久存储在区块链上。</li>
<li><a href="https://docs.soliditylang.org/en/v0.8.17/units-and-global-variables.html#special-variables-and-functions">全局变量</a>：这些变量和函数由 Solidity“注入”到您的代码中，无需专门创建或从任何地方导入它们即可使用。这些提供了代码运行时的区块链环境信息，还包括程序中会用到的功能性函数。</li>
</ol>
<p>你可以按如下方式区分变量作用域：</p>
<ol>
<li>状态变量通常位于智能合约内部，但位于函数外部。</li>
<li>局部变量位于函数内部，不能从该函数之外访问。</li>
<li>全局变量不是由你声明的，当时它们“神奇地”可供你使用。</li>
</ol>
<p>这是我们的 <code>HotFudgeSauce</code> 示例，稍作修改以显示不同类型的变量。我们给 <code>qtyCups</code> 一个初始值，然后给除了我以外的每个人分一杯 Fudge Sauce（因为我正在节食）。</p>
<p><img src="https://lh4.googleusercontent.com/Z9wk7BXxi-669WmSamT25cV88_RK-psGJvCA69vUxgpcs2_EzJFzLZJOitQlsuSl_AD-LdRylCaKh4Iumo_tVHmft2mtKofDu4qZfWQ8Z7BhdZ-6g4B6JAwLF57gMi-osCSDocgyE6kdqMCL4uxjBtpzjMf6rlLM0Ja6w4ndaIxEZhE3YsxmDY1BIWOwnA" alt="Annotated image showing state variables, local variables and global variables in a smart contract" width="600" height="400" loading="lazy"></p>
<h2 id="how-visibility-specifiers-work">如何使用可见性标识符（visibility specifier）</h2>
<p>“可见性”这个词的使用有点令人困惑，因为在公共区块链上，几乎所有东西都是“可见的”，因为透明度是一个关键特征。这里的可见性意味着一段代码可以被另一段代码看到和访问的能力。</p>
<p>可见性指定变量、函数或智能合约可以从定义它的代码所在的区域之外访问的程度。可以根据整个系统中的哪些部分需要访问它来调整其可见范围。</p>
<p>如果你是 JavaScript 或 NodeJS 开发人员，那么你已经熟悉可见性——你导出一个对象的时，就是为了使它在声明它的文件之外可见。</p>
<h3 id="">可见度类型</h3>
<p>在 Solidity 中有 <a href="https://docs.soliditylang.org/en/v0.8.16/cheatsheet.html#function-visibility-specifiers">4 种不同类型的可见性</a>：<code>public</code>、<code>external</code>、<code>internal</code> 和 <code>private</code>。</p>
<p><strong>Public</strong> 函数和变量可以在合约内部、外部、其他智能合约和外部账户（你 <a href="https://metamask.io/">Metamask</a> 钱包中的那种）访问——几乎可以从任何地方访问。这是最广泛、最宽松的可见性级别。</p>
<p>当一个存储变量被赋予 <code>public</code> 可见度时，Solidity 会自动为该变量的值创建一个隐性的 getter 函数。</p>
<p>所以在我们的 <code>HotFudgeSauce</code> 智能合约中，我们不需要 <code>get()</code> 方法，因为 Solidity 会隐式地为我们提供完全一样的功能，只需给 <code>qtyCups</code> 一个 <code>public</code> 可见度修饰符。</p>
<p><strong>Private</strong> 函数和变量只能在声明它们的智能合约中访问。但是它们不能在包含它们的智能合约之外访问。 <code>private</code> 是四个可见性说明符中限制性最强的。</p>
<p><strong>Internal</strong> 可见性类似于 <code>private</code> 可见性，因为内部函数和变量只能从声明它们的合约中访问。但是标记为 internal 的函数和变量也可以从派生合约（即从声明合约继承的子合约）访问，但不能从合约外部访问。稍后我们将讨论继承（和派生/子合约）。</p>
<p>状态变量的默认可见度就是 <code>internal</code>。</p>
<p><img src="https://lh5.googleusercontent.com/gcNCm0-739P27Bqp_5zYT6kM_fmfW3tnrx-ZJ8iKH5Gnhpp9-JXo4NrSk7UaUPv9SF34ka7eNYtFK-FmnTGtHSupFUE6A6UvX1738cqDG0X9qhWCRdqI-jGf1BCpvK2Qi6MqMQWgN4H3VOpa5xh2zVkQrQlfGuBPRWIovNxfDH3zhFuxHKsUpXq9Oh_Saw" alt="gcNCm0-739P27Bqp_5zYT6kM_fmfW3tnrx-ZJ8iKH5Gnhpp9-JXo4NrSk7UaUPv9SF34ka7eNYtFK-FmnTGtHSupFUE6A6UvX1738cqDG0X9qhWCRdqI-jGf1BCpvK2Qi6MqMQWgN4H3VOpa5xh2zVkQrQlfGuBPRWIovNxfDH3zhFuxHKsUpXq9Oh_Saw" width="600" height="400" loading="lazy"></p>
<p>4 种可见度标识符的表格</p>
<p><strong>external</strong> 可见性说明符不适用于变量 - 只有函数可以指定为 external。</p>
<p>external 函数不能从声明合约或继承自声明合约的合约的内部调用。因此，它们只能从该合约之外调用。</p>
<p>这就是它们与公共函数的不同之处——公共函数也可以从声明它们的合约内部调用，而外部函数则不能。</p>
<h2 id="what-are-constructors">什么是构造函数</h2>
<p>构造函数是一种特殊类型的函数。在 Solidity 中，它是可选的，仅在合约创建时执行一次。</p>
<p>在下面的示例中，我们有一个显式构造函数，它接受一些数据作为参数。你必须在创建智能合约时将此构造函数参数注入到这个智能合约中。</p>
<p><img src="https://lh6.googleusercontent.com/51je7Hakb-_ARNnomBYpwoKpf2j86WvwRLDlaFzGLlGowJLt1T1MX2978gAGv8eZCoiZc-jZParqor28wBLx97WyrMrso-EwSRHnOBi3O956nIyHN4-cezZJk8avP7JBegFOq4DJ997t1YnC61bD4IUH08SPEbvw6HoZSbXqbOt_LwzfQAHXfeXiNC6xHw" alt="51je7Hakb-_ARNnomBYpwoKpf2j86WvwRLDlaFzGLlGowJLt1T1MX2978gAGv8eZCoiZc-jZParqor28wBLx97WyrMrso-EwSRHnOBi3O956nIyHN4-cezZJk8avP7JBegFOq4DJ997t1YnC61bD4IUH08SPEbvw6HoZSbXqbOt_LwzfQAHXfeXiNC6xHw" width="600" height="400" loading="lazy"></p>
<p>有入参的 Solidity 构造函数</p>
<p>要了解构造函数何时被调用，先了解智能合约创建的几个阶段：</p>
<ul>
<li>它被编译成字节码（你可以在<a href="https://blog.chain.link/what-are-abi-and-bytecode-in-solidity/">这里</a>阅读更多关于字节码的信息）。这个阶段称为“编译时间”。</li>
<li>它被创建（构造） - 这是构造函数开始运行的时候。这可以称为“构造时间”。</li>
<li>然后字节码被部署到区块链。这就是“部署”。</li>
<li>部署的智能合约字节码在区块链上运行（执行）。这可以被认为是“运行时”。</li>
</ul>
<p>在 Solidity 中，与其他语言不同，程序（智能合约）仅在构造函数完成其创建智能合约的工作后才会部署。</p>
<p>有趣的是，在 Solidity 中，最终部署的字节码并不包含构造函数代码。这是因为在 Solidity 中，<a href="https://blog.openzeppelin.com/deconstructing-a-solidity-contract-part-ii-creation-vs-runtime-6b9d60ecb44c/">构造函数代码是创建代码（构造时间）的一部分</a>，而不是运行时代码的一部分。它在创建智能合约时用完了，因为它只会调用一次，在这个阶段过去后，就不需要被调用了，所以不回在最终部署的字节码中。</p>
<p>因此，在我们的示例中，构造函数创建（构造）<code>Person</code> 智能合约的一个实例。我们的构造函数希望我们将一个名为 <code>_name</code> 的字符串值传递给它。</p>
<p>当构建智能合约时，<code>_name</code> 的值将存储在名为 <code>name</code> 的状态变量中（这通常也是将配置信息和其他数据传递到智能合约的方式）。然后当实际部署合约时，状态变量 <code>name</code> 将保存我们传递给构造函数的任何字符串值。</p>
<h3 id="">理解为什么这样设计构造函数</h3>
<p>你可能想知道为什么我们费心将值注入构造函数。为什么不把它们写进合约呢？</p>
<p>这是因为我们希望合约是可配置的或“参数化的”。我们想要的不是将一个值硬编码（把值写死），而是在需要时注入合适的数据，这样才能给合约带来的灵活性和可重用性。</p>
<p>在我们的示例中，假设 <code>_name</code> 指的是将要部署合约的以太坊网络的名称（如 Rinkeby、Goerli、Kovan、Mainnet 等）。</p>
<p>我们如何将这些信息提供给智能合约？将所有这些值都放入其中会很浪费。这也意味着我们需要添加额外的代码来确定合约在哪个区块链上运行。然后我们必须从我们存储在合约中的硬编码列表中选择正确的网络名称，这将会在部署时使用更多的 gas。</p>
<p>相反，我们可以在将智能合约部署到相关区块链网络时将其注入构造函数。这样，我们就可以编写一个可以使用任意数值作为参数的合约。</p>
<p>另一个常见的用例是当你的智能合约继承自另一个智能合约，并且你需要在创建合约时将值传递给父智能合约。但是继承是我们后面要讨论的。</p>
<p>我提到构造函数是可选的。在 <code>HotFudgeSauce</code> 中，虽然我们没有编写显式构造函数，但是 Solidity 支持隐式构造函数。因此，如果我们不在智能合约中包含构造函数，Solidity 将<a href="https://docs.soliditylang.org/en/v0.8.13/contracts.html#constructors">假定一个默认构造函数</a>，看起来像 <code>constructor() {}</code>。</p>
<p>如果你评估一下它的作用，你会发现它其实什么都不做，这就是为什么它可以被隐藏（被隐式创建）并且让编译器使用默认构造函数。</p>
<h2 id="interfaces-and-abstract-contracts">接口和抽象合约</h2>
<p><a href="https://docs.soliditylang.org/en/develop/contracts.html#interfaces">solidity 中的接口</a>是一个需要理解的基本概念。以太坊上的智能合约是公开可见的，因此你可以通过它们的函数与它们进行交互（在可见度标识符允许的范围内！）。</p>
<p>这就是使智能合约“可组合”的原因，也是为什么如此多的 Defi 协议被称为“金钱乐高”——你可以编写与其他智能合约交互的智能合约，这些智能合约又与其他智能合约交互等等……现在你明白它的意思了。</p>
<p>当你想让智能合约 A 与智能合约 B 进行交互时，你需要 B 的接口。接口为你提供了各种函数的索引，你可以使用这些函数调用某个的智能合约。</p>
<p>接口的一个重要特征是它们不能对定义的任何函数有任何实现（代码逻辑）。接口只是函数名称及其预期参数和返回类型的集合。它们并不是 Solidity 独有的概念。</p>
<p>因此，我们的 <code>HotFudgeSauce</code> 智能合约的接口看起来像这样（请注意，按照惯例，solidity 接口的命名方式是在智能合约的名称前加上“I”前缀((就变成了 IHotFudgeSauce))：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// SPDX-License-Identifier: MIT</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pragma solidity ^</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">0.8.7</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">interface IHotFudgeSauce {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">get</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">view</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">uint</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">increment</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">decrement</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>就是这样！由于 <code>HotFudgeSauce</code> 只有三个函数，因此界面仅显示这些函数。</p>
<p>但这里有一个值得注意的点：接口并不需要包含智能合约中可调用的所有函数，所以你可以删掉一些不必要的函数，让接口包含打算调用的函数的定义就可以！</p>
<p>因此，如果你只想在 <code>HotFudgeSauce</code> 上使用 <code>decrement()</code> 方法，那么你完全可以从接口中删除 <code>get()</code> 和 <code>increment()</code> - 但你也将无法调用合约中的这两个函数。</p>
<p>那么到底发生了什么？好吧，接口只是让你的智能合约知道在你的目标智能合约中可以调用哪些函数，这些函数接受哪些参数（及其数据类型），以及你可以期望返回什么类型的数据。在 Solidity 中，这就是你与另一个智能合约交互所需的全部信息。</p>
<p>在某些情况下，你可以拥有一个类似于但又不同于接口的概念 -- 抽象合约(abstract contract)。</p>
<p><a href="https://docs.soliditylang.org/en/develop/contracts.html#abstract-contracts">抽象合约</a>是使用 <code>abstract</code> 关键字声明的，合约中声明了一个或多个函数但未实现的函数。或者使用另一种定义，至少有一个函数已声明但未实现。</p>
<p>反过来说，抽象合约可以有其实现的函数（不像接口不能有函数实现），但只要有一个函数未实现，合约就必须标记为抽象的：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// SPDX-License-Identifier: MIT</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pragma solidity ^</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">0.8.7</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">abstract contract Feline {</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;int public age;</span></p><br><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;// not implemented.</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">utterance</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">virtual</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">bytes32</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">);</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// implemented.</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">setAge</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(int _age) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;age = _age;</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;}</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>你可能（合理地）想知道这有什么意义。好吧，抽象合约不能直接实例化（创建）。它们只能被继承，继承它的合约可以使用它的函数。</p>
<p>因此，抽象合约通常被用作其他智能合约可以“继承”的模板或“基础合约”，从而迫使继承的智能合约实现抽象（父）合约声明的某些函数。这是在很多情况下的一种很有用的设计模式，即在相关合约之间强制统一结构。</p>
<p>当我们稍后讨论继承时，你会对继承相关的知识更清晰。现在，请记住，你可以声明一个不实现其所有函数的抽象智能合约——但如果你这样做，你将无法实例化它，而未来继承它的智能合约必须完成实现那些未实现函数。</p>
<p>接口和抽象合约之间的一些重要区别是：</p>
<ul>
<li>接口中不能有实现的函数，而抽象合约可以有任意数量的函数实现，但是至少有一个函数是“抽象的”（即未实现）。</li>
<li>接口中的所有函数都必须标记为 “external”，因为它们只能由实现该接口的其他合约调用。</li>
<li>接口不能有构造函数，而抽象合约可以有。</li>
<li>接口不能有状态变量，抽象合约可以有。</li>
</ul>
<h2 id="smart-contract-example-2">智能合约实例#2</h2>
<p>对于接下来的几个 Solidity 概念，我们将使用下面的智能合约。因为这个例子包含了一个在现实世界中实际使用的智能合约，我选择它也是因为我对 Chainlink Labs 有明显的偏好，因为我在那里工作 (😆) 而且 Chainlink Labs 是一家很棒的公司。但这也是我学到很多 Solidity 的地方，除此以外，通过真实世界的例子学习会更好。</p>
<p>因此，请先阅读下面的代码和评论。如果你仔细阅读，你已经了解了理解下面合约所需的 99%。然后我们继续从这份合约中学关键知识。</p>
<table style="border:none;border-collapse:collapse;"><colgroup><col width="694"></colgroup><tbody><tr style="height:0pt"><td style="border-left:solid #e0e0e0 1pt;border-right:solid #e0e0e0 1pt;border-bottom:solid #e0e0e0 1pt;border-top:solid #e0e0e0 1pt;vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// SPDX-License-Identifier: MIT</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pragma solidity ^</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">0.8.7</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">import</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract PriceConsumerV3 {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; AggregatorV3Interface internal priceFeed;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">/**</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; * Network: Goerli</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; * Aggregator: ETH/USD</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; * Address: 0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; */</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">constructor</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; priceFeed = AggregatorV3Interface(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/**</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; * Returns the latest price</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; */</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">getLatestPrice</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">view</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">int</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">/*uint80 roundID*/</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">,</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int price,</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">/*uint startedAt*/</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">,</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">/*uint timeStamp*/</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">,</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">/*uint80 answeredInRound*/</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; ) = priceFeed.latestRoundData();</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> price;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>该智能合约从实时运行 Chainlink 价格馈送预言机（参见 etherscan 上的预言机）获取 <a href="https://docs.chain.link/docs/get-the-latest-price/">1 ETH 最新的美元价格</a>。该示例使用 Goerli 网络，因此你不会在以太坊主网上花费真钱。</p>
<p>现在，你需要了解 6 个基本的 Solidity 概念：</p>
<ol>
<li>
<p>在 <code>pragma</code> 语句之后我们有一个 import 语句。这会将现有代码导入我们的智能合约。</p>
<p>这非常有用，因为这是我们重用他人编写的代码并从中受益的方式。你可以查看在此 <a href="https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol">GitHub 链接</a>上导入的代码。</p>
<p>实际上，当我们编译我们的智能合约时，这个导入的代码会被拉入并与它一起编译成字节码。我们马上就会明白为什么我们需要它……</p>
</li>
<li>
<p>单行注释是用<code>//</code>标记的。现在你正在学习多行注释。它们可能跨越一行或多行并使用 <code>/*</code> 和 <code>*/</code> 开始和结束注释。</p>
</li>
<li>
<p>我们声明了一个名为 <code>priceFeed</code> 的变量，它的数据类型为 <code>AggregatorV3Interface</code>。但是这种奇怪的类型是从哪里来的呢？从导入语句中导入的代码中——我们能够使用 <code>AggregatorV3Interface</code> 类型，因为 Chainlink 定义了它。</p>
<p>如果你查看 Github 链接，你会看到该类型定义了一个接口（我们刚刚讨论完接口）。所以 <code>priceFeed</code> 是对 <code>AggregatorV3Interface</code> 类型的某个对象的引用。</p>
</li>
<li>
<p>看一下构造函数。这个构造函数不接受参数，但我们可以很容易地将 ETH/USD 喂价(price feed)的 oracle 智能合约的地址 <code>0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e</code> 作为地址类型的参数传递给它。相反，我们在构造函数中对地址进行硬编码。</p>
<p>但我们也正在创建对 Price Feed Aggregator 智能合约的引用（使用称为 <code>AggregatorV3Interface</code> 的接口）。</p>
<p>现在我们可以调用 <code>AggregatorV3Interface</code> 上可用的所有方法，因为 <code>priceFeed</code> 变量引用该智能合约。事实上，我们接下来要做的是……</p>
</li>
<li>
<p>让我们跳转到函数 <code>getLatestPrice()</code>。你可以从我们在 <code>HotFudgeSauce</code> 中的讨论中认出它的结构，这个函数正在做一些有趣的事情。</p>
<p>在这个 <code>getLatestPrice()</code> 函数中，我们调用了存在于 <code>AggregatorV3Interface</code> 接口中的 <code>latestRoundData()</code> 函数。如果你查看此方法的<a href="https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol#L22">源代码</a>，你会注意到此 <code>latestRoundData()</code> 函数返回 5 种不同类型的整数！</p>
</li>
</ol>
<p><img src="https://lh5.googleusercontent.com/GMqCsBxRblJ2rMQDrMPTx1iDfj6Q3h0eiPqE-RE0-MaHwDDyr6JVna6-57e4jdQxp6VJ4FbMWUMKxFpM2ot4BzqsagLpmD2clz1xCjQhAfl4tn8HhCK4uSKj1hZkDGwXJj9DhuY7nmunj4j_aDx9APc8mqXUPoR9Tl20DdYvjIoUjotmQfJByXKbW2EY5g" alt="Calling methods on another smart contract from our smart contract" width="600" height="400" loading="lazy"></p>
<p>通过我们的智能合约调用其他智能合约的方法</p>
<p>在我们的智能合约中，我们注释掉了 4 个不需要的值。所以这意味着 Solidity 函数可以返回多个值（在这个例子中我们返回了 5 个值），所以我们可以挑选需要的。</p>
<p>另一种使用调用 <code>latestRoundData()</code> 的结果的方法是：<code>( ,int price, , ,) = priceFeed.latestRoundData()</code>，对于 5 个返回值中的 4 个，我们不给它们变量名以忽略它们。</p>
<p>当我们将变量名分配给一个函数返回的一个或多个值时，我们称之为“解构赋值（destructuring assignment）”，因为我们解构返回值（将每个值分开）并在解构时对它们赋值，就像我们对上面的 <code>price</code> 所做的那样。</p>
<p>由于你已经了解了接口，我建议你查看 Chainlink Labs 的 <a href="https://github.com/smartcontractkit/">GitHub repo</a> 以检查 <a href="https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.6/AggregatorProxy.sol#L211"><code>Aggregator</code> 合约</a>中已实现的 <code>latestRoundData()</code> 函数以及 <code>AggregatorV3Interface</code> 如何提供与 <code>Aggregator</code> 合约交互的接口。</p>
<h2 id="what-is-contract-state">什么是合约状态</h2>
<p>在我们继续之前，重要的是要确保你理解将经常要看到的术语。</p>
<p>计算机科学中的“状态(state)”具有<a href="https://en.wikipedia.org/wiki/State_(computer_science)">明确的含义</a>。虽然它会变得非常混乱，但状态的关键在于它指的是程序在运行时“记住”的所有信息。此信息可以改变、更新、删除、创建等。而且，如果你在不同时间为其快照，信息将处于不同的“状态”。</p>
<p>所以状态只是程序的当前快照，在其执行期间的某个时间点 - 它的变量持有什么值，它们在做什么，已经创建或删除了哪些对象，等等。</p>
<p>我们之前已经研究了三种类型的变量——状态变量、局部变量和全局变量。状态变量以及全局变量为我们提供了智能合约在任何给定时间点的状态。因此，智能合约的状态是对以下内容的描述：</p>
<ol>
<li>它的状态变量持有什么值，</li>
<li>区块链相关的全局变量在那个时刻有什么值，以及</li>
<li>智能合约账户余额（如果有的话）。</li>
</ol>
<h2 id="state-mutability-keywords-modifiers-">状态可变性关键字（修饰符：modifier）</h2>
<p>现在我们已经讨论了状态、状态变量和函数，让我们了解一些 Solidity 关键字，这些 Solidity 关键字指定了我们可以对状态执行的操作。</p>
<p>这些关键字称为<a href="https://docs.soliditylang.org/en/v0.8.17/cheatsheet.html#modifiers">修饰符</a>。但并非所有这些都允许你修改状态。事实上，其中许多修饰符明确禁止你修改状态。</p>
<p>以下是你将在真正的智能合约中看到的 Solidity 修饰符：</p>
<table style="border:none;border-collapse:collapse;"><colgroup><col width="128"><col width="141"><col width="355"></colgroup><tbody><tr style="height:0pt"><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;background-color:#666666;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;text-align: center;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#f1f3f4;background-color:transparent;font-weight:700;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">修饰符关键字</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;background-color:#666666;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;text-align: center;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#f1f3f4;background-color:transparent;font-weight:700;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">适用于…</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;background-color:#666666;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;text-align: center;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#f1f3f4;background-color:transparent;font-weight:700;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">目的</span></p></td></tr><tr style="height:0pt"><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;text-align: center;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">constant</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;text-align: center;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">状态变量</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">声明的同时进行赋值。硬编码成代码，它给定的值永远不能改变。</span></p><br><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">当我们知道一个值永远不会改变时使用 - 例如，如果我们永远不会（永远）允许用户购买超过 50 单位的东西，我们可以将 50 声明为常量值（constant）。</span></p></td></tr><tr style="height:0pt"><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;text-align: center;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">immutable</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;text-align: center;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">状态变量</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">这些状态变量是在智能合约的顶部声明的，但在构造时给它们赋值（仅一次！）——即通过构造函数。一旦它们收到值，实际上就变成常量了。并且它们的值实际上存储在代码本身而非存储槽（storage slot）中（storage 将在后面解释）。</span></p></td></tr><tr style="height:0pt"><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;text-align: center;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">view</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;text-align: center;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">函数</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">A你通常会在可见性说明符之后看到它。&nbsp;  </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">view</span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> 修饰符意味着该函数只能“查看”（读取）合约状态，但不能更改它（不能“写入”合约状态）。这实际上是一个只读修饰符。如果该函数需要使用合约状态中的任何值，但不修改该值，则它将是一个 view 函数。</span></p></td></tr><tr style="height:0pt"><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;text-align: center;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;text-align: center;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">函数</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure 函数不允许写入（修改）合约状态，甚至不允许从中读取！它们做的事情不会以任何方式与区块链状态交互。&nbsp; 通常这些可以是辅助函数，可以进行一些计算或将一种数据类型的输入转换为另一种数据类型等。</span></p></td></tr><tr style="height:0pt"><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;text-align: center;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">payable</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;text-align: center;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">函数</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">该关键字使函数能够接收 Eth。没有这个关键字，你就不能在调用函数时发送 ETH。</span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">请注意，在 Solidity 版本 0.8.17 中，有 </span><a href="https://docs.soliditylang.org/en/v0.8.17/080-breaking-changes.html?highlight=payable%20address#new-restrictions" style="text-decoration:none;"><span style="font-size:11pt;font-family:Arial;color:#1155cc;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:underline;-webkit-text-decoration-skip:none;text-decoration-skip-ink:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">重大更新</span></a><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> 允许使用 `payable` 作为数据类型。&nbsp; 具体来说，我们现在可以将 </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">address</span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> 数据类型转化为 </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">payable</span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">address</span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> 数据类型，方式是通过执行类型转换，执行代码类似于 </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">payable(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">0xdCad3a6d3569DF655070DEd06cb7A1b2Ccd1D3AF</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">).</span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">这样做的目的是使给定的以太坊地址成为可支付的，之后我们可以将 ETH 发送到该地址。&nbsp;</span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">请注意，payable 的这种用法是一种类型转换，与函数修饰符不同，尽管使用了相同的关键字。稍后我们将介绍地址类型，但是你可以先阅读相关内容</span><a href="https://docs.soliditylang.org/en/v0.8.16/types.html#address" style="text-decoration:none;"><span style="font-size:11pt;font-family:Arial;color:#1155cc;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:underline;-webkit-text-decoration-skip:none;text-decoration-skip-ink:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">在此处</span></a><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">.</span></p></td></tr><tr style="height:0pt"><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;text-align: center;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">virtual</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;text-align: center;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">函数</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">这是一个稍微高级的话题，在继承部分有详细介绍。这个修饰符允许函数在从它继承的子合约中被“覆盖”。&nbsp; 换句话说，一个带有关键字 </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#2f9b00;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">virtual</span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> 的函数可以在另一个继承自这个合约的子合约中用不同的内部逻辑“重写”。</span></p></td></tr><tr style="height:0pt"><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;text-align: center;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">override</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">函数</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">这是 </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">virtual</span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> 修饰符的反面。&nbsp; 当子合约“重写”在它继承的基础合约（父合约）中声明的函数时，它会用 </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">override</span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> 来标记重写的函数，以表明其实现覆盖了父合约中给出的函数。如果父合约的 </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">virtual</span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> 函数没有被子合约覆盖，则父合约的逻辑将应用于子合约。</span></p></td></tr><tr style="height:0pt"><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;text-align: center;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">indexed</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">事件</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">我们将在本手册后面介绍事件（event）。它们是由智能合约“发出”的小数据包，通常是为了响应发生的值得注意的事件。&nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#2f9b00;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">indexed</span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> 关键字表示事件中包含的其中一条数据应存储在区块链中，以供以后检索和过滤。一旦我们在本手册后面了解了事件和日志记录，你就会了解它的意义。</span></p></td></tr><tr style="height:0pt"><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;text-align: center;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">anonymous</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">事件</span></p></td><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">这里的 </span><a href="https://docs.soliditylang.org/en/v0.8.17/cheatsheet.html#modifiers" style="text-decoration:none;"><span style="font-size:11pt;font-family:Arial;color:#1155cc;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:underline;-webkit-text-decoration-skip:none;text-decoration-skip-ink:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">文档</span></a><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> 说“不将 event signature 存储为 topic”，这对你来说可能意义不大。但是关键字确实表明它正在使 event 的某些部分“匿名”。因此，一旦我们理解了本手册后面的 event 和 topic，你就知道它的意义了。</span></p></td></tr></tbody></table>
<p>请注意，不是存储变量的变量（即在给定函数范围内声明和使用的局部变量）不需要状态修饰符。这是因为它们实际上并不是智能合约状态的一部分。它们只是该函数内部局部状态的一部分。那么根据定义，它们是可修改的，不需要对其可修改性进行控制。</p>
<h2 id="data-locations-storage-memory-and-stack">数据存储类型 – storage/memory/stack</h2>
<p>在以太坊和基于 EVM 的链上，系统内的数据可以在多个“数据位置”存储以被访问。</p>
<p>数据存储位置是 EVM 基本设计和架构的一部分。当你看到 “memory”、“storage” 和 “stack” 等词时，你应该开始思考“数据存储位置”——即数据可以存储（写入）和从中检索（读取）的位置。</p>
<p>数据位置会影响代码在运行时的执行方式。除此以外，它对智能合约在部署和运行期间使用的 <a href="https://ethereum.org/en/developers/docs/gas/">gas</a> 数量也有非常重要的影响。<br>
&nbsp;<br>
gas 的使用需要对 EVM 和称为操作码（opcode）的东西有更深入的了解——我们可以暂时搁置这个讨论。虽然有用，但并不是你了解数据存储位置的充分条件。</p>
<p>虽然到目前为止我已经提到了 3 个 <em>数据位置</em> ，但还有 2 种其他方式可以在智能合约中存储和访问数据：“calldata” 和 “code”。但这些不是 EVM 设计中的数据位置。它们只是提到过的 3 个数据位置的子集。</p>
<p>让我们从 storage 开始。在 EVM 的设计中，需要永久存储在区块链上的数据被放置在相关智能合约的“storage”区域。这包括任何合约“状态变量”。</p>
<p>由于存储将数据永久保存在区块链上，因此所有数据都需要在网络中的所有节点之间同步，这就是节点必须就数据状态达成共识的原因。这种共识使存储使用起来很昂贵。</p>
<p>你已经看到了存储变量（也称为合约状态变量）的示例，但这里是取自 <a href="https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/VRFConsumerBaseV2.sol">Chainlink VRF（可验证随机数）的 consumer 智能合约</a>的示例</p>
<p><img src="https://lh3.googleusercontent.com/oV_AXHhYqs7DIB_WQvzB7A97qlnVKAoQYAvLCr9euiwC-XlO8d23-HZuwGjkANpEBnMKQ8u1MrBupg8IWjdX9_YubpjQobML8AyKAQ9vnU_nBxgTAxlYHA-JJlc1xZ3wD14zzER6gAyKmkQ7yLQDCKOr2V5M48Xo5t2SfYVoPRJKI_lUJ5yJMh8YSNaT9w" alt="Storage data location. Putting data in the contract's storage layout." width="600" height="400" loading="lazy"></p>
<p>Storage 数据。将数据放入合约的存储布局中。</p>
<p>创建和部署上述合约时，传递给合约构造函数的任何地址都会永久存储在智能合约的 storage 中，并且可以使用变量 <code>vrfCoodinator</code> 访问。由于此状态变量被标记为<code>immutable</code>，因此在此之后无法更改。</p>
<p>回忆一下上一节关于关键字，我们在上一节讨论了 <code>immutable</code> 变量和 <code>constant</code> 变量，这些值没有放在 storage 里面。在构造合约时，它们成为代码本身的一部分，因此这些值不会像 storage 变量那样消耗那么多的 gas。</p>
<p>现在让我们看 <code>memory</code>。这表示临时存储，你可以在其中读取和写入智能合约运行期间所需的数据。一旦使用该数据的函数执行完毕，该数据将被擦除。</p>
<p><code>memory</code> 位置空间就像一个临时记事本，每次触发函数时都会在智能合约中提供一个新的，执行完成后，该临时记事本将被删除。</p>
<p>在理解 storage 和 memory 的区别时，您可以将 storage 视为传统计算世界中的一种硬盘，因为它具有“持久”存储数据的意义。但 memory 在传统计算中更接近 RAM。</p>
<p><code>堆栈(stack)</code>是执行大部分 EVM 计算的数据区域。 EVM 遵循<a href="https://en.wikipedia.org/wiki/Stack_machine#Comparison_with_register_machines">基于 stack 的计算模型</a>，而不是基于寄存器的计算模型，这意味着要执行的每个操作都需要使用<a href="https://en.wikipedia.org/wiki/Stack_(abstract_data_type)">stack 数据结构</a>进行存储和访问。</p>
<p>stack 的深度——即它可以容纳的项目总数——是 1024，stack 中的每个项目可以是 256 位（32 字节）长。这与存储数据位置中每个键和值的大小相同。</p>
<p>你可以在<a href="https://docs.soliditylang.org/en/v0.8.17/introduction-to-smart-contracts.html#storage-memory-and-the-stack">此处</a>详细了解 EVM 如何控制对 stack 数据存储区域的访问。</p>
<p>接下来说说 <code>calldata</code>。我假设你对以太坊智能合约<a href="https://ethereum.org/en/whitepaper/#messages">消息</a>和<a href="https://ethereum.org/en/developers/docs/transactions/#:~:text=An%20Ethereum%20transaction%20refers%20to,takes%20place%20within%20a%20transaction.">交易</a>有基本的了解。如果你没有，请先阅读这些链接。</p>
<p>消息和交易是调用智能合约函数的方式，它们包含执行这些函数所需的各种数据。此消息数据存储在 <code>calldata</code> 中，<code>calldata</code> 是 memory 只读部分，其中包含函数名称和参数等内容。</p>
<p>这与外部可调用函​​数相关，因为 internal 函数和 private 函数不使用 calldata。calldata 仅存储要被“传入”函数执行的数据和函数参数。</p>
<p>请记住，<code>calldata</code> 是内存，只是 <code>calldata</code> 是只读的。你不能向其中写入数据。</p>
<p>最后，<code>代码(code)</code>不属于以上的任何一个存储类型，而是指智能合约的编译字节码，它被永久部署和存储在区块链上。该字节码存储在不可变的 ROM（只读存储器）中，其中加载了要执行的智能合约的字节码。</p>
<p>还记得我们如何讨论 Solidity 中 immutable 变量和 constant 变量之间的区别吗？immutable 值被赋值一次（通常在构造函数中），constant 变量的值被硬编码到智能合约代码中。因为它们是硬编码的，常量值按字面编译并直接嵌入到智能合约的字节码中，并存储在这个代码/ROM 中。</p>
<p>和 <code>calldata</code> 一样，<code>code</code>也是只读的——如果你理解了上一段，你就会明白为什么！</p>
<h2 id="how-typing-works">数据类型原理</h2>
<p>类型是编程中一个非常重要的概念，因为它是我们为数据提供结构的方式。从该结构中，我们可以以安全、一致和可预测的方式对数据运行操作。</p>
<p>当一种语言具有严格类型时，这意味着该语言严格定义了每条数据的类型，并且不能为具有类型的变量赋予另一种类型。</p>
<p>换句话说，在严格类型语言中：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">int a =</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">1</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// &nbsp; &nbsp;这里的 1 是整数类型</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">string b= </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"1"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">//&nbsp; 这里的 1 是字符串类型</span></p><br><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">b=a </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">//&nbsp; 非法! b 是一个字符串，它不能承载整数类型，同理 a 也一样！</span></p></td></tr></tbody></table>
<p>但是在没有类型的 JavaScript 中，<code>b=a</code> 也成立——这使得 JavaScript 成为“动态类型”。</p>
<p>同样，在静态类型的语言中，你不能将整数传递给需要字符串的函数。但是在 JavaScript 中，我们可以将任何东西传递给函数，程序仍然可以编译，但在执行程序时可能会抛出错误。</p>
<p>例如这个函数：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">add</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">a,b</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">){</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> a + b</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">add(</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">1</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">, </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">2</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 输出是 3，整数类型</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">add(</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">1</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">, </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"2"</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// “2” 是一个字符串，而不是整数，所以输出变成了字符串“12” (!?)</span></p></td></tr></tbody></table>
<p>可以想象，这会产生一些很难发现的错误。尽管它会产生意想不到的结果，但是代码编译甚至可以执行都不会失败。</p>
<p>但是强类型语言永远不会让你传递字符串“2”，因为函数会坚持它接受的类型。</p>
<p>让我们看看这个函数是如何用像 Go 这样的强类型语言编写的。</p>
<p><img src="https://lh5.googleusercontent.com/KeWnVGJ3xZB9cJGLGVqM3XndabWGp5WeIbSGGn78QGzyTgZoRR6QWYJcyYw2jn150w9Y9qvZM62IW-Seeh_TjbyTmBYcnzZH5vBpCkUsBSVmgPuzFsa3q-UORkOEQZujoPa4EojQl5gWynWac5PVuNzOpS1OR6yGO_A31RsAFXIsRqJfJZ9HZRpdDW0oCQ" alt="How typing works in syntax, using Golang for illustration purposes" width="600" height="400" loading="lazy"></p>
<p>通过 go 语言来说明数据类型的工作原理</p>
<p>如果传递一个 <code>string</code>（即使它代表一个数字），就会阻止程序编译（构建）。你会看到这样的错误：</p>
<pre><code class="language-go">./prog.go:13:19: cannot use "2" (untyped string constant) as int value in argument to add

Go build failed.
</code></pre>
<p><a href="https://go.dev/play/p/SrE1CXVD_pj">可以亲自尝试一下</a>!</p>
<p>所以类型很重要，因为对于人类来说似乎相同的数据可能会被计算机以非常不同的方式获得。这可能会导致一些非常奇怪的错误，程序崩溃，甚至是严重的安全漏洞。</p>
<p>类型还使开发人员能够创建自己的自定义类型，然后可以使用自定义属性和操作对其进行编程。</p>
<p>有了类型系统，人类便可以通过询问“此数据的类型是什么，它应该能够做什么？”这样的问题来推理数据，并且机器可以完全按照预期进行操作。</p>
<p>这是另一个例子，说明在你我看来相同的数据可能如何被处理器以截然不同的方式解释。取二进制数字序列（即数字只能有 0 或 1 的值，这是处理器使用的<a href="https://www.mathsisfun.com/binary-number-system.html">二进制系统</a>）<code>1100001010100011</code>。</p>
<p>对于人类来说，使用十进制系统看起来是一个非常大的数字——也许是 11 gazillion 之类的。</p>
<p>但是对于二进制的计算机来说，它不是 11 gazillion 之类的东西。计算机将其视为一个 16 位序列（二进制数字的缩写），在二进制中这可能意味着正数（无符号整数）49,827 或带符号整数 -15,709 或英镑符号 £ 的 UTF-8 表示或其他不同的东西！</p>
<p><img src="https://lh6.googleusercontent.com/zH0CD3sU2PRWVf0rdtAsvHgV7-7PiyOdKZbz6wOEkE2gax4KeMd7EswERIAR1iEbn_NhI-2P381GDujQ27-o-XiHIyB_lY7pjUKIK7XP-nqzLAlYNdNErYXRYuVPdKsiD4n0wSqtnxcwwP78OAYAUbZ4mFpe9hKSewPymjrSyjaUlIO2Ap28ftinejnLNw" alt="A sequence of bits can be interpreted by a computer to have very different meanings" width="600" height="400" loading="lazy"></p>
<p>计算机可以将一系列的二进制位解释为很多不同的含义（<a href="https://livebook.manning.com/book/programming-with-types/chapter-1/18">来源</a>）</p>
<p>所以所有这些解释都是在说类型很重要，并且类型可以“内置”到一种语言中，<strong>即使</strong>该语言不严格强制类型，如 JavaScript。</p>
<p>JavaScript 具有<a href="https://www.w3schools.com/js/js_datatypes.asp">内置类型</a>，如数字(numbers)、字符串(strings)、布尔值(booleans)，对象(objects)和数组(arrays)。但正如我们所看到的，JavaScript 并不像 Go 这样的静态类型语言一样对于数据类型严格。</p>
<p>现在回到 Solidity。 Solidity 在很大程度上是一种静态类型的语言。声明变量时，还必须声明其类型。更进一步，如果你尝试将字符串传递给需要整数的函数，Solidity 将直接拒绝编译。</p>
<p>事实上，Solidity 对类型非常严格。例如，不同类型的整数也可能无法编译，如下例所示，其中函数 <code>add()</code> 需要一个无符号整数（正），并且只会与该数字相加，因此始终返回一个正整数。但是返回类型指定为 <code>int</code>，这意味着它可以是正数或负数！</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">add</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">uint256 a</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">int256</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">){</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> a + </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">10</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">//</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> solidity 编译器会报错:</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">//</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> TypeError: Return argument type uint256 is not implicitly convertible to expected type (type of first return variable) int256.</span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">r&nbsp;</span></p></td></tr></tbody></table>
<p>因此，尽管输入和输出都是 256 位整数，但函数只接收无符号整数这一事实，就会使编译器抱怨无符号整数类型不能隐式转换为有符号整数类型。</p>
<p>以上对类型的控制相当严格！开发人员可以通过将 return 语句重写为 <code>return int256(a + 10)</code> 来强制转换（称为类型转换）。但是这种行动需要考虑一些问题，这超出了我们在这里讨论的范围。</p>
<p>现在，请记住 Solidity 是静态类型的，这意味着在代码中声明每个变量时必须明确指定它们的类型。你可以组合类型以形成更复杂的复合类型。接下来，我们讨论一些内置类型。</p>
<h2 id="solidity-data-types">Solidity 数据类型</h2>
<p>内置于语言中并且可以“开箱即用”的类型通常被称为“原语（primitive）”。它们是语言固有的。你可以组合 primitive 数据类型以形成更复杂的数据结构，这些数据结构成为“自定义（custom）”数据类型。</p>
<p>例如，在 JavaScript 中，<a href="https://developer.mozilla.org/en-US/docs/Glossary/Primitive">primitive</a>不是 JS 对象并且也没有方法或属性的数据。 JavaScript 中有 7 种基本数据类型：<code>string</code>、<code>number</code>、<code>bigint</code>、<code>boolean</code>、<code>undefined</code>、<code>symbol</code> 和 <code>null</code>。</p>
<p>Solidity 也有自己的 primitive 数据类型。有趣的是，Solidity 没有“undefined”或“null”。相反，当你声明一个变量及其类型，但不为其分配值时，Solidity 将为该类型分配一个<a href="https://docs.soliditylang.org/en/v0.8.17/control-structures.html#default-value">默认值</a>。该默认值究竟是什么，取决于数据类型。</p>
<p>Solidity 的许多 primitive 数据类型都是相同“基本”类型的变体。例如，<code>int</code> 类型本身具有子类型，而子类型就基于 <code>integer</code> 可以存储的二进制位。</p>
<p>如果这让你有点困惑，请不要担心 - 如果你不熟悉位和字节，这并不容易，我将很快介绍整数。</p>
<p>在我们探索 Solidity 类型之前，你必须了解另一个非常重要的概念 - 它是编程语言中许多错误和“意外陷阱”的来源。</p>
<p>这就是值类型（value type）和引用类型（reference type）之间的区别，以及程序中数据“按值传递（pass by value）”与“按引用传递（pass by reference）”之间的区别。我将在下面进行快速总结，但你还可以在继续之前观看<a href="https://www.youtube.com/watch?v=1HHjKG1372E">这段简短的视频</a>。</p>
<h3 id="vs">按引用传递 vs 按值传递</h3>
<p>在操作系统级别，当程序运行时，程序在执行期间使用的所有数据都存储在计算机 RAM（内存）中的位置。当你声明一个变量时，操作系统会分配一些内存空间来保存该变量的数据，这些存储空间会分配给或最终分配给该变量的值。</p>
<p>还有一种数据，就是常说的“指针”。该指针指向可以找到该变量及其值的内存位置（计算机 RAM 中的“地址”）。因此，指针实际上包含了对计算机内存中数据所在位置的引用。</p>
<p>因此，当你在程序中传递数据时（例如，当你将值分配给新变量名称时，或者当你将输入（参数）传递给函数或方法时，语言的编译器可以通过两种方式实现这一点。它可以通过指向计算机内存中数据位置的指针，<strong>或者</strong>它可以复制数据本身，并传递实际值。</p>
<p>第一种方法是“通过引用传递”。第二种方法是“按值传递”。</p>
<p>Solidity 的数据类型基元分为两类——它们要么是值类型（value type），要么是引用类型（reference type）。</p>
<p>换句话说，在 Solidity 中，当你传递数据时，数据的类型将决定你传递的是值的副本还是对值在计算机内存中位置的引用。</p>
<p><img src="https://lh6.googleusercontent.com/Nj0wt2rDWoanEGT3onBaNxJEnoBp-7NXBrXdL07SO1qbj6PYiE-fhz2zGId-AuGqLiMMHCJIwYbEMYp5oKebmhMudnOhhM_-ZRkLBiatMR6zgcpAPbMDl4hqlLr4UJYa061Plha044IqX_pqeWnx5V1P53dYxopqlddIFmj7qHnXAclPKdLy4XYuwkeGlA" alt="Value Types and Reference Types in Solidity" width="600" height="400" loading="lazy"></p>
<p>Solidity 中值类型（Value Types）和引用类型（Reference Types）</p>
<p>在 Solidity 的“值类型”中，整数分为两类——<code>uint</code> 是无符号的（只有正整数，所以它们没有正负号）和 <code>int</code> 是有符号的（可以是正数也可以是负数，如果你把它们写下来，它们有加号或减号）。</p>
<p>整数类型还可以指定它们有多少位长 - 或者有多少位用于表示 <code>integer</code>。</p>
<p><code>uint8</code> 是由 8 个二进制数字（位）表示的整数，最多可以存储 256 个不同的值 (2^8=256)。由于 <code>uint</code> 用于无符号（正）整数，这意味着它可以存储从 0 到 255（不包括 1 到 256）的值。</p>
<p>但是，当你使用带符号的整数（如 <code>int8</code>）时，其中一位将用于表示它是正数还是负数。这意味着我们只剩下 7 位，因此我们最多只能表示 2^7 (128) 个不同的值，包括 0。因此 <code>int8</code> 可以表示从 -127 到 +127 的任何值。</p>
<p>通过扩展，<code>int256</code> 的长度为 256 位，可以存储 +/- (2^255) 值。</p>
<p>位长度是 <a href="https://docs.soliditylang.org/fr/latest/types.html#integers">8 的倍数</a>（因为 8 位构成一个字节），因此你可以使用 <code>int8</code>、<code>int16</code>、<code>int24</code> 等一直到 256（32 字节）。</p>
<p>地址指的是以太坊账户类型——智能合约账户或外部拥有账户（又名“EOA”。你的 Metamask 钱包代表一个 EOA）。所以地址也是 Solidity 中的一种类型。</p>
<p>地址的默认值（如果你声明一个类型地址的变量但不分配任何值，则将具有的值）为<code>0x00000000000000000000000000000000000000000000000000</code>，这也是此表达式所代表的值：<code>address（0）</code>。</p>
<p>布尔值表示真值还是假值。最后，我们有<a href="https://docs.soliditylang.org/en/v0.8.17/types.html#fixed-size-byte-arrays">固定大小的字节数组</a>，如 <code>bytes1</code>、<code>bytes2</code> … <code>bytes32</code>。这些是包含字节的固定长度数组。所有这些类型的值在代码中传递时都会被复制。</p>
<p>对于“引用类型”，我们有数组，它们可以在声明时指定固定大小，或者动态大小的数组。虽然它们声明时大小是固定的，但其大小可以“调整”，因为数组中元素的数量会增加。</p>
<p>字节是一种底层数据类型，指的是编码为二进制格式的数据。编译器最终将所有数据还原为二进制形式，以便 EVM（或者在传统计算中，处理器）可以使用它。</p>
<p>与其他更易读的数据类型相比，存储和使用字节通常更快、更高效。</p>
<p>你可能想知道为什么我没有在上图中的任何一种数据类型中引用字符串。那是因为在 Solidity 中，字符串实际上是动态大小的数组，数组存储以 UTF-8 编码格式编码的字节序列（只是二进制数）。</p>
<p>它们不是 Solidity 中的原语（primitive）。在 JavaScript 中，它们是原语，但即使在 JavaScript 中，字符串也类似于（但不相同）数组，并且是<a href="https://tc39.es/ecma262/#sec-ecmascript-language-types-string-type">一系列以 UTF-16 编码的整数值</a>。</p>
<p>在智能合约中将 <code>string</code> 存储为 <code>bytes</code> 类型通常更高效，因为 <code>string</code> 和 <code>bytes</code> 之间的转换非常容易。因此，将 <code>string</code> 存储为 <code>bytes</code> 但在函数中将它们作为 string 返回是很有用的。你可以在下面看到一个示例：</p>
<table style="border:none;border-collapse:collapse;"><colgroup><col width="737"></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// SPDX-License-Identifier: MIT</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pragma solidity ^</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">0.8.0</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract StringyBytes {</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 传入字符串 “Zubin” 时返回 0x5a7562696e</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">stringIntoBytes</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">string memory input</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">bytes memory </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">){</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> bytes(input);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 传入字符串 “0x5a7562696e” 时返回 "Zubin"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">bytesIntoString</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">bytes memory input</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">string memory </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">){</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> string(input);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>除了 Solidity 字符串，<code>bytes</code> 数据类型是一个<a href="https://docs.soliditylang.org/en/v0.8.17/types.html#dynamically-sized-byte-array">动态大小的字节数组</a>。此外，与其他固定大小字节数组不同，它是一种引用类型。 Solidity 中的 <code>bytes</code> 类型是“array of bytes”的简写，在程序中可以写成 <code>bytes</code> 或 <code>byte[]</code>。</p>
<p>如果你对字节和字节数组感到困惑……我表示同情。</p>
<p>字符串和字节数组的底层细节与本手册不太相关。现在的重点是一些数据类型通过引用传递，而另一些数据类型通过复制它们的值来传递。</p>
<p>可以认为没有指定大小的 Solidity 字符串和字节是引用类型，因为它们都是动态大小的数组。</p>
<p>最后，在 Solidity 的原语中，我们有<code>结构体（structure）</code>和<code>映射（mapping）</code>。有时这些被称为“复合”数据类型，因为它们是由其他原语组成的。</p>
<p><code>struct</code> 将一段数据定义为具有一个或多个属性，并指定每个属性的数据类型和名称。结构使你能够定义自己的自定义类型，以便你可以将数据片段组织和收集到一个更大的数据类型中。</p>
<p>例如，你可以拥有定义 <code>Person</code> 的结构，如下所示：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">struct Person {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; string name;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; uint age;</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;bool isSolidityDev;</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;Job&nbsp; job </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;// Person 结构体包含一个自定义数据类型 Job</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p><br><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">struct Job {</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;string employer;</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;string department;</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;boolean isRemote;</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p><br></td></tr></tbody></table>
<p>你也可以通过下面的方法初始化 <code>Person</code> 结构体:</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// Job 结构体是没有初始化的</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 这意味着它的属性都是默认值</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Person memory p;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">P.name = </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"Zubin"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">p.age = </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">41</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">p.isSolidityDev = </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">true</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 或者通过调用函数一样的方式来初始化结构体</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Person p =&nbsp; Person(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"Zubin"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">,&nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"41"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">, </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"true"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">, Job(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"Chainlink Labs"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">, </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"DevRel"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">, </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">true</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">));</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 或者通过键值对的方式</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Job j = Job({ employer: </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"Chainlink Labs"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">, </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"DevRel"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">,&nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">true</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">});</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">p.job = j </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// this is done in dot notation style.</span></p></td></tr></tbody></table>
<p>映射（mapping）类似于哈希表（hashtable）、字典（dictionary）或 JavaScript 对象（object）和映射（map），但功能少一些。</p>
<p><code>mapping</code> 也是一个键值对，但是键的数据类型有限制，你可以在<a href="https://docs.soliditylang.org/en/v0.8.17/types.html#mapping-types">这里</a>查看。与映射键关联的数据类型可以是任何原语、结构，甚至其他映射。</p>
<p>以下是映射的声明、初始化、写入和读取方式——以下示例来自 <a href="https://github.com/smartcontractkit/LinkToken/blob/f307ea6d4c02dd87ea4c1f1032e3e384ceac20c2/contracts/v0.4/token/linkStandardToken.sol">Chainlink Link Token 智能合约源代码</a>。</p>
<p><img src="https://lh5.googleusercontent.com/5x3v-NtBWU2gejrQDWU1m-D-9ZXzTi5JLDs2bMCq1d-TVoxCjFmTEEQg1af4pxEfRbrrpnQaPJLcVauLOZ-amT2hDuNhvXVuX9-cMdMWPiGiRblgcC-oX9dkt75uHzO1ieBVFl_MA6NtJMmWZQ39ypOhOE4qHQWEzk410vme9TR2_lvEzGI2O9_NPo2JGg" alt="Declaring and using the Mappings type in Solidity" width="600" height="400" loading="lazy"><br>
在 Solidity 中声明和使用映射</p>
<p>如果你尝试使用映射中不存在的键访问一个值，它将返回存储在映射中的类型的<a href="https://docs.soliditylang.org/en/v0.8.17/control-structures.html#default-value">默认值</a>。</p>
<p>在上面的例子中，<code>balances</code> 映射中所有值的类型都是 <code>uint256</code>，它的默认值为 <code>0</code>。所以如果我们调用 <code>balanceOf()</code> 并传入一个没有任何 LINK 通证的地址，我们将会得到 <code>0</code> 值。</p>
<p>在这个例子中这一设置是合理的，但是当我们想要找出一个键是否存在于映射中时，它可能有点棘手。</p>
<p>目前没有办法枚举映射中存在哪些键（也就是说，没有与 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys">JavaScript 的 <code>Object.keys()</code></a> 方法等效的方法）。使用键检索只会返回与数据类型关联的默认值，这并不能清楚地告诉我们该键是否实际存在。</p>
<p>映射有一个有趣的“陷阱”。与你可以将键值数据结构作为参数传递给函数的其他语言不同，Solidity 不支持将映射作为参数传递给函数，除非函数可见性被标记为 <code>internal</code>。因此，你无法编写接受键值对作为参数的外部或公共可调用函数。</p>
<h2 id="how-to-declare-and-initialize-arrays-in-solidity">如何在 Solidity 中声明和初始化数组</h2>
<p>Solidity 有两种类型的数组，因此了解声明和初始化它们的不同方式是很有必要的。</p>
<p>Solidity 中的两种主要数组类型是固定大小数组和动态大小数组。</p>
<p>为了强化你的记忆，请回忆前几节内容。固定大小的数组按值传递（在代码中传递时复制），动态大小的数组按引用传递（指向内存地址的指针在代码中传递）。</p>
<p>它们的语法和容量（大小）也不同，这决定了我们何时使用其中一个与另一个。</p>
<p>这是固定大小的数组在声明和初始化时的样子。它的固定容量为 6 个元素，一旦声明就不能更改。 6 个元素的数组的内存空间已分配且无法更改。</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">string[</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">6</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">] fixedArray; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 最大空间是 6 个元素。</span></p><br><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">fixedArray[0] = ‘</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">a’; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 第 1 个元素设置为 ‘a’</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">fixedArray[4]=</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">‘e’</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 第 5 个元素设置为 ‘e’</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">fixedArray.push(‘</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">f’</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">)</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> // 不能这么做. 固定大小的数组不能使用 push() 函数</span></p><br><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">fixedArray[5]=</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">‘f’</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 第 6 个元素设置为 ‘f’</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">fixedArray[6]=</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">‘g’</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 不能这么做，&nbsp; 超出了固定的大小</span></p></td></tr></tbody></table>
<p>也可以通过使用以下语法声明一个固定大小的数组，声明中包含变量名，数组的大小及其元素的类型：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// datatype arrayName[arraySize];</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">string</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> myStrings[10]; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 大小为 10 的字符串数组</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">myStrings[0] = “chain.link”;</span></p></td></tr></tbody></table>
<p>与其不同，按如下方式声明和初始化的动态大小数组，它的容量是不确定的，这样你就可以使用 push() 方法添加元素：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">uint[] dynamicArray;</span></p><br><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// Push：在数组末尾增加一个值</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 数组的长度会加 1</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">dynamicArray.push(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">1</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">dynamicArray.push(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">2</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">dynamicArray.push(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">3</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// dynamicArray 现在是 [1,2,3]</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">dynamicArray.length; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 3</span></p><br><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// Pop：删掉最后一个元素</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 数组的长度会减 1</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">uint lastNum = dynamicArray.</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pop</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">()&nbsp;</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">dynamicArray.length;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> // 2</span></p><br><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// delete 关键字将指定索引的值重制回默认值</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">delete</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> dynamicArray[</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">1</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">];</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> // 第二个元素不再是 2，而是 0 了</span></p></td></tr></tbody></table>
<p>你还可以在同一行代码中声明和初始化数组的值。</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">string[</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">3</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">] fixedArray = [</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"a"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">, </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"b"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">, </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"c"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">]; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 固定大小的字符串数组</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">fixedArray.push(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"abc"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">); </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 不会成功，因为是固定长度的数组</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">String</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">[] dynamicArray =[</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"chainlink"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">, </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"oracles"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">]; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">/// 动态大小的数组</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">dynamicArray.push(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"rocks"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">);&nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 会成功.</span></p></td></tr></tbody></table>
<p>这些数组是在 storage 中存储的。但是，如果你只需要函数内的临时数组（存储在 memory）怎么办？在这种情况下，有两条规则：只允许使用固定大小的数组，并且必须使用 <code>new</code> 关键字。</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">inMemArray</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">string memory firstName, string memory lastName</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">)</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">string[] memory</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">)</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; {</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 在 memory 中创建一个长度为 2 的固定大小数组</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; string[] </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">memory</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> arr = </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">new</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> string[](</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">2</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; arr[</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">0</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">] = firstName;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; arr[</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">1</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">] = lastName;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> arr;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span></p></td></tr></tbody></table>
<p>显然，有几种方法可以声明和初始化数组。当你想要对 gas 和计算进行优化时，你需要仔细考虑需要哪种类型的数组、它们的容量是多少，以及它们是否可能在没有上限的情况下增长。</p>
<p>这也会影响你的代码设计并受其影响——你是需要数组存储在 storage 还是 memory 中。</p>
<h2 id="what-are-function-modifiers">函数修饰符（function modifier）是什么</h2>
<p>在编写函数时，我们通常会收到一些输入，我们需要在处理其余“业务”逻辑之前对这些输入进行某种验证、检查或运行其他逻辑。</p>
<p>例如，如果你使用纯 JavaScript 编写，你可能想要检查您的函数接收的是整数而不是字符串。如果它在后端，你可能需要检查 POST 请求是否包含正确的身份验证标头和密码。</p>
<p>在 Solidity 中，我们可以通过声明一个称为修饰符（modifier）来执行这些类型的验证步骤，修饰符是是一个类似函数的代码块。</p>
<p>修饰符是一段代码，可以在运行主函数（即应用了修饰符的函数）之前或之后自动运行。</p>
<p>修饰符也可以从父合约继承。它是避免重复代码的一种方法，方法是提取通用功能放入修饰符中，而修饰符可以在整个代码库中重用。</p>
<p><a href="https://docs.soliditylang.org/en/v0.8.17/contracts.html#modifiers">修饰符</a>看起来很像函数。观察修饰符的关键是 <code>_</code>（下划线）出现的位置。该下划线就像一个“占位符”，指示主函数何时运行，可以认为是在当前下划线所在的位置插入了主函数。</p>
<p>因此，在下面的修饰符代码中，我们运行条件检查以确保消息发送者是合约的所有者（owner），然后我们运行调用此修饰符的函数的其余部分。请注意，单个修饰符可以由任意数量的函数使用。</p>
<p><img src="https://lh4.googleusercontent.com/Suv8g0wpHjPOgWCljmrlorL4CcqBrYS2JQWMjIpGMleXCSayt7P7dzobb_5G5sueR_v6LZu9R_RLPFWjBtgLAV9MlLIe-3zw5sG6FgfjdJUf3B994xhkZxTcTJsYx1RSIplKYZpeQhX85HG-Er6ZFu7hlBXOT9QGrmQzY7AoZFB6pzUm6s2peDf4OKkUcQ" alt="How function modifiers are written, and the role of the underscore symbol" width="600" height="400" loading="lazy"></p>
<p>函数修饰符怎么写，以及下划线符号的作用</p>
<p>在此示例中，<code>require()</code> 语句在下划线 (<code>changeOwner()</code>) 之前运行，通常来说，可以通过这样的方式来确保只有当前所有者（owner）才能更改谁拥有合约。</p>
<p>如果你切换修饰符的顺序并且 <code>require()</code> 语句排在第二位，那么 <code>changeOwner()</code> 中的代码将首先运行。在那之后 <code>require()</code> 语句才会运行，那将是一个错误！</p>
<p>修饰符也可以接受输入——你只需将输入的数据类型和名称传递给修饰符。</p>
<table style="border:none;border-collapse:collapse;"><colgroup><col width="711"></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">modifier</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> validAddress(address addr) {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 地址不应为空（即 address(0)）</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">require</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(addr != address(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">0</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">), </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"Address invalid"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;// 继续执行剩下的逻辑</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; _;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">transferTokenTo</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">address someAddress</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">validAddress</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">someAddress</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// do something....</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>修饰符是一个很方便的封装逻辑片段的方式，这些逻辑片段可以在你的 dApp 中的各种智能合约中重复使用。重用逻辑会使你的代码更易于阅读、维护和推理——因此遵循 <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a>（Don't Repeat Yourself 不要重复自己）原则。</p>
<h2 id="error-handling-in-solidity-require-assert-revert">Solidity 中的异常处理 - require/assert/revert</h2>
<p>Solidity 中的错误处理可以通过几个不同的关键字和操作来实现。</p>
<p>当出现错误时，EVM 将恢复对区块链状态的所有更改。换句话说，当抛出异常并且未在 try-catch 块中捕获时，该异常将在被调用的方法的堆栈中“冒泡”, 并返回给用户。当前调用（及其子调用）中对区块链状态所做的所有更改都将被撤销。</p>
<p>在诸如 <code>delegatecall</code>、<code>send</code>、<code>call</code> 等底层函数中有一些例外，其中错误会将布尔值 <code>false</code> 返回给调用者，而不是冒出一个错误。</p>
<p>作为开发人员，你可以采用三种方法来处理和抛出错误：<code>require()</code>、<code>assert()</code> 或 <code>revert()</code>。</p>
<p>require 语句检查你指定的布尔条件，如果为假，它将抛出带有你提供的字符串或没有说明（如果没有指定）的错误：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">requireExample</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">require</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(msg.value &gt;= </span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">1</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> ether, </span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"you must pay me at least 1 ether!"</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">);</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>在继续我们的代码逻辑之前，我们使用 <code>require()</code> 来验证输入、验证返回值和检查其他条件。</p>
<p>在此示例中，如果函数的调用者未发送至少 1 个 ETH，该函数将恢复并抛出一条错误消息：“你必须至少支付 1 个 ETH！”。</p>
<p>你想要返回的错误字符串是 <code>require()</code> 函数的第二个参数，但它是可选的。没有它，你的代码将抛出一个没有数据的错误——如果没有数据的话，就不会很有帮助。</p>
<p><code>require()</code> 的好处是它会返回未使用的 gas，但在 <code>require()</code> 语句之前使用的 gas 将丢失。这就是我们应该尽早使用 <code>require()</code> 的原因。</p>
<p><code>assert()</code> 函数与 <code>require()</code> 非常相似，只是<a href="https://docs.soliditylang.org/en/v0.8.17/control-structures.html#panic-via-assert-and-error-via-require">它抛出类型为 <code>Panic(uint256)</code> 而不是 <code>Error(string)</code> 的错误</a>。</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract ThrowMe { &nbsp;</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">assertExample</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; assert(address(</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">this</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">).balance == </span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">0</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">);</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><span class="Apple-tab-span" style="white-space:pre;"></span></span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; </span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// Do something.</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:10.5pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>assert 也用于略有不同的情况——这些情况下需要不同类型的保护。</p>
<p>大多数情况下，你使用 assert 来检查“invariant(不变)”的数据片段。在软件开发中，不变量是一个或多个数据，其值在程序执行时永远不会改变。</p>
<p>上面的代码示例是一个微型合约，并不是为了接收或存储任何 ETH 而设计的。它的设计旨在确保它的合约余额始终为零，这就是我们使用 assert 测试的不变量。</p>
<p><code>assert()</code> 调用也用在 internal 函数中。它们可以测试本地状态不包含或不可能的值，但由于合约状态变得“脏”，这些值可能已经改变。</p>
<p>正如 <code>require()</code>， <code>assert()</code> 也会回退所有更改。但是在 Solidity 的 v0.8 之前，<code>assert()</code> 用于耗尽所有剩余的 gas，这一点与 <code>require()</code> 不同。</p>
<p>通常，你可能会更多地使用 <code>require()</code> 而不是 <code>assert()</code>。</p>
<p>第三种方法是使用 <code>revert()</code> 调用。这通常用于与 <code>require()</code> 相同的情况，但使用 <code>revert()</code>的场景中，一般条件逻辑会更复杂。</p>
<p>此外，你可以在使用 <code>revert()</code> 时抛出自定义错误。就 gas 消耗而言，使用自定义错误通常可以更便宜，并且从代码和错误可读性的角度来看，自定义错误通常可以提供更多信息。</p>
<p>请注意我是如何通过在自定义错误名称前加上合约名称，从而提高其可读性和可追溯性的，通过这种方式我们可以知道是哪个合约引发了错误。</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract ThrowMe { &nbsp;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 自定义错误</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; error ThrowMe_BadInput(string errorMsg, uint inputNum);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">revertExample</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">uint input</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">if</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (input &lt; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">1000</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> ) {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><span class="Apple-tab-span" style="white-space:pre;"></span></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; revert ThrowMe_BadInput(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"Number must be an even number greater than 999"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">,&nbsp; input);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">if</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (input &lt; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">0</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; revert(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"Negative numbers not allowed"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>在上面的示例中，我们使用了一次 revert 和一个带有两个特定参数的自定义错误，然后我们再次使用 revert 且仅包含一个字符串错误数据。在任何一种情况下，区块链状态都会 revert，未使用的 gas 将返回给调用者。</p>
<h2 id="inheritance-in-solidity">Solidity 中的继承</h2>
<p><a href="https://www.freecodecamp.org/news/inheritance-in-java-explained/">继承</a>是面向对象编程 (OOP) 中的一个非常重要的概念。我们不会在这里详细介绍 <a href="https://www.freecodecamp.org/news/object-oriented-concepts/">OOP</a> 是什么。但是，可以将继承理解为，一段代码通过导入和嵌入另一段代码来“继承”数据和函数。</p>
<p><a href="https://goo.gl/maps/YgMqAwFfHUP8EZmS9">Solidity 中的继承</a>还允许开发人员访问、使用和修改继承自合约的属性（数据）和函数（行为）。</p>
<p>接收这种继承材料的合约称为派生合约、子合约或子类。其代码可用于一个或多个派生合约的合约称为父合约。</p>
<p>继承让代码的重用变得更加方便——想象一下继承链，应用程序代码从其他代码继承，而被继承代码又从其他代码继承，等等。与其写出整个继承层次结构，我们可以只使用几个关键词来“扩展”继承链中所有应用程序代码获得的函数和数据。通过这样的方式，合约就可以从其层次结构中的所有父合约中受益，就像每一代都继承下来的基因一样。</p>
<p>与 Java 等某些编程语言不同，Solidity 允许多重继承。多重继承是指派生合约能够从多个父合约继承数据和方法。换句话说，一个子合约可以有多个父合约。</p>
<p>你可以通过查找 <code>is</code> 关键字来发现子合同并识别其父合同。</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract A {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; string public constant A_NAME = </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"A"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">getName</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">string memory</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> A_NAME;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract B is A {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; string public constant B_NAME = </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"B"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>如果你使用浏览器内的 <a href="http://remix.ethereum.org/">Remix IDE</a> 仅部署合约 B，你会注意到合约 B 可以访问 <code>getName()</code> 方法，即使它从未写在合约 B 中。当你调用该函数时，它返回“A”，这是在合约 A 中实现的逻辑数据，而不是合约 B。合约 B 可以访问存储变量 <code>A_NAME</code> 和 <code>B_NAME</code>，以及合约 A 中的所有函数。</p>
<p>这就是继承的工作原理。这就是合约 B 如何重用合约 A 中已经编写的代码，这些代码可能是由其他人编写的。</p>
<p>Solidity 允许开发人员更改父合约中的函数在派生合约中的实现方式。修改或替换继承代码的函数称为“重写(overriding)”。为了理解它，让我们探讨一下当合约 B 尝试实现自己的 <code>getName()</code> 函数时会发生什么。</p>
<p>通过向合约 B 添加 <code>getName()</code> 来修改代码。确保函数名称和签名与合约 A 中的相同。子合约在 <code>getName()</code> 函数中的逻辑实现可能与它在父合约中的实现方式完全不同，尽管函数名称及其签名是一模一样的。</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract A {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; string public constant A_NAME = </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"A"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">getName</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">string memory</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> A_NAME</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract B is A {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; string public constant B_NAME = </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"B"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">getName</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">string memory</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// … any logic you like. Can be totally different</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// from the implementation in Contract A.</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> B_NAME;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>编译器会给出两个错误：</p>
<ol>
<li>在合约A中，会提示你“trying to override non-virtual function（试图覆盖非虚函数）”，提示你是否忘记添加 <code>virtual</code> 关键字。</li>
<li>在合约 B 中，它会抱怨 <code>getName()</code> 函数缺少标识符 <code>override</code>。</li>
</ol>
<p>这意味着你在合约 B 中的新 <code>getName</code> 试图重写父合约中同名的函数，但父合约的函数未标记为 <code>virtual</code> - 这意味着它无法被重写。</p>
<p>你可以更改合约 A 的功能并添加 <code>virtual</code> 关键字，如下所示：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">getName</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">virtual</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">string memory</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">A_NAME</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>添加关键字 <code>virtual</code> 不会改变函数在合约 A 中的运行方式。并且它不要求继承合约<strong>必须</strong>重新实现它。它只是意味着如果开发人员选择，此功能<strong>可能</strong>会被任何派生合约重写。</p>
<p>添加 <code>virtual</code> 修复了编译器对合约 A 的提出的问题，但对合约 B 没有。这是因为合约 B 中的 <code>getName</code> 还需要添加 <code>override</code> 关键字，如下所示：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">getName</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> pure </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">override</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">string memory</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> B_NAME;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>我们还为合约 B 的 getName() 添加了 <code>pure</code> 关键字，因为这个函数不会改变区块链的状态，并且只读取 constant（constant，你会记得，在编译时被硬编码到字节码中，不在存储数据位置）。</p>
<p>请记住，只有在父合约和子合约中，函数的名称和签名相同时，你才需要重写它。</p>
<p>但是对于名称相同但参数不同的函数会发生什么情况呢？当这种情况发生时，它不是重写，而是重载。并且没有冲突，因为这些函数有不同的参数，所以它们的签名可以向编译器表明它们是不同的。</p>
<p>例如，在合约 B 中，我们可以有另一个带有参数的 <code>getName()</code> 函数。与父合约 A 的 <code>getName()</code> 实现相比，不同的参数会使得函数的“签名”不同。重载函数不需要任何特殊关键字：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// getName() now accepts a string argument.&nbsp;</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// Passing in “Abe Lincoln” returns the string “My name is: Abe Lincoln”</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">getName</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">string memory name</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">string memory</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; bytes memory n = abi.encodePacked(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"My name is:&nbsp; "</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">, name);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> string(n);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>不要担心 <code>abi.encodepacked()</code> 方法调用。稍后当我们谈论编码和解码时，我会解释这一点。现在只需了解 <code>encodepacked()</code> 将字符串编码为字节，然后将它们连接起来，并返回一个字节数组。</p>
<p>我们在本手册的前一节（在类型那一节）讨论了 Solidity 字符串和字节之间的关系。</p>
<p>此外，由于你已经了解了函数修饰符，这恰是可以添加修饰符也是可继承的地方。以下是你的操作方式：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract A {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#c27ba0;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">modifier</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> X </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">virtual</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;// … some logic</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;}</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract B is A {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#c27ba0;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">modifier</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> X </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">override</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// … logic that replaces X in Contract A</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;}</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>你可能想知道如果继承链中存在具有相同名称和签名的函数，将调用哪个版本的函数。</p>
<p>例如，假设有一个合约继承的链条，如 A → B → C → D → E，它们都有一个 <code>getName()</code> 且都重写了前一个父合约中的 <code>getName()</code> 。</p>
<p>哪个 <code>getName()</code> 被调用？答案是最后一个——这个合约继承结构中的“最后派生”的函数实现。</p>
<p>子合约中的状态变量不能与其父合约具有相同的名称和类型。</p>
<p>例如，下面的合约 B 将无法编译，因为它的状态变量“隐藏”了父合约 A 的状态变量。但请注意合约 C 如何正确处理此问题：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract A {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; string public author = </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"Zubin"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">getAuthor</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">virtual</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">string memory</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> author;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br><br></span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// Contract B would not compile</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract B is A {</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// Not OK.&nbsp; author shadows the state variable in Contract A!</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; string public author = </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"Mark Twain"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br><br></span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// This will work.</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract C is A {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">constructor</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(){</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; author = </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"Hemingway"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>值得注意的是，通过将新值传递给合约 C 的构造函数中的变量 <code>author</code>，我们实际上覆盖了合约 A 中的值。然后调用继承方法 <code>C.getAuthor()</code> 将返回 'Hemingway' 而不是 'Zubin' ！</p>
<p>还值得注意的是，当一个合约继承自一个或多个父合约时，区块链上只会创建一个（组合）合约。编译器有效地将所有其他合约及其父合约（parent contract）等编译成一个单一的编译合约（称为“扁平化（flatten）”合约）。</p>
<h2 id="inheritance-with-constructor-parameters">继承与构造函数参数</h2>
<p>一些构造函数指定输入参数，因此它们需要你在实例化智能合约时将参数传递给它们。</p>
<p>如果该智能合约是父合约，则其派生合约也必须传递参数以实例化父合约。</p>
<p>有两种方法可以将参数传递给父合约——在列出父合约的语句中，或者直接在每个父合约的构造函数中。你可以在下面看到这两种方法：</p>
<p><img src="https://lh5.googleusercontent.com/8MV8HOaET3aVL30ygclKgPhUKIiMK8feTpNNN20W4qFQ880SPBFNeRb2BsS7fLR7b8iUJpRPdG1osMGT451FKOZGpuiihn8h00hTHipxe-MPh9QQ1t2bm2r-qhe4xj7vFFewg64ve9NPjlMlN3BHaX7hWG2ZJvMC8-tsHS2vV74ZluERre2fcH_52CFO9Q" alt="8MV8HOaET3aVL30ygclKgPhUKIiMK8feTpNNN20W4qFQ880SPBFNeRb2BsS7fLR7b8iUJpRPdG1osMGT451FKOZGpuiihn8h00hTHipxe-MPh9QQ1t2bm2r-qhe4xj7vFFewg64ve9NPjlMlN3BHaX7hWG2ZJvMC8-tsHS2vV74ZluERre2fcH_52CFO9Q" width="600" height="400" loading="lazy"></p>
<p>在 <code>ChildTwo</code> 合约的方法 2 中，你会注意到传递给父合约的参数首先提供给子合约，然后沿着继承链向上传递。</p>
<p>这不是必需的，但却是一种非常常见的模式。关键是父合约构造函数期望输入参数，而我们需要在实例化子合约时提供它们。</p>
<h2 id="type-conversion-and-type-casting-in-solidity">Solidity 中的类型转换</h2>
<p>有时我们需要将一种数据类型转换为另一种数据类型。当我们这样做时，我们需要非常小心地转换数据，同时考虑计算机会如何理解转换后的数据。</p>
<p>正如我们在关于类型化数据的讨论中看到的那样，JavaScript 有时会对数据做一些奇怪的事情，因为它是动态类型的语言。但这也是为什么一般地介绍类型转换和类型转换的概念是有用的。</p>
<p>采用以下 JavaScript 代码：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">var</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> a = </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">"1"</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">var</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> b = a +&nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">9</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// we get the string '19'!!</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">typeof</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> a </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// string</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">typeof</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> b </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// string&nbsp;</span></p></td></tr></tbody></table>
<p>有两种方法可以将变量 <code>a</code> 转换为整数。第一个称为类型转换，由程序员显式完成，通常涉及使用 <code>()</code> 的类似构造函数的操作符。</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">a = </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Number</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(a) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// Type casting the string to number is explicit.</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">typeof</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> a </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// number</span></p></td></tr></tbody></table>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">var</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> b = a +&nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">9</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 10. A number. More intuitive!</span></p></td></tr></tbody></table>
<p>现在让我们将 <code>a</code> 重置为字符串并进行隐式转换，也称为类型转换。这是编译器在执行程序时隐式完成的。</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">a = </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#d1f1a9;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">'1'</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">var</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> b = a * </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">9</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// Unlike addition, this doesn't concatenate but implicitly converts 'a' to a number!&nbsp;</span></p><br><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">b </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// number 9, as expected!</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">typeof</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> b </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// number</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">typeof</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> a </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// still a string…</span></p></td></tr></tbody></table>
<p>在 Solidity 中，类型转换（显式转换）在某些类型之间是允许的，代码类似于下面这样：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">uint256 a = </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">2022</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">bytes32 b = bytes32(a);</span></p><br><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// b now has a value of</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 0x00000000000000000000000000000000000000000000000000000000000007e6</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// which is 32 bytes (256) bits of data represented in</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 64 Hexadecimal Characters, where each character is 4 bits (0.5 bytes).</span></p></td></tr></tbody></table>
<p>在此示例中，我们将大小为 256 位的整数（因为 8 位构成 1 个字节，所以这是 32 个字节）转换为大小为 32 的字节数组。</p>
<p>由于 2022 的整数值和字节值的长度均为 32 字节，因此在转换过程中没有“丢失”信息。</p>
<p>但是，如果您尝试将 256 位转换为 8 位（1 字节），会发生什么情况？尝试在基于浏览器的 Remix IDE 中运行以下代码：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract Conversions {</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">explicit256To8</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">uint8</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; uint256 a = </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">2022</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; uint8 b = uint8(a);</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> b;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> // 230.</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;}</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>为什么整数 2022 会转换为 230？这显然不是我们预想的结果。明显是一个错误，对吧？</p>
<p>原因是大小为 256 位的无符号整数将包含 256 个二进制数字（0 或 1）。所以 <code>a</code> 保存整数值 '2022' 并且该值（以位为单位）将有 256 位数字，其中大部分将为 0，除了最后 11 位数字将是......（通过将 2022 从十进制系统到二进制 <a href="https://decimaltobinary.com/256-in-binary">此处</a>）。</p>
<p>另一方面，<code>b</code> 的值只有 8 位或数字，即 11100110。这个二进制数转换为十进制时（你可以使用相同的转换器 - 只需再另一个框中填写！）是 230。不是 2022 .</p>
<p>哎呀。</p>
<p>所以发生了什么事？当我们将整数的大小从 256 位减少到 8 位时，我们最终去掉了数据的前三位数字 (11111100110)，这完全改变了二进制值！</p>
<p>朋友们，这就是信息丢失。</p>
<p>所以当你显式转换时，编译器在某些情况下会允许你这样做，但是你可能会丢失数据。因为你明确要求这样做，所以编译器会假设你知道自己在做什么。这可能是许多错误的根源，因此请确保正确测试代码以获得预期结果，并在将数据显式转换为较小尺寸时要小心。</p>
<p>投射到更大的尺寸不会导致数据丢失。由于 2022 只需要 11 位来表示，您可以将变量 <code>a</code> 声明为 <code>uint16</code> 类型，然后将其向上转换为 <code>uint256</code> 类型的变量 <code>b</code>，而不会丢失数据。</p>
<p>另一种有问题的转换是从无符号整数转换为有符号整数。尝试以下示例：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract Conversions {</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">unsignedToSigned</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">int16, uint16</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; int16 a = </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">-2022</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; uint16 b = uint16(a);</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// uint256 c = uint256(a); // Compiler will complain</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; </span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (a, b);</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> // b is 63514</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; }</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>请注意，作为 16 位大小的带符号整数的 <code>a</code> 将 -2022 作为（负整数）值保存。如果我们想要将它类型转换为一个 <em>unsigned</em> 整数（只有正数）值，编译器也会允许我们这样做。</p>
<p>但是如果你运行代码，你会看到 <code>b</code> 不是 -2022 而是 63,514！因为 <code>uint</code> 无法保存有关减号的信息，它丢失了该数据，并且生成的二进制被转换为大量十进制（以 10 为基数）数字 - 显然这和预期不一样，是一个 bug。</p>
<p>如果你更进一步，取消注释给 <code>c</code> 赋值的那一行，你会看到编译器报错 “<em>不允许从 “int16” 到 “uint256” 的显式类型转换</em>”。即使我们在 <code>uint256</code> 中向上转换为更多的位，因为 <code>c</code> 是一个无符号整数，它不能包含负号信息。</p>
<p>因此，在显式强制转换时，请务必考虑，在强制编译器更改数据类型后，该值的计算结果会是什么。这会是许多错误和代码错误的根源。</p>
<p>Solidity 类型转换和类型转换还有更多内容，你可以在<a href="https://betterprogramming.pub/solidity-tutorial-all-about-conversion-661130eb8bec">这个文章</a> 中深入了解一些细节。</p>
<h2 id="how-to-work-with-floating-point-numbers-in-solidity">Solidity 中如何使用浮点数</h2>
<p>Solidity 不处理小数点。这在未来可能会改变，但目前你无法真正使用浮点数，如 93.6。事实上，在您的 Remix IDE 中键入 <code>int256 floating = 93.6;</code> 会抛出如下错误：<em>Error: Type rational_const 468 / 5 is not implicitly convertible to expected type int256.</em></p>
<p>这里发生了什么？ 468 除以 5 是 93.6，这似乎是一个奇怪的错误，但这基本上是编译器说它不能处理浮点数。</p>
<p>按照错误的提示，将变量的类型声明为 <code>fixed</code> 或 <code>ufixed16x1</code></p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">fixed floating = </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">93.6</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span></p></td></tr></tbody></table>
<p>你会收到“<em>UnimplementedFeatureError：Not yet implemented - FixedPointType</em>”错误。</p>
<p>因此，在 Solidity 中，我们通过将浮点数乘以 10 的指数，将浮点数转换为整数（无小数点）来解决这个问题，指数大小为小数点右边的小数位数。</p>
<p>在这种情况下，我们将 93.6 乘以 10 得到 936，我们必须在某处的变量中跟踪我们的因子 (10)。如果数字是 93.2355，我们会将其乘以 10 的 4 次方，因为我们需要将小数点右移 4 位以使数字完整。</p>
<p>使用 ERC 代币时，我们会注意到小数位通常为 10、12 或 18。</p>
<p>例如，1 Ether 是 1*(10^18) wei，即 1 后接 18 个零。如果我们想用浮点数表示，我们需要将 1000000000000000000 除以 10^18（这将得到 1），但如果它是 1500000000000000000 wei，那么除以 10^18 将在 Solidity 中抛出编译器错误，因为它无法处理 1.5 的返回值。</p>
<p>在科学计数法中，10^18 也表示为 1e18，其中 1e 表示 10，后面的数字表示 1e 的指数。</p>
<p>所以下面的代码会产生一个编译器错误：“<em>Return argument type rational_const 3 / 2 is not implicitly convertible to expected type…int256</em>”：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">divideBy1e18</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">()</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">int</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">1500000000000000000</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">/(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">1e18</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">);</span><span style="font-size:9pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> // 1.5 → Solidity can’t handle this.</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>上述除法运算的结果是 1.5，但是有小数点，Solidity 目前不支持。因此 Solidity 智能合约返回非常大的数字，通常最多 18 位小数，这超出了 JavaScript 的处理能力。因此，你需要在前端使用 <a href="https://docs.ethers.io">Ethersjs</a> 等 JavaScript 库处理这个问题，这些库为 <a href="https://docs.ethers.io">BigNumber</a> 实现辅助函数/v5/api/utils/bignumber/) 类型。</p>
<h2 id="hashing-abi-encoding-and-decoding">哈希、ABI 编码（encoding）和解码（decoding）</h2>
<p>随着你使用 Solidity 越来越多，你会看到一些听起来很奇怪的术语，例如哈希、ABI 编码和 ABI 解码。</p>
<p>虽然这些可能需要一些学习才可以理解，但它们对于使用密码技术（尤其是以太坊）来说是非常基础的。它们原则上并不复杂，只是一开始可能有点难以掌握。</p>
<p>让我们从哈希开始。使用加密数学，你可以将<strong>任何</strong>数据转换为（非常大的）唯一整数。此操作称为哈希算法。哈希算法有一些重要的特点：</p>
<ol>
<li>它们是确定性的——相同的输入将<strong>总是</strong>产生相同的输出，每次都是如此。但是使用不同的输入产生相同输出的概率极小。</li>
<li>如果只有输出，基本不可能（或计算上不可行）对输入进行逆向工程。这是一个单向过程。</li>
<li>输出的大小（长度）是固定的——无论输入大小如何，算法都会为所有输入生成固定大小的输出。换句话说，哈希算法的输出将始终具有固定的位数，具体取决于算法</li>
</ol>
<p>哈希算法有许多行业标准，但你可能会最常见地看到 SHA256 和 Keccak256。这些非常相似。256 指的是大小——生成的哈希中的位数。</p>
<p>例如，请进入<a href="https://emn178.github.io/online-tools/keccak_256.html">此网站</a> 并将“FreeCodeCamp”复制并粘贴到文本输入中。使用 Keccak256 算法，输出将（始终）为“796457686bfec5f60e84447d256aba53edb09fb2015bea86eb27f76e9102b67a”。</p>
<p>这是一个 64 字符的十六进制字符串，由于十六进制字符串中的每个字符代表 4 位，因此该十六进制字符串为 256 位（32 字节长）。</p>
<p>现在，删除文本输入框中除“F”之外的所有内容。结果是一个完全不同的十六进制字符串，但它仍然有 64 个字符。这是 Keccak265 哈希算法的“固定大小”性质。</p>
<p>现在粘贴回“FreeCodeCamp”并更改任意字符。你可以把“F”变成小写。或者加一个空格。对于你所做的每个单独更改，哈希十六进制字符串输出都会发生很大变化，但长度不变。</p>
<p>这是哈希算法的一个重要特性。最细微的变化都会大大改变散列。这意味着你始终可以通过比较它们的哈希来测试两个事物是否相同（或根本没有被篡改）。</p>
<p>在 Solidity 中，比较哈希比比较原始数据类型要高效得多。</p>
<p>例如，比较两个字符串通常是通过比较它们的 ABI 编码（字节）形式的哈希值来完成的。在 Solidity 中比较两个字符串的常见辅助函数如下所示：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">compareStrings</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">string memory str1, string memory str2</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">)</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">bool</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">)</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (keccak256(abi.encodePacked((str1))) ==</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keccak256(abi.encodePacked((str2))));</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span></p></td></tr></tbody></table>
<p>稍后我们将讨论 ABI 编码是什么，但请注意 <code>encodePacked()</code> 的结果是一个 <code>bytes</code> 数组，然后使用 keccak256 算法（这是 Solidity 使用的哈希算法）对其进行哈希处理。比较输出的哈希值（256 位整数）是否相等。</p>
<p>现在让我们转向 ABI 编码。首先，我们记得 ABI（Application Binary Interface：应用程序二进制接口）是指定如何与部署的智能合约进行交互的接口。 ABI 编码是将给定元素从 ABI 转换为字节以便 EVM 可以处理它的过程。</p>
<p>EVM 在位和字节上运行计算。所以编码是<a href="https://stackoverflow.com/questions/14822323/why-is-encoding-and-decoding-needed-for-any-programming-language-database">将结构化输入数据转换为字节</a>的过程，使得计算机可以运行它。解码是将字节转换回结构化数据的逆过程。有时，编码也称为“序列化”。</p>
<p>你可以在 <a href="https://docs.soliditylang.org/en/latest/cheatsheet.html#global-variables">此处</a>。编码数据的方法将它们转换为字节数组（“bytes”数据类型）。相反，解码其输入的方法期望字节数据类型作为输入，然后将其转换为已编码的数据类型。</p>
<p>你可以在以下代码片段中观察到这一过程：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// SPDX-License-Identifier: MIT</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pragma solidity ^</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">0.8.13</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract EncodeDecode {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br><br></span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// Encode each of&nbsp; the arguments into bytes</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">encode</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; uint x,</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; address addr,</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; uint[] calldata arr</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">external</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">bytes memory</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> abi.encode(x, addr, arr);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">decode</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">bytes calldata bytesData</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">)</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">external</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uint x,</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; address addr,</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uint[] memory arr</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">)</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; (x, addr, arr) = abi.decode(bytesData, (uint, address, uint[]));</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>我在 Remix 中运行了上面的代码，并为 <code>encode()</code> 使用了以下输入：1981, 0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC, [1,2,3,4]。</p>
<p>我返回的字节以十六进制形式表示为：<br>
<code>0x00000000000000000000000000000000000000000000000000000000000007bd0000000000000000000000003c44cdddb6a900fa2b585dd299e03d12fa4293bc000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000004</code>.</p>
<p>我将其作为我的输入，输入到 <code>decode()</code> 函数中，并取回了我原来的三个参数。</p>
<p>因此，编码的目的是将数据转换成 EVM 处理数据所需的字节数据类型。解码将其转换为开发人员可以使用的人类可读结构化数据。</p>
<h2 id="how-to-call-contracts-and-use-the-fallback-function">如何调用合约并且使用 fallback 函数</h2>
<p>根据智能合约的设计和其中存在的可见度标识符，合约可以与其他智能合约或外部拥有的账户（EOA）进行交互。</p>
<p>通过你的钱包在 remix 中调用是后者的一个例子，就像使用 Metamask 一样。你还可以通过 EthersJS 和 Web3JS 这些库，以及 Hardhat 和 Truffle 工具链以可编程方式与智能合约进行交互。</p>
<p>出于本 Solidity 手册的目的，我们将使用 Solidity 与另一个合约进行交互。</p>
<p>一个智能合约调用其他智能合约有两种方式。第一种方式通过使用接口（我们之前讨论过）直接调用目标合约。或者，如果将 Target 合约引入到调用合约的范围内，然后直接调用它。</p>
<p>这种方法如下所示：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract Target {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; int256 public count;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">decrement</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; count--;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">interface ITarget {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">decrement</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">external</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">TargetCaller</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">callDecrementInterface</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">address _target</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; ITarget target = ITarget(_target);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; target.decrement();</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">callDecrementDirect</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Target _target</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; _target.decrement();</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>在 Remix 中，你可以先部署 <code>Target</code>，然后调用 <code>count()</code> 以查看 count 变量的默认值为 <code>0</code>，正如预期的那样。如果你调用 <code>decrement()</code> 方法，该值将减 1。</p>
<p>然后你可以部署<code>TargetCaller</code>，有两个方法可以调用，这两个方法都会将 <code>Target</code> 中 <code>count</code> 的值减 1。</p>
<p>请注意，这两种方法中的每一种都使用略有不同的语法访问 <code>Target</code> 合约。当使用 <code>ITarget</code> 接口进行交互时，第一个方法接受 <code>Target</code> 的地址，而第二个方法将 <code>Target</code> 视为自定义类型。</p>
<p>只有当 <code>Target</code> 合约在与 <code>TargetCaller</code> 文件中声明或导入时，第二种方法才可用。大多数情况下，你将与第三方部署的智能合约进行交互，为了能够交易，第三方一般都会发布了 ABI 接口。</p>
<p>每调用一次这些方法，<code>Target</code> 中的 <code>count</code> 值就会减 <code>1</code>。这是与其他智能合约交互的一种非常常见的方式。</p>
<p>第二种方法是使用 Solidity 提供的“底层”调用语法。当你还想向目标合约发送一些 ETH 时，你可以使用它。将在下一节讨论发送 ETH，但现在只需将 Remix 中的代码替换为以下内容：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract Target {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; int256 public count;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">decrement</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">int num</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">payable</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; count = count - num;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">interface ITarget {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">decrement</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">int num</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">external</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">payable</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">TargetCaller</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">callDecrementLowLevel</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">address _target</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; ITarget target = ITarget(_target);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; target.decrement{value:</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">0</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">5</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// other decrementing functions….</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>你会注意到 <code>decrement()</code> 现在接受一个参数，并且接口和 <code>Target</code> 合约会使用这个新的输入数据进行更新。</p>
<p>接下来请注意，<code>TargetCaller</code> 实现了一个使用新语法调用 <code>decrement()</code> 的新函数，如下所述。</p>
<p><img src="https://lh4.googleusercontent.com/xwOSOgsIrqRNzz2PFYziACphMXlVp1QWDItbAAaxAHGYChhIq7eyntuP6FQOrYdWL37dvD4qyUrp2SAOgwk8BC9nnm27k_0jCOCbTvEBbTPvGvd2xMoCfIjV75s6l9O7a-nEPJbpFVzKDnOdP-2S6Ot-8vtV6iD-SRYNp1pCRe8mfQDij7_c8Ek2LDux6Q" alt="Calling a contract's function and sending it value in Wei." width="600" height="400" loading="lazy"></p>
<p>在下一节中，我们将看到示例，展示如何通过底层方法调用目标智能合约以向其发送 Ether。</p>
<p>但是当你调用一个合约，而这个合约实际上并没有你想要调用的函数时会发生什么？</p>
<p>利用 Solidity 在 EVM 上的运行机制，这一点可能会被人利用以进行恶意攻击。或者，更常见的是，它可能会意外发生。例如，当接口中存在错误，并且编译器无法将你发送的函数和参数与合约中实际包含的任何内容相匹配时，就会发生这种情况。那么会发生什么？</p>
<p>对于这些情况，许多合约采用了一种称为回退（fallback）函数的特殊函数。该函数看起来像一个普通函数，但它不需要 <code>function</code> 关键字。如果你想让它也处理你的合约被发送一些以太币的情况，你还必须将它标记为 <code>payable</code>。但不推荐你通过方式来接受收款。</p>
<p>让我们通过重新调整我们之前的 <code>Target</code>、<code>ITarget</code> 和 <code>TargetCaller</code> 并添加一个 fallback 函数来看一下回发生什么，代码如下所示：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract Target {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; int256 public count;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">decrement</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(int num) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">payable</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; count = count - num;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; fallback() external </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">payable</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; count++;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">interface ITarget {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">decrement</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">int num</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">external</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">payable</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">nonExistentFunction</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">external</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">TargetCaller</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">callFallback</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">address _target</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; ITarget target = ITarget(_target);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; target.nonExistentFunction();</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>我们部署了一个新的 <code>Target</code> 实例以后，就可以调用 <code>count()</code> 并看到它被设置为默认值零。</p>
<p>接下来我们可以部署 <code>TargetCaller</code> 并调用 <code>callFallback()</code> 方法，该方法在内部调用 <code>nonExistentFunction()</code>。</p>
<p>值得注意的是，该接口表示 <code>nonExistentFunction()</code> 可用，但实际的 <code>Target</code> 合约没有这样的函数。这就是触发 <code>Target</code> 的 fallback 函数并且计数值现在增加 <code>1</code> 的原因。</p>
<p>fallback 函数的目的是在没有其他函数可用于处理情况下，处理外部对合约的调用。如果 fallback 被标记为 <code>payable</code>，fallback 函数还将使智能合约能够接收 Ether（尽管不推荐这样使用 fallback）。我们将在下一节中介绍这一点。</p>
<h2 id="how-to-send-and-receive-ether">如何发送和接收 Ether</h2>
<p>如果你想要从你的智能合约向目标合约发送以太币，需要使用以下三种内置的 Solidity 方法之一，来调用目标合约：<code>transfer</code>、<code>send</code> 或 <code>call</code>。</p>
<p><code>transfer</code> 失败时会抛出异常，而 <code>send</code> 和 <code>call</code> 将返回一个布尔值，所以你需要在继续运行下面的代码之前检查该值。在这三个中，出于安全原因不再推荐使用 <code>transfer</code> 和 <code>send</code>，但你仍然可以使用它们并且它们会起作用。</p>
<p>除以下情况外，智能合约无法接收 Ether：</p>
<ul>
<li>它们实现了一个<code>payable</code> 的 fallback 函数或<code>payable</code> 的特殊接收函数，或者</li>
<li>当调用合约调用 <code>selfdestruct</code> 并强制目标合约接受其所有剩余的 ether。然后调用合约从区块链中删除。这是另一个话题，经常被开发者恶意使用。</li>
</ul>
<p>如果你希望智能合约接收 ETH，通常建议您使用 <code>receive()</code> 函数。当然你也可以通过将 fallback 函数设置为 <code>payable</code> 来实现，但推荐的做法还是使用 <code>receive()</code> 函数。</p>
<p>如果你只依赖 fallback 函数，你的编译器会向你发出以下消息：“<em>警告：这个合约有 payable fallback 函数，但没有 receive ether 的函数。考虑添加 receive ether 的函数。”</em></p>
<p>如果你同时拥有 <code>receive</code> 和 <code>fallback</code>，你可能想知道 Solidity 如何决定使用哪个函数接收 Ether。该设计会告诉你这些函数的设计初衷是什么。</p>
<p>receive 是用来接收 ether 的。而 fallback 函数，正如我们在上一节中讨论的那样，是在合约在被调用时，被调用的函数不存在的时使被使用的。</p>
<p>Solidity 通过检查调用者发送的交易中的 <code>msg.data</code> 字段来匹配要调用的方法。如果该字段是一个非空值，并且该值与被调用合约中声明的任何其他函数都不匹配，则会触发 fallback 方法。</p>
<p>如果 <code>msg.data</code> 为空，那么它将检查是否有已实现的 receive 函数。如果有，这个 receive 函数将会被调用以接受 ETH。如果不存在 receive 函数，它将默认使用 fallback 函数。</p>
<p>在你的合约中，使用 <code>receive</code> 函数来接受 ETH 更好。对于 fallback 函数，你可以在没有任何函数能够“处理”某个函数调用时去使用它。</p>
<p>这是一个超级方便的逻辑树，显示了 receive 和 fallback 应该的使用方式。</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">哪一个 </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">函数</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">被</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">调用</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">, </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">fallback</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">or</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">receive</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">()?</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">send</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Ether</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">msg</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">.</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">data</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">is</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">empty</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">?</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; / \</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">yes</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">no</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; / &nbsp; &nbsp; \</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">receive</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">exists</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">?&nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">fallback</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">()</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; / &nbsp; \</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">yes</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">no</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; /&nbsp; &nbsp; &nbsp; \</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">receive</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">fallback</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">()</span></p></td></tr></tbody></table>
<p>(感谢: <a href="https://solidity-by-example.org/sending-ether/">Solidity By Example</a>)</p>
<p>回到我们探索 fallback 函数的示例，我们可以向 <code>Target</code> 添加一个 <code>receive</code> 函数，如下所示：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract Target {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; int256 public count;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">decrement</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">payable</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; count = count - num;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; fallback() external </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">payable</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; count++;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp;&nbsp;&nbsp;&nbsp;receive() external </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">payable</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; count+=5;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">interface ITarget {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">decrement</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">int num</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">external</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">payable</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">nonExistentFunction</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">external</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">TargetCaller</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">callFallback</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">address _target</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; ITarget target = ITarget(_target);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; target.nonExistentFunction();</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>我们已经看到 <code>callFallback</code> 将如何更改 <code>Target</code> 合约中的变量 count 的值。但是，如果我们部署一个新的 <code>Target</code> 实例，我们现在可以向它发送 10 wei，如下所示，因为它现在具有“payable”和“receive”功能。在发送 10 wei（或任何其他金额）之前，<code>Target</code> 的余额为零，如下所示。</p>
<p><img src="https://lh5.googleusercontent.com/yCKofUjShb7KqezaShYgDsPMbx9EBX1aetb9AbzJJawPz-ehvzhsHkkW_Uyd4Ah95Wmrm6nbkKCCDKAkoFU7_obDJ6zeQIB1nU9Q9laJo3o58Lg1_A_tRj-zQyEj7_DOaIeKoc1fV8wAcUbfUY2vYhVljTrewhT1PyeWZbWyAv5KYdKySqtWu3P6D_3_bQ" alt="yCKofUjShb7KqezaShYgDsPMbx9EBX1aetb9AbzJJawPz-ehvzhsHkkW_Uyd4Ah95Wmrm6nbkKCCDKAkoFU7_obDJ6zeQIB1nU9Q9laJo3o58Lg1_A_tRj-zQyEj7_DOaIeKoc1fV8wAcUbfUY2vYhVljTrewhT1PyeWZbWyAv5KYdKySqtWu3P6D_3_bQ" width="600" height="400" loading="lazy"></p>
<p>使用空的 calldata（<code>msg.data</code>）点击交易按钮将改变余额，如下图所示。我们可以检查 <code>count</code> 以查看它增加了 5，因为这是 <code>receive</code> 函数中的逻辑。</p>
<p><img src="https://lh5.googleusercontent.com/dMbTEYYraIK2EwGRYdkKxBFQ6rS1inTuyvVeNwJbObQoxAyJaZ957HHDm4buyKl5le1iP_f41hOB-4ACVm8Oz0wVK1LjPfylhEfpwP9oBo5RlWcN27XemNWWwrt0S2Vng5TaQlZyfnIhqPrV8xUaR2ZVS-Oj7F0XQUALn2AxENG_1PiK_uGT3mJLw-Iklw" alt="dMbTEYYraIK2EwGRYdkKxBFQ6rS1inTuyvVeNwJbObQoxAyJaZ957HHDm4buyKl5le1iP_f41hOB-4ACVm8Oz0wVK1LjPfylhEfpwP9oBo5RlWcN27XemNWWwrt0S2Vng5TaQlZyfnIhqPrV8xUaR2ZVS-Oj7F0XQUALn2AxENG_1PiK_uGT3mJLw-Iklw" width="600" height="400" loading="lazy"></p>
<p>将 Wei 发送到 Target 合约并观察更新后的余额</p>
<p>如果我们调用 <code>callFallback</code> 并给它新的 <code>Target</code> 实例的地址，我们会注意到它只递增 <code>1</code>。如果我们包含一些 wei，那也会增加 <code>Target</code> 的余额。</p>
<p>因此，任何将 ETH 转移到智能合约的转账，都需要接收 ETH 的智能合约具有可以接收 ETH 的 <code>payable</code> 函数。至少，接收 ETH 的智能合约需要一个 <code>payable</code> fallback 函数，尽管 <code>payable</code> <code>receive</code> 函数是接收 ETH 转账的更好方法。</p>
<h2 id="solidity-libraries">Solidity 库（library）</h2>
<p>在任何编程语言中，库都是指辅助和功能型函数的集合，这些函数旨在被多个代码文件重用。这些函数解决了特定的、重复出现的编程问题。</p>
<p>在 Solidity 中，库也是做同样的事情，但有一些特殊的属性。</p>
<p>首先，它们是无状态的——也就是说，它们不存储数据（常量除外，因为它们不会改变区块链的状态）。它们也不能接收 value（这意味着它们不能有 <code>payable receive</code> 或 <code>fallback</code> 函数）。</p>
<p>它们也不能从其他合约或库继承，库也不能有子（派生）合约。</p>
<p>库中声明的所有函数都不能是抽象的——也就是说，它们必须都有具体的实现。</p>
<p>由于 Solidity 库是无状态的，因此其中的任何方法都不能修改区块链的状态。这意味着库中的所有方法都是 <code>pure</code> 或 <code>view</code> 函数。</p>
<p>Solidity 库的另一个有趣属性是它们不需要导入到你的智能合约中。它们可以作为独立合约部署，然后在所有使用智能合约中通过它们的接口调用——就像传统工程世界中的 API 服务一样。</p>
<p>但是，库必须包含公共或外部方法，才有意义。同时，该库可以作为独立合约部署，从而具有自己的以太坊地址的，并可被所有使用智能合约调用。</p>
<p>如果库仅包含 internal 方法，那么 EVM 只是将库代码“嵌入”到使用该库的智能合约中（因为无法从其他智能合约访问 internal 函数）。</p>
<p>除了代码重用以外，Solidity 中的库还具有其他的优势。在区块链上一次性部署一个库可以避免重复部署或导入库的代码，从而节省未来的 gas 成本。</p>
<p>让我们看一个简单的库，然后剖析代码以了解如何使用库。</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">library WeirdMath {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; int private constant factor = </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">100</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">applyFactor</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">int self</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">int</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> self * factor;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">add</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">int self, int numberToAdd</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">int</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> self + numberToAdd;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>该库有两种对 <code>int</code> 数据类型进行操作的方法。第一个参数称为 <code>self</code>。第一种方法接受一个数字，然后将其乘以存储在库代码中的常量值。第二种方法接受两个数字并将它们相加。</p>
<p>现在让我们看看如何在用户智能合约中使用它。</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// SPDX-License-Identifier: MIT</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pragma solidity &gt;=</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">0.5.22</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> &lt;=</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">0.8.17</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract StrangeMath {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// Method 1 - using Library name with dot notation</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">multiplyWithFactor</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">int num</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">int</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> WeirdMath.applyFactor(num);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// Method 2 - the 'using' keyword and dot notation.</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// Syntax: using &lt;&lt;Library Name&gt;&gt; for data type of the first argument in the method to be called.</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">using</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> WeirdMath </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">for</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> int;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">addTwoNums</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">int num1, int num2</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pure</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">int</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> num1.add(num2);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>首先要注意的是，有两种使用 <code>WeirdMath</code> 库的方法。</p>
<p>你可以通过以下任一方式使用它：</p>
<ol>
<li>调用库的名称后跟要调用的函数，或者</li>
<li>直接在你希望函数操作的数据类型上调用函数。此数据类型必须与库函数中 <code>self</code> 参数的类型相同。</li>
</ol>
<p>第一种方法由代码片段中的方法 1 演示，我们使用 <code>WeirdMath.add(num1, num2);</code> 调用库。</p>
<p>第二种方法使用 Solidity 的 <code>using</code> 关键字。表达式 <code>return num1.add(num2);</code> 将 <code>WeirdMath</code> 库的 add 函数应用到 <code>num1</code> 变量上。这与将其作为 <code>self</code> 传入效果相同，self 是 add 函数的第一个参数。</p>
<h2 id="events-and-logs-in-solidity">Solidity 中的事件（events）和日志（logs）</h2>
<p>智能合约可以发出事件。这些事件包含开发人员指定的一些数据。</p>
<p>事件不能被其他智能合约调用使用。相反，它们作为日志存储在区块链上，并且可以通过从区块链读取的 API 进行检索。</p>
<p>这意味着你的应用程序（通常是你的前端应用程序）可以从区块链“读取”包含事件数据的日志。通过这种方式，你的用户界面可以对区块链上的事件做出响应。</p>
<p>这就是更新应用程序用户界面以响应链上事件的方式。由于可以查询区块链上的这些日志，因此日志是一种廉价的存储形式，正如前面关于存储区域的讨论中所讨论的那样。</p>
<p>可以使用相关的区块链浏览器来获取智能合约发出的事件，因为公共区块链上的所有内容都是公开可见的。但是如果智能合约的字节码没有经过验证，事件数据可能不是人类可读的（它将被编码），只有经过验证的智能合约的事件才是人类可读的。</p>
<p>节点和其他区块链客户端可以监听（订阅）特定事件。这也是 <a href="http://docs.chain.link">Chainlink 预言机</a> 的工作原理——去中心化的预言机节点监听来自智能合约的事件，然后做出相应的响应。它们甚至可以从事件中提取数据，在链下运行复杂且消耗很多计算资源的计算，然后将可加密验证的计算结果提交回区块链。</p>
<p>其他网络 API 和索引服务 <a href="https://thegraph.com/en/">如 subgraph</a> 之所以成为可能，是因为能够通过智能合约发出的事件查询区块链数据。</p>
<p>这是 Solidity 中事件的样子：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// SPDX-License-Identifier: MIT</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">pragma solidity ^</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">0.8.7</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">contract SimpleStorage {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; uint256 favoriteNumber;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">event</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> storedNumber(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; uint256 indexed oldNumber, </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// up to 3 indexed params allowed</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; uint256 indexed newNumber,</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; uint256 addedNumber,</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; address sender</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; );</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">store</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">uint256 </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">newNumber) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; emit storedNumber(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; favoriteNumber,</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newNumber,</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newNumber + favoriteNumber,</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg.sender</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; );</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; favoriteNumber = newNumber;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">retrieve</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">() </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">view</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">returns</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> (</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">uint256</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">return</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> favoriteNumber;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">}</span></p></td></tr></tbody></table>
<p>首先声明一个事件，并指定其参数及其数据类型。任何具有 <code>indexed</code> 关键字的数据都由 EVM 索引，使得对区块链日志的查询可以使用索引参数作为过滤器，会让日志的检索速度更快。</p>
<p>一个事件最多可以存储 4 个索引参数——取决于它是匿名的还是非匿名的。索引事件参数在 Solidity 世界中也称为“Topics”。</p>
<p>大多数事件都是非匿名的，这意味着它们包含有关事件名称和参数的数据。</p>
<p>非匿名事件只允许开发人员指定 3 个主题，因为第一个主题保留用于指定 ABI 编码的十六进制形式的事件签名。你可以在 <a href="https://docs.ethers.io/v5/concepts/events/#events-solidity">此处</a> 阅读有关匿名和非匿名 topics 的更多信息。</p>
<p>你还可以在相关的区块链浏览器（例如 <a href="http://etherscan.io">etherscan.io</a>）上探索事件。</p>
<p>你可以从两个点进行了解。你可以直接查看合约的地址，然后转到“事件”选项卡（它将仅显示该合约发出的事件）。或者可以转到交易哈希并检查由该交易交互的所有合约发出的所有事件。</p>
<p>例如，下面是以太坊主网上的 <a href="https://docs.chain.link/vrf/v2/subscription/supported-networks#ethereum-mainnet">Chainlink VRF Coordinator 智能合约的</a> 事件的屏幕截图。</p>
<p><img src="https://lh3.googleusercontent.com/P-Ue4bGSB4swKcUxv6DJpuZxADBw6ZKPKeo_H7_x5lZBcJGuHxYUjcYm9rRCi80TFJSkKVpXJ_2zS7Pm9s0ASs9SrmIXc9uGTeTn_9e2FBwgTlRYSKQkB4cfIWRA3K8S8RWx6-5dEb048MtvlUkvPH17So9nPyo9R36L5Zwi8aoe4BdWfw4MIOH0M3NRjw" alt="Inspecting the Chainlink VRF Coordinator Contract's events on etherscan" width="600" height="400" loading="lazy"></p>
<p>在 etherscan 上查看 Chainlink VRF Coordinator 合约的事件</p>
<p>合约的选项卡有一个绿色的勾号，这意味着合同已经过验证，因此事件名称和参数是人类可读的。花点时间研究这张图片，因为它包含很多信息！如果你想直接在 etherscan 上研究它，<a href="https://etherscan.io/address/0x271682DEB8C4E0901D1a1550aD2e64D568E69909#events">单击此处</a>。</p>
<p>该 Chainlink VRF Coordinator 合约响应对加密可验证随机数的请求，并为请求智能合约提供随机数（称为“随机词：random words”）。</p>
<p>如果您想了解“word”在计算机科学中的含义，请查看我和我的同事在 <a href="https://youtu.be/lu3yDJQqqY0?t=2627">Chainlink 2022 黑客马拉松视频</a> 中解决这个问题的方法。</p>
<p>当 VRF Coordinator 合约满足随机数请求时，它会发出一个 <code>RandomWordsFulfilled</code> 事件。该事件包含 4 条数据，其中第一个 <code>requestID</code> 可以被用作索引。</p>
<p>Solidity 事件包含三类数据：</p>
<ol>
<li>发出事件的合约地址。</li>
<li>主题（用于过滤日志查询的索引事件参数）。</li>
<li>非索引参数，简称“数据”，采用ABI编码，以十六进制表示。此数据需要按照 ABI 编码和解码部分中描述的方式进行 ABI 解码。</li>
</ol>
<p>在 Remix 中工作时，你还可以在控制台中检查事件，如下所示：</p>
<p><img src="https://lh4.googleusercontent.com/YyXlklkork7-lcLKXdQqWYYGZeIX8IWaqRUgjKNGG9cG4AiyAG-9MGyjWwP8hXFT9tkSWu5Q6X8qVUHWwgczo-H4W07qAsmAPny4y8FWNz5jBQ1YynBIeor7mtpbZLshRRN4hYrehAj3lhXbzxEYYguNDqeJxjowshHeVLltuzrHXM8RMnS2-82bvGOcjg" alt="Inspecting Event data in the Remix Browser IDE" width="600" height="400" loading="lazy"></p>
<p>在 Remix Browser IDE 中查看事件数据</p>
<p>你还可以在 EthersJS 中以编程方式访问事件 <a href="https://stackoverflow.com/questions/73230175/does-etherjs-transactionreceipt-have-an-events-object">使用合同收据对象</a>。使用我们上面在 SimpleStorage 合约中使用的代码片段，我们可以通过以下 JavaScript 代码来使用 EthersJS 和 Hardhat 访问事件：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:12pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">const</span><span style="font-size:12pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> transactionResponse = </span><span style="font-size:12pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">await</span><span style="font-size:12pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> simpleStorage.store(</span><span style="font-size:12pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">1981</span><span style="font-size:12pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">)</span><span style="font-size:12pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:12pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">const</span><span style="font-size:12pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> receipt = </span><span style="font-size:12pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">await</span><span style="font-size:12pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> transactionResponse.wait()</span><span style="font-size:12pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:12pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:12pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">console</span><span style="font-size:12pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">.log(receipt.events[</span><span style="font-size:12pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">0</span><span style="font-size:12pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">].args.newNumber.toString()) </span><span style="font-size:12pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// 1981</span></p></td></tr></tbody></table>
<p>你还可以在前端应用程序中使用像是 EtherJs 这样的库来<a href="https://docs.ethers.io/v5/getting-started/#getting-started--events">监听事件</a> 和<a href="https://docs.ethers.io/v5/getting-started/#getting-started--history">过滤历史事件</a>。当你的应用程序需要响应区块链上的事件时，这两者都很有用。</p>
<h2 id="time-logic-in-solidity">Solidity 中的时间逻辑</h2>
<p>Solidity 中的时间是根据添加到区块链的每个区块决定的。</p>
<p>全局变量 <code>block.timestamp</code> 指的是块生成并添加到区块链的时间，以毫秒为单位。毫秒计数是指自 Unix 纪元开始以来经过的毫秒数（在计算中，这是 1970 年 1 月 1 日）。</p>
<p>与 Web2 以毫秒为单位引用时间戳不同，该值可能不会每毫秒增加一次。</p>
<p>一个区块通常包含多个交易，并且由于 <code>block.timestamp</code> 指的是区块被开采的时间，所以一个区块中的所有交易都将具有相同的时间戳值。所以时间戳实际上指的是区块的时间，而不是调用者发起交易的时间。</p>
<p>Solidity 支持直接引用以下时间单位：秒、分钟、小时、天和周。</p>
<p>所以我们可以通过类似 <code>uint lastWeek = block.timestamp - 1 weeks;</code> 这样的代码来计算当前区块被开采前 1 周的时间戳，精确到毫秒。该值与 <code>block.timestamp - 7 days;</code> 相同。</p>
<p>你还可以使用它来计算未来的到期日期，例如，你可能希望在现在和下周之间可以进行操作。你可以使用 <code>uint registrationDeadline = block.timestamp + 1 weeks;</code> 来执行此操作，然后我们可以使用 <code>registrationDeadline</code> 作为函数中的验证或保护，如下所示：</p>
<table style="border:none;border-collapse:collapse;"><colgroup></colgroup><tbody><tr style="height:0pt"><td style="vertical-align:top;background-color:#002451;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ebbbff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">function</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">registerVoter</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">address voter</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">) </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">public</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#bbdaff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">view</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> {</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffc58f;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">require</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">(block.timestamp &lt;= registrationDeadline, “registration deadline has passed.”);</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#7285b7;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">// Register the Voter....</span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"><br></span><span style="font-size:11pt;font-family:Consolas,sans-serif;color:#ffffff;background-color:#002451;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">&nbsp; &nbsp; }</span></p></td></tr></tbody></table>
<p>在此函数中，我们仅在当前区块的时间戳未超过注册截止日期时才注册 voter。</p>
<p>当我们想要确保某些操作在正确的时间或在一定时间间隔内执行时，都可以使用此逻辑。</p>
<p>这也是可以配置 <a href="https://docs.chain.link/chainlink-automation/introduction/">Chainlink Automation</a> 的一种方式，这是一种自动执行智能合约的去中心化方式。 Chainlink 去中心化预言机网络可以配置为自动触发您的智能合约，你可以通过检查条件（包括与时间相关的条件）来运行各种自动化。这些被广泛用于空投、促销、特殊奖励、赚取收益等。</p>
<h2 id="conclusion-and-further-resources">总结和更多资源</h2>
<p>恭喜！你完成了这段史诗般的旅程。如果你花时间消化了本手册，并在 Remix IDE 中运行了一些代码，那么你现在已经接受了 Solidity 的培训。</p>
<p>从这里开始，这是一个练习、重复和经验的问题。当你着手构建下一个出色的去中心化应用程序时，请记住重新审视基础知识并关注安全性。安全性在 Web3 空间中尤为重要。</p>
<p>你可以从 <a href="https://blog.openzeppelin.com/">OpenZeppelin 的博客</a> 和 <a href="https://www.trailofbits.com/resources">Trail of Bits</a> 资源等获取有关最佳实践的更多信息。</p>
<p>你还可以通过完成我的同事 Patrick Collins 发布在 freeCodeCamp 上 <a href="https://www.youtube.com/watch?v=gyMwXuJrbJQ&amp;t=59657s">端到端全栈区块链开发人员课程</a>（免费！） 获得更多实践经验。</p>
<p>还有其他资源，例如 blockchain.education 和 freeCodeCamp 自己即将推出的 Web3 课程，可以巩固你的学习。</p>
<p>无论如何，无论你的经验水平如何，本手册都可以成为你快速复习基本概念的“桌面伴侣”。</p>
<p>要记住的重要一点是 Web3 技术总是在不断发展。迫切需要愿意应对复杂挑战、学习新技能并解决分散式架构带来的重要问题的开发人员。</p>
<p>那可能（也应该）是你！所以只要跟随你的好奇心，不要害怕一路上的挣扎。</p>
<p>再一次，我打算不断更新这本手册。因此，如果你发现任何不太正确、过时或不清楚的内容，只需在推文中提及并<a href="https://twitter.com/zubinpratap">标记我</a> 和 freeCodeCamp - 感谢所有人为了保持本手册不过时所做出的努力。</p>
<p>现在……变得很棒！</p>
<h3 id=""><strong>写在最后</strong></h3>
<p>如果你真的想转行做代码，你可以了解更多关于我从律师到软件工程师的旅程。查看 <a href="http://podcast.freecodecamp.org/">freeCodeCamp 播客</a> 的 <a href="http://podcast.freecodecamp.org/53-zubin-pratap-from-lawyer-to-developer">第 53 集</a> 和还有“Lessons from a Quitter”<a href="https://lessonsfromaquitter.com/episode207/">第 207 集</a>。这些为我的职业转变提供了蓝图。</p>
<p>如果你有兴趣改变职业并成为一名专业开发者，请联系<a href="http://linktree.com/zubinpratap">此处</a>。如果你想的话，你还可以查看我的 <a href="http://futurecoderstraining.com/">免费网络研讨会</a> <a href="http://futurecoderstraining.com/">Career Change to Code</a>。</p>
<!--kg-card-end: markdown--> ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
