首页 » SEO优化 » uint256php技巧_Solidity教程Solidity 中的数据类型和数据结构

uint256php技巧_Solidity教程Solidity 中的数据类型和数据结构

访客 2024-11-20 0

扫一扫用手机浏览

文章目录 [+]

Solidity 与任何其他编程措辞一样,有自己的数据类型和数据构造,但具有不同的语法和运用程序。

本教程将先容 Solidity 编程措辞中一些最常用的数据类型和数据构造。

uint256php技巧_Solidity教程Solidity 中的数据类型和数据结构

Solidity 数据类型的性子

Solidity 实质上是一种静态类型的强类型措辞,它在实行源代码之前实行类型检讨。

uint256php技巧_Solidity教程Solidity 中的数据类型和数据结构
(图片来自网络侵删)

作为一种静态类型措辞,Solidity 哀求程序员在编译代码(编译时)之前声明每个变量的数据类型。

而 Solidity 作为一种强类型措辞意味着变量的数据类型不能在程序中修正或转换为另一种数据类型。

Solidity 数据类型

Solidity 和其他编程措辞一样,将其数据类型分为两类:值类型和引用类型。

Solidity 中的值类型

值类型变量是将数据直接存储在分配给自身的堆栈内存中的变量。
[3]

这些类型是按值通报的,这意味着只要将它们分配给新变量或作为参数供应给函数,它们就会被复制,对新副本所做的任何变动都不会影响原始数据。

1.) 整数

Solidity 中的整数数据类型用于存储整数值。
整数类型进一步分为int和uint用于分别声明有符号和无符号整数类型。

i. int/有符号整数

int关键字用于声明有符号整数。
有符号整数是一种数据类型,可以在智能合约中保存正整数和负整数值。

// SPDX-License-Identifier: MITpragma solidity ^ 0.8.13;contract Signed_Integer_Example{ int year = 2022; // positive value (✅) int temperature = -89; // negative value (✅)}ii. uint/无符号整数

该uint关键字用于声明无符号整数。
无符号整数是智能合约中只能保存正整数值的数据类型。

// SPDX-License-Identifier: MITpragma solidity ^ 0.8.13;contract Unsigned_Integer_Example{ uint year = 2022; // positive value (✅) uint temperature = -89; // negative value (❌)}

当您考试测验将负值分配给无符号数据类型时,您将收到以下TypeError:

TypeError: Type int_const -89 is not implicitly convertible to expected type uint256. Cannot implicitly convert signed literal to an unsigned type.2.) 字节

Solidity 中的字节是包含字节序列的固定大小的字节数组。
字节数组的长度在字节的前面定义,如bytes1to bytes32。

该数字相称于字节数组变量可以包含的字符数。

// SPDX-License-Identifier: MITpragma solidity ^ 0.8.13;contract Bytes_Array_Example{ bytes1 one_character = "a"; // 1 character (✅) bytes2 two_characters = "ab"; // 2 characters (✅) bytes3 three_characters = "abc"; // 3 characters (✅) bytes4 four_characters = "abcd"; // 4 characters (✅) bytes5 five_characters = "abcde"; // 5 characters (✅) bytes32 thrity_two_characters = "abcdefghijklmnopqrstuvwxyz123456"; // 32 characters (✅)}

当您考试测验分配超过固定字节大小的字符数时,如下所示:

// SPDX-License-Identifier: MITpragma solidity ^ 0.8.13;contract Bytes_Array_Example{ bytes1 one_character = "ab"; // single value (❌) bytes1 two_characters = "abc"; // single value (❌)}

您将收到以下TypeError:

TypeError: Type literal_string "abc" is not implicitly convertible to expected type bytes1. Literal is larger than the type.3.) 布尔值

Solidity 中的布尔值由bool关键字表示,并且与其他所有编程措辞一样,Solidity 中的布尔值仅接管两个值:true和false:

// SPDX-License-Identifier: MITpragma solidity ^ 0.8.13;contract Boolean_Example{ bool isEthereumMerge = true; // (✅) bool currentUserCanMintToken = false; // (✅) bool isRaining = "true"; // (❌) bool isAdmin = "false"; // (❌)}

当您考试测验将非布尔值分配给布尔变量时,您将收到以下TypeError:

TypeError: Type literal_string "true" is not implicitly convertible to the expected type bool.4.) 地址

地址是 Solidity 中的一种分外数据类型,能够吸收和发送 Ether。
地址数据类型旨在存储以太坊地址,该地址常日以0x值开头。

