solidity 引用类型修饰符memory、calldata与storage 常量修饰符Constant与Immutable区别

您所在的位置:网站首页 ram和storage的区别 solidity 引用类型修饰符memory、calldata与storage 常量修饰符Constant与Immutable区别

solidity 引用类型修饰符memory、calldata与storage 常量修饰符Constant与Immutable区别

2023-03-09 04:50| 来源: 网络整理| 查看: 265

测试2 python-图灵-1 java-图灵-1

在solidity语言中

引用类型修饰符(引用类型为存储空间不固定的数值类型)

memory、calldata与storage,它们只能修饰引用类型变量,比如字符串、数组、字节等...

memory 适用于方法传参、返参或在方法体内使用,使用完就会清除掉,释放内存

calldata 仅适用于方法传参,修饰该变量的值不能修改

storage 仅适用于方法体内,而且它的指针必须指向链上数据。使用完,链上数据将保存最新状态

 

常量修饰符

constant 编译前已经确定,编译后不能再修改常量的值

constant 它不是状态变量,所以它不储存在插槽(Slot)里面,获取该常量的方法修饰必须是Pure,而不是View

immutable 它是状态变量,所以它储存在插槽(Slot)里,获取该变量的方法修饰必须是View,而不是Pure

immutable 必须在构造函数里面赋值,之后就不能再修改

 

contract ConstantImmutable{       string private constant name ="Thinkingchain";     uint private immutable age;       constructor(uint256 _age){         age = _age;         //age = 10;     }     function getName() public pure returns(string memory){         return name;     }     function getAge() public view returns(uint){         return age;     }     /*     function setAge() public{         age++;     }     */ } 内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/xingzheakai/p/17188227.html

标签: 区块链 你还没有登录,请先登录或注册! 还没有人评论,欢迎说说您的想法! 相关课程 以太坊智能合约开发 3612 58元 GO语言区块链开发入门 2985 28元 GO语言区块链开发-密码学 3226 9.8元


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3