1.状态变量
pragma solidity >=0.4.0 <0.6.0;
contract SimpleStorage {
uint storedData; //整型变量
// ......
}
2.函数
pragma solidity >=0.4.0 <0.6.0;
contract SimpleAuction {
function bid () public payable{
// ......
//同时函数还包括输入参数和返回类型说明
}
}
3.函数修饰器
pragma solidity >=0.4.22 <0.6.0;
contract Purchase{
address public seller;
//定义函数修饰器,该修饰器可以用于函数条件限制
modifier onlySeller() {
require{
masg.sender == seller,
"Only seller can this."
};
_;
}
}
4.事件
pragma solidity >=0.4.0 <0.6.0;
contract SimpleAuction {
event HighestBidIncreased (address bidder, uint amount) ;//事件原型
function bid () public payable{
// ......
emit HighestBidIncreased (msg.sender, msg.value) ; //触发事件
}
}
5.结构体
pragma solidity >=0.4.0 <0.6.0;
contract Ballot{
struct Voter{ //通过struct关键字,定义一个合约内的结构
uint weight; //结构体内定义的成员变量
bool voted;
address delegate;
uint vote;
}
}
6.枚举类型
pragma solidity >=0.4.0 <0.6.0;
contract Purchase{
enum State { Created, Locked, Inactive } //从0开始,定义枚举变量
}