solidity语言11

it2022-05-05  228

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"); } posted on 2018-02-28 16:55 北京涛子 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/liujitao79/p/8484539.html


最新回复(0)