Solidity学习疑问总结

1字节(Byte)=8位(bit)

创建对象?

    struct People {
        string name;
        uint age;
    }
    People public person = People({name:'ljq',age:12});

创建数组?查询mapping映射?

contract SimpleStorage {
    mapping (string => uint256) public nameToFavoriteNumber;
    struct People {
        string name;
        uint age;
    }
    People[] public PeopleList;
    function addPerson(string memory name, uint256 age ) public {
        PeopleList.push(People({name:'qq',age:13}));
        nameToFavoriteNumber[name] = age;
    }
}

 calldata,memory,storage?  calldata:不可修改的临时变量 memory:可修改的临时变量 storage:可修改的永久变量

import合约?

import "./SimpleStorage.sol";

contract StorageFactory {
    SimpleStorage public simpleStorage;
    function createSimpleStorageContract() public {
        simpleStorage = new SimpleStorage();
    }
}

与其它合约交互?

继承和重写?

import "./SimpleStorage.sol";

contract ExtraStorage is SimpleStorage{
    // is:给ExtraStorage合约加一些额外的函数,同时也包含了SimpleStorage合约的所有功能
    // virtual: 允许该函数被重写
    function store(uint256 _favNumber) public virtual {
        favNumber = _favNumber + 5;
    }
}

 

posted @ 2024-10-04 23:09  木木的奇奇  阅读(1)  评论(0编辑  收藏  举报