Solidity8.0
12-Solidity8.0-view和pure区别

前言
视图和纯函数
可以声明 Getter 函数view或pure.
View函数声明不会更改任何状态。
Pure函数声明不会更改或读取任何状态变量。
一、Solidity8.0-view和pure区别
1.view和pure区别
代码如下(示例):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;
contract ViewAndPure {
uint public x = 1;
// Promise not to modify the state.
function addToX(uint y) public view returns (uint) {
return x + y;
}
// Promise not to modify or read from the state.
function add(uint i, uint j) public pure returns (uint) {
return i + j;
}
}
总结
日拱一卒。
本文介绍了Solidity 8.0中view和pure函数的区别,讲解了它们在不改变状态的前提下如何使用,通过示例代码展示了如何在智能合约中正确应用这些特性。
2073

被折叠的 条评论
为什么被折叠?



