• solidity语言13


    函数过载

    合约内允许定义同名函数,但是输入参数不一致
    
    pragma solidity ^0.4.17;
    
    contract A {
        function f(uint _in) public pure returns (uint out) {
            out = 1;
        }
    
        function f(uint _in, bytes32 _key) public pure returns (uint out) {
            out = 2;
        } 
    }
    
    pragma solidity ^0.4.16;
    
    contract A {
        function f(B _in) public pure returns (B out) {
            out = _in;
        }
    
        function f(address _in) public pure returns (address out) {
            out = _in;
        }
    }
    
    contract B {
    }
    

    事件

    // 事件可方便使用EVM的日志工具,通过dapp的用户接口调用回调函数进行监听。
    
    pragma solidity ^0.4.0;
    
    contract ClientReceipt {
        event Deposit(
            address indexed _from,
            bytes32 indexed _id,
            uint _value
        );
    
        function deposit(bytes32 _id) public payable {
            // 任何调用当前函数的操作都会被检测到
            Deposit(msg.sender, _id, msg.value);
        }
    }
    
    var abi = /* abi as generated by the compiler */;
    var ClientReceipt = web3.eth.contract(abi);
    var clientReceipt = ClientReceipt.at("0x1234...ab67"); /* address */
    
    var event = clientReceipt.Deposit();
    event.watch(
        function(error, result) {
            if (!error)
                console.log(result);
    });
    
    /*或者
    var event = clientReceipt.Deposit(function(error, result) {
        if (!error)
            console.log(result);
    });
    */
    

    使用log低层接口

    log0, log1, log2, log3, log4 ~ logN
    
    pragma solidity ^0.4.10;
    
    contract C {
        function f() public payable {
            bytes32 _id = 0x420042;
            log3(
                bytes32(msg.value),
                bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20),
                bytes32(msg.sender),
                _id
            );
        }
    }
    

    用于理解事件的其他资源

    接口

    当合约继承其他合约,仅一个单独的合约被区位链建立,代码从基础合约复制到建立的合约中。

    pragma solidity ^0.4.16;
    
    contract owned {
        address owner;
        
        // 构造函数
        function owned() { 
            owner = msg.sender;
        }
    }
    
    // 继承合约owned的所有属性,但不能使用通过this进行外部访问
    contract mortal is owned {
        function kill() {
            if (msg.sender == owner)
                selfdestruct(owner);
        }
    }
    
    
    // These abstract contracts are only provided to make the
    // interface known to the compiler. Note the function
    // without body. If a contract does not implement all
    // functions it can only be used as an interface.
    contract Config {
        function lookup(uint id) public returns (address adr);
    }
    
    contract NameReg {
        function register(bytes32 name) public;
        function unregister() public;
    }
    
    
    contract named is owned, mortal {
        function named(bytes32 name) {
            Config config = Config(0xD5f9D8D94886E70b06E474c3fB14Fd43E2f23970);
            NameReg(config.lookup(1)).register(name);
        }
    
        function kill() public {
            if (msg.sender == owner) {
                Config config = Config(0xD5f9D8D94886E70b06E474c3fB14Fd43E2f23970);
                NameReg(config.lookup(1)).unregister();
                mortal.kill();
            }
        }
    }
    
    contract PriceFeed is owned, mortal, named("GoldFeed") {
        function updateInfo(uint newInfo) public {
            if (msg.sender == owner)
                info = newInfo;
        }
    
        function get() public view returns(uint r) {
            return info;
        }
    
        uint info;
    }
    
    pragma solidity ^0.4.0;
    
    contract owned {
        address owner;
    
        function owned() public {
            owner = msg.sender;
        }
    }
    
    contract mortal is owned {
        function kill() public {
            if (msg.sender == owner)
                selfdestruct(owner);
        }
    }
    
    contract Base1 is mortal {
        function kill() public {
            /* do cleanup 1 */ 
            mortal.kill(); 
        }
    }
    
    contract Base2 is mortal {
        function kill() public { 
            /* do cleanup 2 */ 
            mortal.kill(); 
        }
    }
    
    contract Final is Base1, Base2 {
    }
    
    pragma solidity ^0.4.0;
    
    contract owned {
        function owned() public { 
            owner = msg.sender; 
        }
        
        address owner;
    }
    
    contract mortal is owned {
        function kill() public {
            if (msg.sender == owner)
                selfdestruct(owner);
        }
    }
    
    contract Base1 is mortal {
        function kill() public { 
            /* do cleanup 1 */ 
            super.kill(); 
        }
    }
    
    contract Base2 is mortal {
        function kill() public { 
            /* do cleanup 2 */ 
            super.kill(); }
    }
    contract Final is Base1, Base2 {
    }
    
  • 相关阅读:
    macOS 遇到 svnadmin无法使用的情况
    语音识别进化简史:从造技术到建系统
    你是什么垃圾?人工智能面对干垃圾和湿垃圾“有点蒙”
    垃圾分类的事,让机器人做去吧!
    怎样才能叫一只奶牛自愿挤奶?
    第一次,脑机接口可以实时读取人类语言了
    机器人工作原理的超详细解析,生动、形象!
    1900页数学基础:面向CS的线性代数、拓扑、微积分和最优化
    微软Azure AI负责人:OpenAI只在微软云上训练模型
    Velodyne收购高清地图公司 将研发更安全的ADAS系统
  • 原文地址:https://www.cnblogs.com/liujitao79/p/8484797.html
Copyright © 2020-2023  润新知