操作 Enum
contract Demo {
enum State {
PENDING,
ACTIVE,
INACTIVE
}
State private state;
// 更新 state
function updateState(State _state) public {
state = _state;
}
// 重置 state
function resetState() public {
delete state; // 使用 delete 重置为默认值 PENDING, 即 0
}
// 获取 state
function getState() public view returns (State) {
return state;
}
}
部署 Demo 合约,state 默认值为 PENDING,即 0
调用 getState 方法,返回值为 uint8 类型的整数 0
调用 updateState 方法,传入 0 / 1 / 2,分别表示 PENDING / ACTIVE / INACTIVE
调用 getState 方法,返回值为传入的整数
调用 resetState 方法,重置 state 为默认值 PENDING,即 0
调用 getState 方法,返回值为 0
操作 Array
操作动态大小的数组:
contract Demo {
uint[] private dynamicArray;
// 获取数组长度
function getLength() public view returns (uint) {
return dynamicArray.length;
}
// 获取指定元素
function get(uint _index) public view returns (uint) {
return dynamicArray[_index];
}
// 更新指定元素
function set(uint _index, uint _value) public {
dynamicArray[_index] = _value;
}
// 添加元素
function push(uint _value) public {
dynamicArray.push(_value); // 使用 push 添加元素到数组末尾
}
// 重置指定元素; eg: [1, 2, 3] - [1, 0, 3]
function deleteByIndex(uint _index) public {
delete dynamicArray[_index]; // 使用 delete 将该元素重置为默认值, 不会改变数组长度
}
// 删除元素; eg: [1, 2, 3] - [1, 2]
function pop() public {
dynamicArray.pop(); // 使用 pop 删除数组末尾的元素
}
// 删除指定元素; eg: [1, 2, 3] - [1, 3]
function remove(uint _index) public {
for (uint i = _index; i < dynamicArray.length - 1; i++) {
dynamicArray[i] = dynamicArray[i + 1];
}
dynamicArray.pop();
}
// 删除所有元素; eg: [1, 2, 3] - []
function clear() public {
delete dynamicArray; // 使用 delete 删除所有元素, 数组长度变为 0
}
}
操作固定大小的数组:
contract Demo {
uint[3] private fixedArray1;
uint[] private fixedArray2 = new uint[](3);
// 获取数组长度
function getLength() public view returns (uint, uint) {
return (fixedArray1.length, fixedArray2.length);
}
// 获取指定元素
function get(uint _index) public view returns (uint, uint) {
return (fixedArray1[_index], fixedArray2[_index]);
}
// 更新指定元素
function set(uint _index, uint _value) public {
fixedArray1[_index] = _value;
fixedArray2[_index] = _value;
}
// 重置指定元素; eg: [1, 2, 3] - [1, 0, 3]
function deleteByIndex(uint _index) public {
delete fixedArray1[_index];
delete fixedArray2[_index];
// 使用 delete 将该元素重置为默认值, 不会改变数组长度
}
// 重置所有元素; eg: [1, 2, 3] - [0, 0, 0]
function clear() public {
delete fixedArray1;
delete fixedArray2;
// 使用 delete 将所有元素重置为默认值, 不会改变数组长度
}
}
在内存中创建数组:
contract Demo {
function createArray(uint size) public pure returns (uint[] memory) {
uint[] memory arr = new uint[](size);
for (uint i = 0; i < size; i++) {
arr[i] = i;
}
return arr;
}
}
操作 Mapping
contract Demo {
mapping(address => uint) private myBalance;
// 获取指定值
function getMyBalance(address _user) public view returns (uint) {
return myBalance[_user];
}
// 更新指定值
function setMyBalance(address _user, uint _amount) public {
myBalance[_user] += _amount;
}
// 重置指定值
function deleteMyBalance(address _user) public {
delete myBalance[_user]; // 使用 delete 将该值重置为类型的默认值
}
}
实现可迭代的 Mapping:
contract Demo {
mapping(address => uint) private balance; // 用户 => 余额
mapping(address => bool) public isCustomer; // 用户 => 是否是客户
address[] public customers; // 客户列表
// 存款
function deposit(address _customer, uint _amount) public {
balance[_customer] += _amount;
if (!isCustomer[_customer]) {
customers.push(_customer);
isCustomer[_customer] = true;
}
}
// 通过地址取款
function getBalance(address _customer) public view returns (uint) {
return balance[_customer];
}
// 通过索引取款
function getBalanceByIndex(uint _index) public view returns (uint) {
return balance[customers[_index]];
}
}
操作 Struct
contract Demo {
struct Student {
uint id;
string name;
}
Student[] private students;
}
- 创建 Struct 实例:
function addStudent1(string memory _name) public {
// 按属性顺序创建 Struct 实例, 并添加到 students 数组
students.push(Student(students.length, _name));
}
function addStudent2(string memory _name) public {
// 按属性名创建 Struct 实例, 并添加到 students 数组
students.push(Student({id: students.length, name: _name}));
}
function addStudent3(string memory _name) public {
// 创建 Struct 实例, 但不初始化, 会使用默认值
Student memory student;
// 设置属性值
student.id = students.length;
student.name = _name;
// 添加到 students 数组
students.push(student);
}
- 访问 Struct 实例属性:
function getStudentInfoByIndex(
uint _index
) public view returns (uint, string memory) {
Student memory student = students[_index];
return (student.id, student.name);
}
- 更新 Struct 实例属性:
function updateStudentNameByIndex(uint _index, string memory _name) public {
Student storage student = students[_index];
student.name = _name;
}
- 重置 Struct 实例属性:
function deleteStudentNameByIndex(uint _index) public {
Student storage student = students[_index];
delete student.name; // 使用 delete 将该属性值重置为默认值
}
- 重置 Struct 实例:
function deleteStudentByIndex(uint _index) public {
delete students[_index]; // 使用 delete 将该 Struct 实例的属性值都重置为默认值
}