本小节讨论Solidity的构造函数和析构函数
其中标记蓝色的部分为构造函数,标记红色的部分为析构函数,与java中的一样,
构造函数主要用于初始化,析构函数主要用于销毁,在构造函数中初始化amount为90
pragma solidity ^0.4.13;
contract MyCoin{
uint amount;
address owner;
function MyCoin() public{
amount=90;
owner=msg.sender;
}
function getBalance() public constant returns (uint){
return amount;
}
function kill() public{
if(owner==msg.sender){
selfdestruct(owner);
}
}
}
参考上一小节
在Run选项中,点击create 构建合约实例
点击第3步的getBalance,右边出现90,说明合约调用成功
点击kill,再次点击getBalance,会出现以下提示,说明合约对象已经销毁
作者:温春水 致力于研究区块链技术
微信:wencs1314 QQ群:612968783