فهم الـ Classes في جافا: حجر الأساس في OOP

مرحباً بك في عالم البناء المنظم! تخيّل أنك تريد بناء مدينة. قبل أن تضع أيّ مبنى، تحتاج إلى مخطط هندسي يصف كيف سيكون هذا المبنى: عدد الغرف، لون الجدران، شكل النوافذ. في جافا، الـ Class هي هذا المخطط أو القالب الذي نصنع منه الـ Objects.

ببساطة، الـ Class هي وصف أو نموذج لأشياء من نفس النوع. فهي تحدد Properties (الصفات) والسلوكيات (الأفعال) التي ستتمتع بها الـ Objects المنتمية إليها.

هيكل الـ Class (Class Structure)

لنفكر في مثال واقعي: سيارة. ما هي الصفات المشتركة بين معظم السيارات؟ لون، موديل، سرعة قصوى. وما هي الأفعال التي يمكن للسيارة القيام بها؟ التحرك، التوقف، إصدار صوت. في جافا، نترجم الصفات إلى متغيرات (Variables) أو خصائص (Attributes/Fields)، ونترجم الأفعال إلى دوال (Methods).

// This is the blueprint (Class) for a car
public class Car {
    // Attributes - variables that describe the object's state
    String color;
    String model;
    int maxSpeed;

    // Behaviors (Methods) - functions that describe what the object can do
    void move() {
        System.out.println("The car is moving...");
    }

    void stop() {
        System.out.println("The car is stopping.");
    }
}

في الكود أعلاه، قمنا بتعريف Class اسمها Car. لاحظ أننا لم ننشئ سيارة فعلية بعد، بل أنشأنا فقط القالب الذي يمكننا من خلاله صنع أي عدد من السيارات لاحقاً.

كيف ننشئ Object من Class؟

الآن، لنستخدم مخططنا (الـ Class) لبناء سيارة حقيقية! نستخدم الكلمة المفتاحية new لإنشاء Object أو نسخة (Instance) من الـ Class.

public class Main {
    public static void main(String[] args) {
        // Create a Car object from the Car class
        Car myCar = new Car();

        // Now I can assign values to myCar properties
        myCar.color = "Red";
        myCar.model = "Toyota";
        myCar.maxSpeed = 200;

        // And call its methods (behaviors)
        myCar.move(); // Will print: The car is moving...
        myCar.stop(); // Will print: The car is stopping.

        // I can create another car (another object) from the same class
        Car yourCar = new Car();
        yourCar.color = "Blue";
        yourCar.model = "Honda";
        // Each object has its own independent data
    }
}

الخلاصة: Car هي الـ Class (القالب)، بينما myCar و yourCar هما two Objects مستقلان تم إنشاؤهما من ذلك القالب، ولكل منهما قيمه المخزنة في الذاكرة.

Constructor: المُهيئ الذكي

لاحظ في المثال السابق أننا أنشأنا السيارة أولاً (new Car())، ثم قمنا بتعيين خصائصها سطراً تلو الآخر. ماذا لو أردنا إنشاء سيارة وبتعيين خصائصها في نفس لحظة الإنشاء؟ هنا يأتي دور Constructor.

Constructor هو دالة خاصة داخل الـ Class تتم استدعاؤها تلقائياً عند إنشاء الـ Object باستخدام new. وظيفتها الأساسية هي تهيئة حالة الـ Object (تعيين القيم الأولية للخصائص).

public class Car {
    String color;
    String model;
    int maxSpeed;

    // This is the Constructor
    // 1. Its name matches the class name exactly.
    // 2. It has no return type (we don't write void or int...).
    public Car(String carColor, String carModel, int carMaxSpeed) {
        color = carColor;
        model = carModel;
        maxSpeed = carMaxSpeed;
        System.out.println("A new car has been created from the model: " + model);
    }

    void move() {
        System.out.println("The " + model + " is moving...");
    }
}

// Now in the main function
public class Main {
    public static void main(String[] args) {
        // Create an object using the constructor - we provide initial values directly
        Car myCar = new Car("Red", "Toyota", 200);
        // It will automatically print: A new car has been created from the model: Toyota

        myCar.move(); // It will print: The Toyota is moving...

        // Another object with different values
        Car yourCar = new Car("Blue", "Honda", 180);
    }
}

استخدام Constructor يجعل الكود أكثر تنظيماً وأماناً، حيث نضمن أن لكل Object خصائص مُهيأة عند ولادته.

ملخص الدرس

  • الـ Class: هي مخطط أو قالب نحدد فيه Properties (المتغيرات) والسلوكيات (الدوال) لـ Collection من الـ Objects المتشابهة.
  • الـ Object: هو نسخة فعلية (Instance) تم إنشاؤها من الـ Class، وله قيم حقيقية في الذاكرة.
  • الكلمة المفتاحية new: تُستخدم لإنشاء Object جديد من Class معينة.
  • Constructor: هو دالة خاصة تُستدعى عند إنشاء الـ Object لتهيئة قيمه الأولية. اسمه مطابق لاسم الـ Class وليس له نوع إرجاع.