0Pricing
Blockchain Smart Contracts with Solidity · درس

الهياكل والتعدادات

صمّم نماذج للبيانات المعقدة في Solidity باستخدام أنواع struct المخصصة، ومثّل مجموعات الحالات الثابتة باستخدام enum.

الهياكل والتعدادات درس مجاني في Blockchain Smart Contracts with Solidity على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Blockchain Smart Contracts with Solidity، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Blockchain Smart Contracts with Solidity 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Custom Types?

Beyond primitives like uint and address, Solidity lets you define your own types. Structs group related fields, and enums represent a fixed set of named states.

Declaring a Struct

A struct bundles several variables under one name, perfect for modeling real-world entities like a user or an order.

struct User {
    string name;
    uint age;
    address wallet;
}

Creating Struct Instances

You can construct a struct by positional arguments or by naming each field. Named arguments are clearer and less error prone.

User memory u = User({
    name: "Alice",
    age: 30,
    wallet: msg.sender
});

Structs in Storage vs Memory

A struct in storage persists on chain; in memory it is a temporary copy. Assigning a storage struct to a memory variable copies it, so changes do not write back.

Updating Struct Fields

To modify persisted data, work with a storage reference and assign to its fields directly.

User storage u = users[msg.sender];
u.age = 31;

Structs Inside Mappings

Structs shine when stored in a mapping keyed by address or id, giving each user their own record.

mapping(address => User) public users;
users[msg.sender] = User("Bob", 25, msg.sender);

Declaring an Enum

An enum defines a small set of named options. It improves readability over raw numbers for representing state.

enum Status { Pending, Shipped, Delivered, Cancelled }

Using Enum Values

Enum members are stored as uint starting at 0. You compare and assign them by name, which makes contract logic self-documenting.

Status public status = Status.Pending;

function ship() public {
    status = Status.Shipped;
}

Enums in State Machines

Enums are ideal for modeling a state machine. Guard transitions with require so an order can only move to a valid next state.

function deliver() public {
    require(status == Status.Shipped, "Not shipped yet");
    status = Status.Delivered;
}

Combining Structs and Enums

Put an enum field inside a struct to track each record state, for example storing a Status on every order struct so each order tracks its own lifecycle.

Gas Considerations

Each struct field is a storage slot, and writing storage is expensive. Order fields to pack smaller types together, and use enums (one byte range) instead of strings for fixed states to save gas.

Quick Check

Check your structs and enums knowledge.

Recap

You learned Solidity custom types:

  • Structs group related fields and are often stored in mappings
  • Mind storage vs memory when copying or updating structs
  • Enums model fixed states as integers and power state machines
  • Combine them and pack fields to save gas

Custom types make contracts more expressive and maintainable.

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

هل درس «الهياكل والتعدادات» مجاني؟

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

ماذا ستتعلم في «الهياكل والتعدادات»؟

صمّم نماذج للبيانات المعقدة في Solidity باستخدام أنواع struct المخصصة، ومثّل مجموعات الحالات الثابتة باستخدام enum. تتمرن على Blockchain Smart Contracts with Solidity مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Blockchain Smart Contracts with Solidity؟

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

كم من الوقت يستغرق درس «الهياكل والتعدادات»؟

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

هل يمكنني كتابة وتشغيل أكواد في درس Blockchain Smart Contracts with Solidity هذا؟

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

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

  1. أنواع البيانات والمتغيرات في Solidity
  2. بُنى التحكم والحلقات
  3. الدوال ومعدّلات الرؤية
  4. الهياكل والتعدادات
← العودة إلى Blockchain Smart Contracts with Solidity