الدرس الثامن: البرمجة الكائنية (Object-Oriented Programming - OOP) في Python

الدرس الثامن: البرمجة الكائنية (Object-Oriented Programming - OOP) في Python
الدرس الثامن: البرمجة الكائنية (Object-Oriented Programming - OOP) في Python

 

الدرس الثامن: البرمجة الكائنية (Object-Oriented Programming - OOP) في Python

مقدمة:

البرمجة الكائنية هي أسلوب برمجي قوي بيساعدنا نصمم برامج معقدة بطريقة منظمة وسهلة للتطوير. باستخدام OOP، بنقدر نمثل البيانات ككائنات تحتوي على خصائص (Attributes) وسلوكيات (Methods). في الدرس ده، هنتعلم أساسيات OOP، زي إنشاء الفئات (Classes)، استخدام الكائنات (Objects)، والوراثة (Inheritance).


أولاً: ما هي البرمجة الكائنية؟

OOP هي طريقة لتقسيم البرنامج إلى أجزاء صغيرة (كائنات) تمثل أشياء من الواقع أو أفكار منطقية. كل كائن بيكون عنده:

  1. خصائص (Attributes): معلومات عن الكائن.
  2. سلوكيات (Methods): وظائف أو أفعال يقوم بها الكائن.

ثانيًا: إنشاء الفئات (Classes) والكائنات (Objects)

1. تعريف الفئة (Class)

الفئة هي قالب (Template) بيحدد شكل الكائن.

مثال:


class Car:
# Method to initialize the object
def __init__(self, brand, model, year):
self.brand = brand # Attribute
self.model = model
self.year = year
# Method to display car details
def display_info(self):
print(f"{self.brand} {self.model}, Year: {self.year}")

2. إنشاء كائن (Object)

الكائن هو نسخة من الفئة.


my_car = Car("Toyota", "Corolla", 2020)
my_car.display_info()

الناتج:


Toyota Corolla, Year: 2020

ثالثًا: الخصائص (Attributes) والوظائف (Methods)

1. التعامل مع الخصائص

نقدر نخزن بيانات داخل الكائن باستخدام الخصائص:


my_car.year = 2021 # تعديل خاصية
print(my_car.year) # عرض خاصية

2. كتابة وظائف داخل الفئة

الوظائف (Methods) بتعبر عن سلوك الكائن.


class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
print(f"{self.name} says: Woof Woof!")
my_dog = Dog("Buddy", 3)
my_dog.bark()

الناتج:


Buddy says: Woof Woof!

رابعًا: المفاهيم الأساسية في OOP

1. الوراثة (Inheritance)

الوراثة بتتيح لنا إنشاء فئة جديدة بناءً على فئة موجودة.

مثال:


class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print("This animal speaks!")
class Cat(Animal): # Cat inherits from Animal
def speak(self):
print(f"{self.name} says: Meow!")
my_cat = Cat("Luna")
my_cat.speak()

الناتج:

Luna says: Meow!

2. التغليف (Encapsulation)

بيساعدنا نحافظ على البيانات الخاصة داخل الكائن.


class BankAccount:
def __init__(self, balance):
self.__balance = balance # Private attribute
def deposit(self, amount):
self.__balance += amount
def withdraw(self, amount):
if amount <= self.__balance:
self.__balance -= amount
else:
print("Insufficient balance!")
def get_balance(self):
return self.__balance
account = BankAccount(1000)
account.deposit(500)
print(account.get_balance()) # الناتج: 1500

3. تعدد الأشكال (Polymorphism)

بيسمح باستخدام نفس الدالة (Method) بطرق مختلفة في الفئات الفرعية.


class Bird:
def fly(self):
print("Flying in the sky!")
class Penguin(Bird):
def fly(self):
print("Penguins can't fly!")
sparrow = Bird()
penguin = Penguin()
sparrow.fly() # الناتج: Flying in the sky!
penguin.fly() # الناتج: Penguins can't fly!

خامسًا: أمثلة عملية على OOP

1. نظام إدارة الموظفين:


class Employee:
def __init__(self, name, position, salary):
self.name = name
self.position = position
self.salary = salary
def display_details(self):
print(f"Name: {self.name}, Position: {self.position}, Salary: {self.salary}")
employee1 = Employee("Ahmed", "Developer", 5000)
employee1.display_details()

2. حساب مساحات الأشكال:


class Shape:
def area(self):
pass
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2
rect = Rectangle(4, 5)
circle = Circle(3)
print("Rectangle Area:", rect.area())
print("Circle Area:", circle.area())

3. محاكاة ماكينة قهوة:


class CoffeeMachine:
def __init__(self):
self.water = 1000 # mL
self.coffee = 500 # g
def make_coffee(self, cups):
if self.water >= cups * 200 and self.coffee >= cups * 10:
self.water -= cups * 200
self.coffee -= cups * 10
print(f"Made {cups} cup(s) of coffee!")
else:
print("Not enough water or coffee!")
machine = CoffeeMachine()
machine.make_coffee(3)

خاتمة:

في هذا الدرس، تعرفنا على أساسيات البرمجة الكائنية (OOP) في Python، بما في ذلك الفئات، الكائنات، والمفاهيم الرئيسية زي الوراثة والتغليف. في الدرس القادم، هنستعرض التعامل مع الملفات (File Handling) لإدارة البيانات وحفظها.

تعليقات