0Pricing
Blockchain Smart Contracts with Solidity · 课时

结构体与枚举

使用自定义结构体类型在 Solidity 中建模复杂数据,并使用枚举表示固定的状态集合。

结构体与枚举 是 CoddyKit 上的免费 Blockchain Smart Contracts with Solidity 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Blockchain Smart Contracts with Solidity 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Blockchain Smart Contracts with Solidity 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Custom Types?

Beyond primitives like uint and address, Solidity lets you define your own types. Structs group related fields, and enums represent a fixed set of named states.

Declaring a Struct

A struct bundles several variables under one name, perfect for modeling real-world entities like a user or an order.

struct User {
    string name;
    uint age;
    address wallet;
}

Creating Struct Instances

You can construct a struct by positional arguments or by naming each field. Named arguments are clearer and less error prone.

User memory u = User({
    name: "Alice",
    age: 30,
    wallet: msg.sender
});

Structs in Storage vs Memory

A struct in storage persists on chain; in memory it is a temporary copy. Assigning a storage struct to a memory variable copies it, so changes do not write back.

Updating Struct Fields

To modify persisted data, work with a storage reference and assign to its fields directly.

User storage u = users[msg.sender];
u.age = 31;

Structs Inside Mappings

Structs shine when stored in a mapping keyed by address or id, giving each user their own record.

mapping(address => User) public users;
users[msg.sender] = User("Bob", 25, msg.sender);

Declaring an Enum

An enum defines a small set of named options. It improves readability over raw numbers for representing state.

enum Status { Pending, Shipped, Delivered, Cancelled }

Using Enum Values

Enum members are stored as uint starting at 0. You compare and assign them by name, which makes contract logic self-documenting.

Status public status = Status.Pending;

function ship() public {
    status = Status.Shipped;
}

Enums in State Machines

Enums are ideal for modeling a state machine. Guard transitions with require so an order can only move to a valid next state.

function deliver() public {
    require(status == Status.Shipped, "Not shipped yet");
    status = Status.Delivered;
}

Combining Structs and Enums

Put an enum field inside a struct to track each record state, for example storing a Status on every order struct so each order tracks its own lifecycle.

Gas Considerations

Each struct field is a storage slot, and writing storage is expensive. Order fields to pack smaller types together, and use enums (one byte range) instead of strings for fixed states to save gas.

Quick Check

Check your structs and enums knowledge.

Recap

You learned Solidity custom types:

  • Structs group related fields and are often stored in mappings
  • Mind storage vs memory when copying or updating structs
  • Enums model fixed states as integers and power state machines
  • Combine them and pack fields to save gas

Custom types make contracts more expressive and maintainable.

常见问题解答

「结构体与枚举」课时是免费的吗?

是的 — 「结构体与枚举」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Blockchain Smart Contracts with Solidity 课程的其余内容,请升级到 CoddyKit PRO。 Blockchain Smart Contracts with Solidity 课程共包含 4 节课。

「结构体与枚举」这节课中我会学到什么?

使用自定义结构体类型在 Solidity 中建模复杂数据,并使用枚举表示固定的状态集合。 你通过在浏览器中直接运行的动手代码来练习 Blockchain Smart Contracts with Solidity,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Blockchain Smart Contracts with Solidity 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Blockchain Smart Contracts with Solidity 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「结构体与枚举」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Blockchain Smart Contracts with Solidity 课中编写并运行代码吗?

能。每节 Blockchain Smart Contracts with Solidity 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Solidity 数据类型与变量
  2. 控制结构与循环
  3. 函数与可见性修饰符
  4. 结构体与枚举
← 返回 Blockchain Smart Contracts with Solidity