זה נושא דיון מלווה לערך המקורי ב- https://www.tocode.co.il/bundles/python/lessons/24-inheritance
זה נושא דיון מלווה לערך המקורי ב- https://www.tocode.co.il/bundles/python/lessons/24-inheritance
מה קורה עם נושאים נוספים של ירושה כמו functions override?
אין בעיה בפייתון לבצע Function Override כלומר לשנות מימוש של מתודה במחלקה יורשת. לדוגמה נתונות שתי מחלקות של דוחות אחת בסיסית ושנייה מדפיסה דוח ל PDF:
class Report:
def generate(self):
print("Generating report...")
self._render_content()
print("Report generated.")
def _render_content(self):
print("Rendering basic report content.")
class PDFReport(Report):
def _render_content(self):
print("Rendering PDF-specific report content with charts and layout.")
ופונקציה שמקבלת Report ומפרסמת אותו:
def publish_report(report: Report):
report.generate()
אז עכשיו אפשר להפעיל את הפונקציה עם שני סוגי הדוחות ולכל אחד לקבל את התוצאה הנכונה:
publish_report(Report())
publish_report(PDFReport())