地址大小为 20 个字节,包含 42 个字符。

0x0000000000000000000000000000000000000000

地址也是从公钥的[4]Keccak-256 散列[5]天生的不区分大小写的十六进制数字。

当您考试测验将字符串分配给地址数据类型时,如下所示:

// SPDX-License-Identifier: MITpragma solidity ^ 0.8.13;contract Address_Example{ address user_address = 0x0000000000000000000000000000000000000000; // (✅) address user_home_address = "Street 2, downtown road"; // (❌)}

您将收到以下TypeError:

TypeError: Type literal_string "Street 2, downtown road" is not implicitly convertible to expected type address.

当您考试测验将非十六进制数字分配给地址数据类型时,如下所示的八进制数字:

// SPDX-License-Identifier: MITpragma solidity ^ 0.8.13;contract Address_Example{ address user_address = 0x0000000000000000000000000000000000000000; // (✅) address phone_address = 080123456789; // (❌)}

您将收到以下ParserError:

ParserError: Octal numbers not allowed.

地址值类型进一步分为两种:

功能addressaddress payable检讨地址余额✅✅发送以太币❌✅吸收以太币❌✅

专业提示:当您希望您的智能合约吸收和发送以太币时,请利用address payable值类型。
当您不肯望您的智能合约吸收或转移以太币时,请利用纯address值类型。

5.) 列举

列举数据类型,也称为列举,使开拓职员能够创建用户定义的数据类型。
用户定义的数据是分配给从零开始的整数常量值的名称。

// SPDX-License-Identifier: MITpragma solidity ^ 0.8.13;contract Enum_Example{ enum Status { Sending, // 0 Success, // 1 Failed // 2 } Status status; function sendSomething () public { status = Status.Sending; // set status to sending }}

从上面的代码片段中,我们创建了一个Status列举来保存我们发送内容时的操作状态。
然后,我们可以利用列举将操作的状态更新为列举中的任何预定义状态Status。

Solidity 中的引用类型(数据构造)

引用类型变量是将数据的位置(内存地址)存储在堆[6]内存上的变量,它们不直接共享数据。

对参考数据所做的变动将始终影响原始数据。

Solidity 中引用类型的示例包括字符串、构造、数组和映射。

1.) 字符串

string类型是一个字符序列。
Solidity 支持利用单引号' '和双引号" "的字符串笔墨。

// SPDX-License-Identifier: MITpragma solidity ^ 0.8.13;contract String_Example{ string name = "John Doe";}2.) 构造

struct数据类型是一种引用数据类型,可用于创建其他数据类型的构造。
构造可以包含值类型和引用类型,包括其他构造,但不能包含其自身的构造。

可以利用以下语法在 Solidity 中创建构造:

struct <Struct_Name> { <data_type> <variable_name>; }

data_type可以是string,int,uint,bool,或任何solidity数据类型。
构造可以在智能合约之外声明,并导入另一个合约中。

构造的用例如下所示:

// SPDX-License-Identifier: MITpragma solidity ^ 0.8.13;contract Struct_Example{ // User profile structure struct UserProfile { string fullName; // string bool isOnboarded; // boolean uint age; // unsigned integer (no negative) } // Create a new record using the created structure UserProfile _newUserProfile = UserProfile("Ayodele Samuel Adebayo", true, 19); // Get the created profile function getUserProfile() public view returns (string memory, bool , uint ){ return (_newUserProfile.fullName, _newUserProfile.isOnboarded, _newUserProfile.age); }}

从上面的代码中,我们为用户配置文件创建了一个构造,期望有一个fullName,isOnboarded状态,和用户年事。
然后,我们利用这个构造来创建一个新的用户,用一个函数来返回所创建的个人资料的信息。

要点:在 Solidity 中利用构造使我们的代码更有条理、可掩护、可重用和可读。

3.) 数组

数组是具有相同数据类型的变量的凑集。
它们存储在一个连续的内存位置,每个数组项都有一个唯一的索引。

Solidity 中的数组可以是固定大小或动态大小,每个数组项都可以通过其唯一索引进行搜索。

i. 动态数组

可以利用以下语法在 Solidity 中创建动态数组:

// SPDX-License-Identifier: MITpragma solidity ^ 0.8.13;contract Dynamic_Array_Syntax{ <datatype[]> <variable_name> = <[array_items]>}

