0Pricing
Mojo Academy · درس

إضافة دوال إلى بنية

أضف سلوكًا إلى نوعك

إضافة دوال إلى بنية درس مجاني في Mojo Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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. 🧩

الأسئلة الشائعة

هل درس «إضافة دوال إلى بنية» مجاني؟

نعم — نص درس «إضافة دوال إلى بنية» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Mojo Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Mojo Academy 4 دروس في المجموع.

ماذا ستتعلم في «إضافة دوال إلى بنية»؟

أضف سلوكًا إلى نوعك تتمرن على Mojo Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Mojo Academy؟

لا تُشترط خبرة سابقة. Mojo Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «إضافة دوال إلى بنية»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Mojo Academy هذا؟

نعم. كل درس في Mojo Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تعريف بنية struct
  2. الحقول ودالة __init__
  3. إضافة دوال إلى بنية
  4. البنى مقابل فئات Python
← العودة إلى Mojo Academy