zoukankan      html  css  js  c++  java
  • 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");
    }
    
  • 相关阅读:
    消息机制
    窗口!窗口!- Windows程序设计(SDK)003
    内联函数的作用
    结构体变量用 . 结构体指针用-> 的原因
    &a和a的区别
    分布电容
    介电常数
    天线
    封装的思想
    关于中断标志位
  • 原文地址:https://www.cnblogs.com/liujitao79/p/8484539.html
Copyright © 2011-2022 走看看