solidity中,声明但没赋值的变量都有它的初始值或默认值
值类型初始值
-
boolean: false
-
string: “”
-
int: 0
-
uint: 0
-
enum: 枚举中的第一个元素
-
address:0x00000000000000000000000000000000000000000000(或address (0) )
-
function
internal:空白方程
external:空白方程
验证初始值
可以用public变量的getter函数验证上面写的初始值是否正确:
bool public _bool; //false
string public _string; //""
int public _int; //0
uint public _uint; //0
adress public _adress; //0x0000000000000000000000000000000000000000
enum ActionSet { Buy, Hold, Sell}
ActionSet public _enum; //第一个元素0
function fi() internal{} // internal空白方程
function fe() external{} // external空白方程
引用类型初始值
-
映射mapping:所有元素都为其默认值的mapping
-
结构体struct:所有成员设为其默认值的结构体
-
数组array:
**动态数组: []** **静态数组(定长)**:所有成员设为**其默认值的静态数组**
-
可以用public变量的getter函数验证上面写的初始值是否正确:
// Reference Types
uint[8] public _staticArray; // 所有成员设其默认值的静态数组[0,0,0,0,0,0,0,0]
uint[] public _dynamicArray; // '[]'
mapping(uint = > address) public _mapping; // 所有元素都为其默认值的mapping
// 所有成员设为其默认值的结构体0,0
struct Student{
uint256 id;
uint256 score;
}
Student public student;
delete操作符
delete a 会让变量 a 的值变为初始值
——可以删除一个变量的值并代替为初始值
// delete操作符
bool public _bool2 = true;
function d() external {
delete _bool2; //delete 会让_bool2变为默认值,false
}