فئات وخصائص expect/actual
بالإضافة إلى الدوال: تعريفات يمكن إعلانها باستخدام expect/actual
فئات وخصائص expect/actual درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Beyond Functions
expect/actual is not limited to functions. You can also declare whole classes, properties, and even objects that differ per platform.
An expect Property
Need a value that each platform defines? Declare an expect val in commonMain and let every target supply the actual value.
expect val platformName: StringEach Target Sets It
Android and iOS each provide an actual val. The property reads like a constant in shared code but resolves per platform.
actual val platformName: String = "iOS"An expect Class
For richer behavior, declare an expect class. It lists the members shared code can use, while each platform fills in the bodies.
expect class Platform() {
val name: String
}The Android actual Class
The androidMain actual class implements every declared member. Here name returns an Android-specific string.
actual class Platform actual constructor() {
actual val name = "Android"
}Match Members Exactly
Each actual class must declare every member the expect promised, with the same names and types. Missing one is a compile error.
Constructors Need actual Too
If the expect class has a constructor, mark the platform one as an actual constructor. The compiler checks that they line up.
Use It Like Any Class
In shared code you just create and use it. The right actual backs the object, so common logic stays unaware of the platform.
val name = Platform().nameAdd Platform-Only Extras
An actual class may add extra private members beyond the expect. Only what the expect declares is visible to shared code.
Prefer the Smallest Tool
Use an expect property for a value, an expect function for an action, and an expect class only when you need state plus behavior. ✨
A Familiar Modern Note
Recent Kotlin nudges you toward interfaces plus expect/actual functions over expect classes, but classes still work well today.
Quick Check
Consider what an actual class owes its expect.
Recap
You saw that expect/actual covers properties and classes too. Declare the shape in commonMain, then fulfill every member in each platform's actual. 🎉
الأسئلة الشائعة
هل درس «فئات وخصائص expect/actual» مجاني؟
نعم — نص درس «فئات وخصائص expect/actual» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «فئات وخصائص expect/actual»؟
بالإضافة إلى الدوال: تعريفات يمكن إعلانها باستخدام expect/actual تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «فئات وخصائص expect/actual»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- لماذا توجد expect/actual
- الحصول على اسم المنصة لكل هدف
- فئات وخصائص expect/actual
- الأخطاء الشائعة وأخطاء المترجم