Kodax

سيارة بالتركيب (Composition) — تحدي Python

بدل الوراثة استخدم التركيب: صنف Engine(hp) فيه start() تُرجع "Engine {hp}hp started". صنف Car(model, engine) يحتوي محركاً (has-a) ودالة start() تُرجع "{model}: " متبوعة برسالة المحرك. أضف upgrade_engine(engine) لاستبدال المحرك.

class Engine:
    def __init__(self, hp):
        self.hp = hp

    def start(self):
        pass


class Car:
    def __init__(self, model, engine):
        pass

    def start(self):
        pass

    def upgrade_engine(self, engine):
        pass

حل هذا التحدي يرتبط بفتح دورة «البرمجة الكائنية» بحسابك.

ابدأ الحل