Home | 简体中文 | 繁体中文 | 杂文 | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | Github | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

9.5. 合约开发

9.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]
]);