合约的结构

Contracts in Solidity are similar to classes in object-oriented languages. Each contract can contain declarations of State Variables 状态变量, Functions 函数, Function Modifiers 函数修饰符, Events 事件, Struct Types 结构体类型 and Enum Types 枚举类型. Furthermore, contracts can inherit from other contracts.

Solidity合约与基于对象的语言定义类似, 每个合约可以包含 State Variables 状态变量 状态变量, Functions 函数 函数, Function Modifiers 函数修饰符 修饰符, Events 事件 事件, Struct Types 结构体类型 结构体类型以及 Enum Types 枚举类型 枚举. 此外, 合约还可以继承其他合约.

State Variables 状态变量

State variables are values which are permanently stored in contract storage. 状态变量是永久存储于合约之中的值.

pragma solidity ^0.4.0;

contract SimpleStorage {
    uint storedData; // State variable 状态变量
    // ...
}

See the 类型 section for valid state variable types and Visibility and Getters for possible choices for visibility.

查看 类型 类型部分, 看看支持哪些变量类型. 查看 Visibility and Getters 部分看看如何访问变量.

Functions 函数

Functions are the executable units of code within a contract.

函数是合约中可运行的计算单元.

pragma solidity ^0.4.0;

contract SimpleAuction {
    function bid() public payable { // Function
        // ...
    }
}

Function Calls can happen internally or externally and have different levels of visibility (Visibility and Getters) towards other contracts.

Function Calls 函数可以在内部/外部调用, 函数面向其他合约有不同等级的访问权限(Visibility and Getters)

Function Modifiers 函数修饰符

Function modifiers can be used to amend the semantics of functions in a declarative way (see Function Modifiers in contracts section).

修饰符通过声明的方式修改函数语义. (参考 Function Modifiers 修饰符一章).

pragma solidity ^0.4.11;

contract Purchase {
    address public seller;

    modifier onlySeller() { // Modifier
        require(msg.sender == seller);
        _;
    }

    function abort() public onlySeller { // Modifier usage
        // ...
    }
}

Events 事件

Events are convenience interfaces with the EVM logging facilities.

事件是实现EVM的日志功能的一个方便的接口.

pragma solidity ^0.4.0;

contract SimpleAuction {
    event HighestBidIncreased(address bidder, uint amount); // Event

    function bid() public payable {
        // ...
        HighestBidIncreased(msg.sender, msg.value); // Triggering event
    }
}

See Events in contracts section for information on how events are declared and can be used from within a dapp.

参考 Events 章节了解详细内容, 关于事件如何定义, 如何在dapp中使用.

Struct Types 结构体类型

Structs are custom defined types that can group several variables (see Structs in types section).

结构体是自定义数据类型, 将不同类型的变量组装成一个组. 参考 Structs 部分了解详细内容.

pragma solidity ^0.4.0;

contract Ballot {
    struct Voter { // Struct
        uint weight;
        bool voted;
        address delegate;
        uint vote;
    }
}

Enum Types 枚举类型

Enums can be used to create custom types with a finite set of values (see Enums in types section).

枚举用于创建自定义类型, 基于值的有限集合, 参考 Enums 部分.

pragma solidity ^0.4.0;

contract Purchase {
    enum State { Created, Locked, Inactive } // Enum
}