Py
Arpy
← العودة للدرس
🌙
🎯 تمارين عملية: الدرس 5
البرمجة كائنية التوجه - Object-Oriented Programming
📝 المهام (7 تمارين)
1
Class Definition
2
Create Objects
3
Class Attributes
4
Inheritance
5
Method Override
6
Encapsulation
7
__str__ Method
Create a class Person with __init__(self, name, age) and a method display_info()
أنشئ فئة Person مع __init__(self, name, age) ودالة display_info()
💡 استخدم class Person: ثم def __init__(self, name, age):
Create a class Car with brand and model attributes, then create two car objects
أنشئ فئة Car مع سمات brand و model، ثم أنشئ كائنين من السيارات
💡 car1 = Car("Toyota", "Corolla")
Create a class Car with class attribute wheels=4 and instance attribute color
أنشئ فئة Car مع سمة فئة wheels=4 وسمة كائن color
💡 سمة الفئة تُعرف خارج __init__ وسمة الكائن تُعرف داخل __init__ مع self
Create a parent class Animal with method speak(), and a child class Dog that inherits from Animal
أنشئ فئة أب Animal مع دالة speak()، وفئة ابن Dog ترث من Animal
💡 استخدم class Dog(Animal): للوراثة
Create class Animal with speak(), and class Dog(Animal) that overrides speak() to return "Bark!"
أنشئ فئة Animal مع speak()، وفئة Dog(Animal) تعيد تعريف speak() لترجع "Bark!"
💡 عرّف speak() في Dog بنفس الاسم لتعديل السلوك
Create a class Student with protected attribute _age and methods get_age() and set_age()
أنشئ فئة Student مع سمة محمية _age ودالتين get_age() و set_age()
💡 السمة المحمية تبدأ بـ _ واحد
Create a class Book with title and author, and implement __str__ method to return "Book: {title} by {author}"
أنشئ فئة Book مع title و author، ونفذ دالة __str__ لترجع "Book: {title} by {author}"
💡 def __str__(self): return f"Book: {self.title} by {self.author}"
# Output will appear here... # النتيجة ستظهر هنا...