【Constructor】

建構子(constructor)是一個在合約被創建時只會跑一次的函數,像是佈署到鏈上時,可用於初始化或宣告某些我們希望一開始就執行的變數。

contract myContract {
    uint a;
    constructor() public {
        a = 0;
    }
}

關於建構子有以下規定:

  1. 一個 Contract 只能有一個建構子。

  2. 可視性可以為 publicinternal

  3. 如果一個 Contract 被標示為 internal,則其為 Abstract Contract

  4. constructor 也可以像普通函數一樣有參數。

Last updated

Was this helpful?