智能合約基本要素概覽
合約類似物件導向語言中的類。
支援繼承。
每個合約中可包含狀態變數(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