下面是一个名字的string动态数组和一个数字的uint动态数组的例子。

// SPDX-License-Identifier: MITpragma solidity ^ 0.8.13;contract Dynamic_Array_Example{ string[] arrayOfNames = ["Faith", "Becky", "Steve"]; uint[] arrayOfNumbers = [0, 1, 2, 3, 4, 5];}ii. 固定大小的数组

可以利用以下语法创建固定大小的数组:

// SPDX-License-Identifier: MITpragma solidity ^ 0.8.13;contract Fixed_Size_Array_Syntax{ <datatype[size]> <variable_name> = <[array_items]>}

下面是 2 个固定大小的string名称数组和 1 个固定大小的uint动态数字数组的示例:

// SPDX-License-Identifier: MITpragma solidity ^ 0.8.13;contract Fixed_Size_Array_Example{ string[2] arrayOfNames = ["Faith", "Becky"]; uint[1] arrayOfNumbers = [0];}

当您考试测验超过固定大小限定时:

// SPDX-License-Identifier: MITpragma solidity ^ 0.8.13;contract Fixed_Size_Array_Example{ string[2] arrayOfNames = ["Faith", "Becky", "Steve"]; // (❌) uint[1] arrayOfNumbers = [0, 1, 2]; // (❌)}

您将分别收到以下TypeError:

TypeError: Type string[3] memory is not implicitly convertible to expected type string[2] storage ref.

TypeError: Type uint8[3] memory is not implicitly convertible to expected type uint256[1] storage ref.4.) 映射

Solidity 中的映射是一种键值对数据构造,其功能类似于 Python 中的字典和 JavaScript 中的哈希表或工具。

可以利用以下语法在 Solidity 中创建映射:

// SPDX-License-Identifier: MITpragma solidity ^ 0.8.13;contract Mapping_Syntax{ mapping (key => value) variable_name;}

个中key可以是除引用类型以外的任何数据类型,并且value可以是值类型和引用类型。

以下是将用户的钱包地址映射到别的额的示例:

// SPDX-License-Identifier: MITpragma solidity ^ 0.8.13;contract Mapping_Example{ mapping (address => uint) users_balances;}

从上面的数据构造实现中,我们可以用用户的钱包地址从区块链中检索到用户的加密货币余额,为uint类型。

总结

数据类型和数据构造是任何编程措辞的根本,也是利用 Solidity 开拓高等 Dapp 的构建块。

在本教程中,我们先容了 Solidity 编程措辞中最常用的数据类型和数据构造。

引用链接

[1] 编写智能合约: https://web3.hashnode.com/solidity-tutorial-learn-how-to-build-your-first-smart-contract[2] 构建和支配 Dapp: https://web3.hashnode.com/solidity-tutorial-how-to-build-and-deploy-an-nft-minting-dapp[3] 堆栈内存中的变量。
: https://en.wikipedia.org/wiki/Stack_(abstract_data_type)[4] 地址也是从公钥的: https://www.cs.utexas.edu/users/moore/acl2/manuals/current/manual/index-seo.php/ETHEREUM____PUBLIC-KEY-TO-ADDRESS[5] Keccak-256 散列: https://cryptomarketpool.com/keccak256/[6] 堆: https://opendsa-server.cs.vt.edu/OpenDSA/Books/CS2/html/HeapMem.html

相关文章

留英IT工作,跨越国度的职业机遇与挑战

随着全球化的不断深入,信息技术(IT)行业已经成为国际人才流动的重要领域。越来越多的中国年轻人选择留英深造,并在英国IT行业开启职...

SEO优化 2024-12-27 阅读0 评论0

疫情之下,金融IT的变革与创新

自2019年底新冠病毒疫情爆发以来,全球经济受到了前所未有的冲击。金融行业作为国家经济的命脉,也面临着前所未有的挑战。在疫情背景下...

SEO优化 2024-12-27 阅读0 评论0

人工智能时代,机器代替语言的探索与挑战

随着科技的飞速发展,人工智能(AI)逐渐渗透到我们生活的方方面面。从智能家居到自动驾驶,从医疗诊断到教育辅导,AI技术正在改变着我...

SEO优化 2024-12-27 阅读0 评论0

交换机封协议,构建高效网络通信的基石

随着互联网技术的飞速发展,网络通信已成为现代社会不可或缺的一部分。交换机作为网络通信的核心设备,其性能直接影响到整个网络的稳定性和...

SEO优化 2024-12-27 阅读0 评论0