الحقول ودالة __init__
هيّئ حالة البنية
الحقول ودالة __init__ درس مجاني في Mojo Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Mojo Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Mojo Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Fields Start Empty
Declaring fields gives a struct its shape, but it does not fill them in. You still need to set each field's starting value. 🧱
Meet __init__
The special method __init__ runs when you create an instance. Its job is to set up the struct's fields with real values.
fn __init__(out self):
self.x = 0
self.y = 0The self Parameter
Inside __init__, self refers to the new instance being built. You assign to self.field to give that instance its data.
self.x = 0Why out self
Writing out self tells Mojo this method is constructing the value, so self starts uninitialized and you must fill every field.
fn __init__(out self):Taking Arguments
An initializer usually takes arguments so each instance can start with different data passed in by the caller.
fn __init__(out self, x: Int, y: Int):
self.x = x
self.y = ySet Every Field
You must assign every field in __init__. Mojo will not let a struct exist with some fields left uninitialized.
Creating an Instance
To build a value, call the struct like a function. Mojo runs __init__ and hands you back a ready instance.
var p = Point(3, 4)Reading a Field
Once built, reach a field with dot access. So p.x reads the x you stored when the instance was created.
print(p.x, p.y)Arguments Map to Fields
A common pattern is naming arguments like the fields and assigning each across. This keeps the initializer easy to read.
self.x = x
self.y = yDefault Starting Values
You can give fields sensible defaults by assigning constants in __init__ when no useful argument is provided by the caller.
fn __init__(out self):
self.x = 0
self.y = 0The Full Picture
Fields declare what data exists; __init__ decides its starting value. Together they turn a blueprint into a usable instance.
struct Point:
var x: Int
var y: Int
fn __init__(out self, x: Int, y: Int):
self.x = x
self.y = yQuick Check
Let us confirm how initialization works.
Recap
You used __init__ to set up fields when an instance is born. Next you will add methods so your type can act, not just hold data. ✨
الأسئلة الشائعة
هل درس «الحقول ودالة __init__» مجاني؟
نعم — نص درس «الحقول ودالة __init__» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Mojo Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Mojo Academy 4 دروس في المجموع.
ماذا ستتعلم في «الحقول ودالة __init__»؟
هيّئ حالة البنية تتمرن على Mojo Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Mojo Academy؟
لا تُشترط خبرة سابقة. Mojo Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «الحقول ودالة __init__»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Mojo Academy هذا؟
نعم. كل درس في Mojo Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تعريف بنية struct
- الحقول ودالة __init__
- إضافة دوال إلى بنية
- البنى مقابل فئات Python