Solidity官方文檔學習筆記(2)

來源:互聯網
上載者:User
智能合約基本要素概覽

合約類似物件導向語言中的類。

支援繼承。

每個合約中可包含狀態變數(State Variables),函數(Functions),函數修飾符(Function Modifiers),事件(Events),結構類型(Structs Types)和枚舉類型(Enum Types)。 狀態變數(State Variables) 變數值會永久儲存在合約的儲存空間

// simple store examplecontract simpleStorage{    uint valueStore; //state variable}

詳情見類型(Types)章節,關於所有支援的類型和變數相關的可見度(Visibility and Accessors)。

函數(Functions) 智能合約中的一個可執行單元。

contract simpleMath{    //Simple add function,try a divide action?    function add(uint x, uint y) returns (uint z){        z = x + y;    }}
上述樣本展示了一個簡單的加法函數。
函數調用可以設定為內部(Internal)的和外部(External)的。同時對於其它合約的不同層級的可見度和存取控制(Visibility and Accessors)。具體的情況詳見後面類型中關於函數的內容。
函數修飾符(Function Modifiers) 函數修飾符用於增強語義。

事件(Events) 事件是以太坊虛擬機器(EVM)日誌基礎設施提供的一個便利介面。用於擷取當前發生的事件。

contract SimpleAuction {    event aNewHigherBid(address bidder, uint amount);        function  bid(uint bidValue) external {        aNewHigherBid(msg.sender, msg.value);    }}
關於事件如何聲明和使用。
結構體類型(Structs Types)
自訂的將幾個變數組合在一起形成的類型。
contract Company {    //user defined `Employee` struct type    //group with serveral variables    struct employee{        string name;        uint age;        uint salary;    }        //User defined `manager` struct type    //group with serveral variables    struct manager{        employee employ;        string title;    }}

枚舉類型(Enum) 特殊的自訂類型,類型的所有值可枚舉的情況。
contract Home {    enum Switch{On,Off}}

附錄學習參考連結: http://wiki.jikexueyuan.com/project/solidity-zh/introduction-smart-contracts.html http://www.tryblockchain.org/智能合約要素.html


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.