使用修饰器modifier可以轻松改变函数的行为。 例如,它们可以在执行函数之前自动检查某个条件。
contract owned {
address owner;
//修饰器所修饰的函数体会被插入到特殊符号 _; 的位置。
modifier onlyOwner {
require(
msg.sender == owner,
"Only owner can call this function."
);
_;
}
//只有合约的创建者才能销毁合约
function destroy() public onlyOwner {
selfdestruct(owner);
}
}