• solidity语言11


    函数修饰符

    pragma solidity ^0.4.11;
    
    contract owned {
        address owner;
    
        // 构造函数
        function owned() public { 
            owner = msg.sender;
        }
    
        // 此合约定义的函数修饰符不使用,用于衍生的合约
        modifier onlyOwner {
            require(msg.sender == owner);
            _; // 引用的函数体部分
        }
    }
    
    contract mortal is owned {
        function close() public onlyOwner {
            selfdestruct(owner);
        }
    }
    
        /* 相当于
        function close() public onlyOwner {
            require(msg.sender == owner);
            selfdestruct(owner);
        }
        */
    
    contract priced {
        modifier costs(uint price) {
            if (msg.value >= price) {
                _;
            }
        }
    }
    
    // 继承合约priced,owned
    contract Register is priced, owned {
        mapping (address => bool) registeredAddresses;
        uint price;
        
        function Register(uint initialPrice) public { 
            price = initialPrice; 
        }
    
        // 这里使用关键字payable很重要,否则函数将自动拒绝所有以太的转帐
        function register() public payable costs(price) {
            registeredAddresses[msg.sender] = true;
        }
    
        /* 相当于
        function register() public payable costs(price) {
            if (msg.value >= price) {
                registeredAddresses[msg.sender] = true;
            }
        }
        */
    
        function changePrice(uint _price) public onlyOwner {
            price = _price;
        }
    
        /* 相当于
        function changePrice(uint _price) public onlyOwner {
            require(msg.sender == owner);
            price = _price;
        }
        */
    }
    
    contract Mutex {
        bool locked;
        
        modifier noReentrancy() {
            require(!locked);
            locked = true;
            _;
            locked = false;
        }
    
        function f() public noReentrancy returns (uint) {
            require(msg.sender.call());
            return 7;
        }
    
        /* 相当于
        function f() public noReentrancy returns (uint) {
            require(!locked);
            locked = true;
    
            require(msg.sender.call());
            return 7;
            
            locked = false;
        }
        */
    }
    

    常量

    pragma solidity ^0.4.0;
    
    contract C {
        uint constant NUMER = 32 ** 22 + 8;
        string constant TEXT = "abc";
        bytes32 constant MYHASH = keccak256("abc");
    }
    
  • 相关阅读:
    推荐一本书 改善你的视力:跟眼镜说再见
    Gentoo中gcc4.1.2到gcc4.3.2的升级
    msbuild学习的一些相关链接
    SqlServer 2005安装问题
    Gentoo linux中安装php5运行环境
    sql 时间函数(全)
    asp.net中的对话框
    win7 资源管理器指向我的电脑
    C/C++ 位操作 总结
    【转】Java字节序转换
  • 原文地址:https://www.cnblogs.com/liujitao79/p/8484539.html
Copyright © 2020-2023  润新知