solidity-4.可見性

可見性或權限控制(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;
    }
}
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 原文:Smart contracts 正如我們在[intro]中看到的那樣,以太坊中有兩種不同類型的帳戶:外部擁有...
    Jisen閱讀 5,117評論 1 7
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現,斷路器,智...
    卡卡羅2017閱讀 136,533評論 19 139
  • 說起這個片,這算是我的“說看就看”的第一個電影了。本來都到家了,結果和好友遠程相約,說看就看,就又回店里二樓,打開...
    西嶺雪2025閱讀 313評論 1 0
  • 前面的入門程序展示了如何從Result獲取對象,然后進行對象的映射這篇文章主要從下面的幾個方面講解: Result...
    Mrsunup閱讀 4,800評論 0 0
  • 雖然今天遇到些不太開心的事情,但看到熱熔膠槍到了,還是一掃不快的殘云,先拿開心果練起了手。 酒瓶子是前陣子在7fr...
    子維馨閱讀 444評論 4 2

友情鏈接更多精彩內容