0Pricing
Mojo Academy · บทเรียน

เมธอด __copyinit__

กำหนดวิธีที่ชนิดข้อมูลของคุณคัดลอกตัวเอง

เมธอด __copyinit__ เป็นบทเรียน Mojo Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Mojo Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Defining How to Copy

When a value is copied, Mojo calls a special method named __copyinit__. It builds a fresh instance from an existing one. 📋

It Runs on Assignment

Every time a copy happens, like plain assignment, __copyinit__ fires. It is the hook that defines what copying your type means.

The Method Signature

It takes the new instance to fill and a read-only existing value to copy from. You set each field on the new one.

fn __copyinit__(out self, existing: Self):
    self.x = existing.x

Initialize Every Field

Inside __copyinit__ your job is to give the new instance valid values for all of its fields, usually mirrored from existing.

Deep vs Shallow Copies

For heap data you decide: copy the pointer, or allocate and clone the buffer. A true deep copy gives full independence.

Why Independence Matters

A correct __copyinit__ ensures the copy owns its own data. Then editing the copy never reaches back and changes the original.

Often Derived for You

You rarely write it by hand. Marking a type Copyable lets Mojo synthesize a field-by-field __copyinit__ automatically.

@fieldwise_init
struct Point(Copyable):
    var x: Int

When to Write It Yourself

Hand-write __copyinit__ only when the default is wrong, like when you manage a raw buffer that needs cloning.

It Defines Copy Semantics

This method is literally what makes your type copyable. Without it, Mojo cannot duplicate your value on assignment.

Keep It Correct and Cheap

A good __copyinit__ is correct first and lean second. Copy only what you must so duplication stays affordable.

A Building Block of Safety

Together with moves, __copyinit__ gives Mojo its predictable value semantics. You control exactly how data is duplicated.

Quick Check

Lets check the copy initializer.

Recap

__copyinit__ defines how your type copies itself, building an independent instance. Mojo often derives it, but you can customize it. 🎯

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

บทเรียน “เมธอด __copyinit__” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “เมธอด __copyinit__”

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

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Mojo Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Mojo Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “เมธอด __copyinit__” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Mojo Academy นี้ได้ไหม

ได้ บทเรียน Mojo Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ซีแมนติกส์ของการคัดลอกเทียบกับการย้าย
  2. เมธอด __copyinit__
  3. เมธอด __moveinit__
  4. การทำลายและ __del__
← กลับไปที่ Mojo Academy