pragma solidity ^0.4.4;
contract Counter {
uint count = 0;
// 合约拥有者
address owner;
// 构造函数(constructor)来初始化合约对象
// 构造函数就是方法名和合约名字相同的函数
// 创建合约时会调用构造函数对状态变量进行数据初始化操作。
// 同一个合约同时只能拥有一个构造函数
function Counter() {
owner = msg.sender;
}
function increment() public {
// 只允许创建者调用
if (owner == msg.sender) {
count = count + 1;
}
}
function getCount() constant returns (uint) {
return count;
}
// 如果一个合约被销毁,那么当前地址指向的是一个僵尸对象,这个僵尸对象调用任何方法都会抛出异常。
// 你想销毁合约,需要调用selfdestruct(address)才能将其进行销毁。
function kill() {
if (owner == msg.sender) { // 检查谁在调用
selfdestruct(owner); // 销毁合约
}
}
}
区块链 以太坊 智能合约 如何销毁 废弃 selfdestruct
猜你喜欢
转载自blog.csdn.net/u013288190/article/details/112748808
今日推荐
周排行