> 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 } // 枚举类型

}

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://u.naturaldao.io/solidity/1.6-solidity-yu-yan-jiao-cheng/compound.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
