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

การเพิ่มเมธอดให้ struct

เพิ่มพฤติกรรมให้ชนิดข้อมูลของคุณ

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

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

Data Plus Behavior

So far your struct only holds data. A method is a function that lives inside the struct and gives your type behavior. 🚀

Methods Live Inside

You write a method by defining a function indented inside the struct, right alongside its fields and __init__.

struct Point:
    var x: Int
    var y: Int
    fn show(self):
        print(self.x, self.y)

self Comes First

A method's first parameter is self, the instance it was called on. Through self the method reads and uses the struct's own fields.

fn show(self):
    print(self.x)

Calling a Method

You call a method with dot syntax on an instance. Mojo passes that instance in as self automatically for you.

var p = Point(3, 4)
p.show()

Methods Can Return Values

Like any function, a method can return a result. Declare the return type after an arrow and hand back a value.

fn sum(self) -> Int:
    return self.x + self.y

Methods Take Arguments Too

After self, a method can accept more arguments, just like a normal function, to work with extra data the caller provides.

fn scaled(self, factor: Int) -> Int:
    return self.x * factor

Read-Only by Default

A plain self parameter is borrowed, so the method can read fields but not change them. This keeps your data safe.

Mutating with mut self

To let a method change the instance, mark the parameter mut self. Now assignments to self.x actually update the value.

fn move_right(mut self, step: Int):
    self.x += step

Methods Build on Each Other

One method can call another through self, so you compose small behaviors into larger ones without repeating code.

fn report(self):
    print(self.sum())

Encapsulation in Action

Keeping fields and the methods that use them in one struct is encapsulation. The type owns both its data and how that data behaves.

A Struct That Does Work

Here a Point both stores coordinates and reports their sum. Your type is now active, not just a passive data container.

struct Point:
    var x: Int
    var y: Int
    fn sum(self) -> Int:
        return self.x + self.y

Quick Check

Let us check what you know about methods.

Recap

You gave your struct methods that read and even mutate its fields. Next you will compare Mojo structs with Python classes. 🧩

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

บทเรียน “การเพิ่มเมธอดให้ struct” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การเพิ่มเมธอดให้ struct”

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

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

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

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

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

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

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

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

  1. การกำหนด struct
  2. ฟิลด์และเมธอด __init__
  3. การเพิ่มเมธอดให้ struct
  4. Struct เทียบกับคลาส Python
← กลับไปที่ Mojo Academy