10-Solidity8.0结构体

Solidity8.0

10-Solidity8.0结构体


在这里插入图片描述


前言

结构
您可以通过创建结构来定义自己的类型。
它们对于将相关数据分组在一起很有用。
结构可以在合同之外声明并在另一个合同中导入。


一、Solidity8.0结构体

1.结构体

代码如下(示例):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8;

contract Todos {
    
    
    struct Todo {
    
    
        string text;
        bool completed;
    }

    Todo[] public todos;

    function create(string memory _text) public {
    
    
        todos.push(Todo(_text, false));
        todos.push(Todo({
    
    text: _text, completed: false}));

        Todo memory todo;
        todo.text = _text;

        todos.push(todo);
    }

    function get(uint _index) public view returns (string memory text, bool completed) {
    
    
        Todo storage todo = todos[_index];
        return (todo.text, todo.completed);
    }

    function update(uint _index, string memory _text) public {
    
    
        Todo storage todo = todos[_index];
        todo.text = _text;
    }

    function toggleCompleted(uint _index) public {
    
    
        Todo storage todo = todos[_index];
        todo.completed = !todo.completed;
    }
}

总结

日拱一卒。

猜你喜欢

转载自blog.csdn.net/yyjava/article/details/125289198
今日推荐