可見性或權限控制(Visibility And Accessors)
可見性
- external 外部可見性
可以被其它合約或發(fā)起交易的方式調用
在合約內部調用要使用this.f() - public 公開可見性
public類型的狀態(tài)變量,會自動創(chuàng)建一個訪問器 - internal 內部可見性
- private 私有可見性
| | 合約內部 | 繼承合約 | 外部合約 | 發(fā)起交易
| ------------ | ------------ | ------------ | ------------
| external | Y | Y | Y | Y
| public | Y | Y | Y | N
| internal | Y | Y | N | N
| private | Y | N | N | N
函數默認是
public
狀態(tài)變量默認的可見性是internal
訪問函數(Getter Functions)
public的狀態(tài)變量,編譯器會默認創(chuàng)建訪問函數。
contract C{
uint public data = 10;
}
變量data為public,所以創(chuàng)建訪問函數如下:
function data() view return (uint data)
外部可以使用C.data()訪問它。
注意:在外部函數中的狀態(tài)變量訪問,只能通過this.data()的方式。
比如:
contract C{
uint public c = 10;
function accessInternal() internal returns (uint){
return c;
}
function accessExternal() external returns (uint){
return this.c();
}
}
注意:public的mapping默認訪問參數是需要參數的,并不是之前說的訪問函數都是無參的。
如下:
mapping (address => uint256) public balanceOf;
function balanceOf(address _owner) view returns (uint256 balance)
函數修改器(Function Modifiers)
http://www.tryblockchain.org/Solidity-FunctionModifiers-%E5%87%BD%E6%95%B0%E4%BF%AE%E6%94%B9%E5%99%A8.html
修改器(Modifiers)可以用來輕易的改變一個函數的行為。比如用于在函數執(zhí)行前檢查某種前置條件。
-
修改器(Modifiers)可以被繼承 -
修改器(Modifiers)可以被重寫(override) -
修改器(Modifiers)可以接收參數的 -
_表示修改器(Modifiers)修飾的函數體替換位置
pragma solidity ^0.4.0;
contract owned {
function owned() { owner = msg.sender; }
address owner;
// This contract only defines a modifier but does not use
// it - it will be used in derived contracts.
// The function body is inserted where the special symbol
// "_;" in the definition of a modifier appears.
// This means that if the owner calls this function, the
// function is executed and otherwise, an exception is
// thrown.
modifier onlyOwner {
if (msg.sender != owner)
throw;
_;
}
}
contract mortal is owned {
// This contract inherits the "onlyOwner"-modifier from
// "owned" and applies it to the "close"-function, which
// causes that calls to "close" only have an effect if
// they are made by the stored owner.
function close() onlyOwner {
selfdestruct(owner);
}
}
contract priced {
// Modifiers can receive arguments:
modifier costs(uint price) {
if (msg.value >= price) {
_;
}
}
}
contract Register is priced, owned {
mapping (address => bool) registeredAddresses;
uint price;
function Register(uint initialPrice) { price = initialPrice; }
// It is important to also provide the
// "payable" keyword here, otherwise the function will
// automatically reject all Ether sent to it.
function register() payable costs(price) {
registeredAddresses[msg.sender] = true;
}
function changePrice(uint _price) onlyOwner {
price = _price;
}
}
例子:使用修改器實現的一個防重復進入鎖
pragma solidity ^0.4.0;
contract Mutex {
bool locked;
modifier noReentrancy() {
if (locked) throw;
locked = true;
_;
locked = false;
}
/// This function is protected by a mutex, which means that
/// reentrant calls from within msg.sender.call cannot call f again.
/// The `return 7` statement assigns 7 to the return value but still
/// executes the statement `locked = false` in the modifier.
function f() noReentrancy returns (uint) {
if (!msg.sender.call()) throw;
return 7;
}
}