您的位置:首页 > 其它

智能合约源文件的基本要素

2017-08-28 13:06 190 查看
合约类似面向对象语言中的类。

合约支持继承

智能合约的基本要素:状态变量(State Variables)、函数(Functions)、函数修饰符(Function Modifiers)、事件(Events)、结构类型(Structs Types)、枚举类型(Enum Types)。

1、状态变量(State Variables)

变量值会永久存储在合约的存储空间

pragma solidity ^0.4.0;

// simple store example

contract simpleStorage{

uint valueStore; //state variable

}


2、函数(Functions)

智能合约中的一个可执行单元

pragma solidity ^0.4.0;

contract simpleMath{
//Simple add function,try a divide action?
function add(uint x, uint y) returns (uint z){
z = x + y;
}
}
上述示例展示了一个简单的加法函数

3、函数修饰符(Function Modifiers)

函数修饰符用于增强语义

4、事件(Events)

事件是以太坊虚拟机(EVM)日志基础设施提供的一个便利接口,用于获取当前发生的事件。

pragma solidity ^0.4.0;

contract SimpleAuction {
event aNewHigherBid(address bidder, uint amount);

function  bid(uint bidValue) external {
aNewHigherBid(msg.sender, msg.value);
}
}
5、结构类型(Structs Types)

自定义的将几个变量组合在一起形成的类型

pragma solidity ^0.4.0;

contract Company {
//user defined `Employee` struct type
//group with serveral variables
struct employee{
string name;
uint age;
uint salary;
}

//User defined `manager` struct type
//group with serveral variables
struct manager{
employee employ;
string title;
}
}
6、枚举类型(Enum Types)

特殊的自定义类型,类型的所有值可枚举的情况

pragma solidity ^0.4.0;

contract Home {
enum Switch{On,Off}
}


这些就是智能合约的基本要素,写的粗略了些,以后研究了这些基本要素之后,会再补充的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  区块链 以太坊