Blockchain Smart Contracts with Solidity · บทเรียน

โครงสร้างและแจกแจง

จำลองข้อมูลที่ซับซ้อนใน Solidity ด้วยชนิดข้อมูลโครงสร้างแบบกำหนดเอง และแทนชุดสถานะตายตัวด้วยชนิดแจกแจง

บทเรียน 4 จาก 413 ขั้นตอน

โครงสร้างและแจกแจง เป็นบทเรียน Blockchain Smart Contracts with Solidity ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.

เริ่มต้นได้ฟรี

เรียนรู้ Blockchain Smart Contracts with Solidity ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “โครงสร้างและแจกแจง” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “โครงสร้างและแจกแจง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Blockchain Smart Contracts with Solidity ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “โครงสร้างและแจกแจง”

จำลองข้อมูลที่ซับซ้อนใน Solidity ด้วยชนิดข้อมูลโครงสร้างแบบกำหนดเอง และแทนชุดสถานะตายตัวด้วยชนิดแจกแจง คุณปฏิบัติ Blockchain Smart Contracts with Solidity ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Blockchain Smart Contracts with Solidity หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Blockchain Smart Contracts with Solidity บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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