> For the complete documentation index, see [llms.txt](https://u.naturaldao.io/solidity/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://u.naturaldao.io/solidity/1.6-solidity-yu-yan-jiao-cheng/compound.md).

# 1.6.2 智能合约的组成部分

在Solidity中，智能合约很类似面向对象编程语言中的类。每个智能合约包含状态变量，函数，函数修饰符，事件，结构类型，枚举类型等的声明。一个智能合约还可以继承自其它智能合约。

## 1.6.2.1 状态变量（State Variables）

状态变量永久存储在智能合约的存储中。如下所示：

pragma solidity ^0.4.0;

contract SimpleStorage {

&#x20;uint storedData; // 状态变量

&#x20;// ...

}

## 1.6.2.2 函数（Functions）

函数是合约中一个可执行的代码单元。

pragma solidity ^0.4.0;

contract SimpleAuction {

&#x20;function bid() public payable { // 函数

&#x20;// ...

&#x20;}

}

函数可以从内部或外部被调用，函数有不同的可见性。

## 1.6.2.3 函数修饰符（Function Modifiers）

函数修饰符可用于改变函数的语义，其用法类似函数的定义，如下所示：

pragma solidity ^0.4.0;

contract Purchase {

&#x20;address public seller;

&#x20;modifier onlySeller() { // 函数修饰符

&#x20;require(

&#x20;msg.sender == seller,

&#x20;"Only seller can call this."

&#x20;);

&#x20;\_;

&#x20;}

&#x20;function abort() public onlySeller { // Modifier usage

&#x20;// ...

&#x20;}

}

## 1.6.2.4 事件（Events）

事件是以太坊虚拟机的日志接口，可用于记录系统信息。

pragma solidity ^0.4.0;

contract SimpleAuction {

&#x20;event HighestBidIncreased(address bidder, uint amount); // 事件

&#x20;function bid() public payable {

&#x20;// ...

&#x20;emit HighestBidIncreased(msg.sender, msg.value); // 触发事件

&#x20;}

}

## 1.6.2.5 结构体类型（Struct Types）

结构体是用户自定义的数据类型，可包含各种类型的数据，如下所示：

pragma solidity ^0.4.0;

contract Ballot {

&#x20;struct Voter { // 结构类型

&#x20;uint weight;

&#x20;bool voted;

&#x20;address delegate;

&#x20;uint vote;

&#x20;}

}

## 1.6.2.6 枚举类型（Enum Types）

枚举类型可被用于定义一组有限常量的集合。

pragma solidity ^0.4.0;

contract Purchase {

&#x20;enum State { Created, Locked, Inactive } // 枚举类型

}

在下面的章节中我们将分别就这些具体的类型作详细介绍。
