Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

15.5. 合約開發

15.5.1. 構造方法

在 Truffer 中部署構造方法需要參數傳遞例子如下,MyContract 需要傳遞參數 _name:

			
pragma solidity ^0.4.19;

contract MyContract {

    string name;

    function MyContract(string _name) public{
        name = _name;
    }

    function getName() public view returns (string) {
        return name;
    }
}
			
			

migrations/3_initial_migration.js

			
var MyContract = artifacts.require("./MyContract.sol");

module.exports = function(deployer) {
  deployer.deploy(MyContract,"Netkiller");
};			
			
			

給構造方法傳遞變數的方法是 deployer.deploy(MyContract,arg1, arg2, ...); arg1 是傳遞的參數。

多個合約傳遞方法是:

			
deployer.deploy([
  [ContractA, arg1, arg2, ...],
  ContractB,
  [ContractC, arg1]
]);