Vícenásobná dědičnost

Intermediate Object-Oriented Programming in Python

Jake Roach

Data Engineer

Vícenásobná dědičnost

Umožňuje třídě dědit funkcionalitu z více než jedné třídy

  • Intern může dědit z Employee i Student
  • Zachovává vztah „je-a"

Třída Intern dědící z tříd Employee a Student.

class Employee:
  def __init__(self, department):
    self.department = department

  def begin_job(self):
    print(f"Welcome to {self.department}!")
class Student:
  def __init__(self, school):
    self.school = school
    self.courses = []

  def add_course(self, course_name):
    self.courses.append(course_name)
Intermediate Object-Oriented Programming in Python

Vícenásobná dědičnost

class Intern(Employee, Student):
    def __init__(self, department, school, duration):
        # Make a call to BOTH constructors
        Employee.__init__(self, department)
        Student.__init__(self, school)
        self.duration = duration

    def onboard(self, mentor):
        # Implementation of a new method
        ...
Intermediate Object-Oriented Programming in Python

Vytvoření objektu Intern

stephen = Intern("Software Development", "Echo University", 10)
stephen.begin_job()  # Method from Employee
Welcome to Software Development!
stephen.add_course("Intermediate OOP in Python")  # Method from Intern
print(stephen.courses)
["Intermediate OOP in Python"]
Intermediate Object-Oriented Programming in Python

Víceúrovňová dědičnost

Dědění z třídy, která sama dědí z jiné třídy – třída se stává „vnoučetem"

  • Zachovává vztah „je-a"

Grafické znázornění víceúrovňové dědičnosti.

class Person:
  def __init__(self, name):
    self.name = name

  def introduce(self):
      print(f"Hello, my name is {self.name}")
class Employee(Person):
  def __init__(self, name, title):
    Person.__init__(self, name)
    self.title = title

  def change_position(self, new_title):
    print(f"Starting new role as {new_title}")
    self.title = new_title
Intermediate Object-Oriented Programming in Python

Víceúrovňová dědičnost

class Manager(Employee):
  def __init__(self, name, title, number_reports):
    Employee.__init__(self, name, title)
    self.number_reports = number_reports

mike = Manager("Mike", "Engineering Manager", 14)
mike.introduce()
mike.change_position("Director of Engineering")
print(mike.number_reports)
Hello, my name is Mike
Starting new role as Director of Engineering
14
Intermediate Object-Oriented Programming in Python

Pořadí rozlišení metod (MRO)

Pořadí, v němž Python určuje, která metoda se použije, pokud ji implementují rodič i potomek pod stejným názvem

Pravidla pro určení MRO:

  • Nejprve se prohledávají potomci
  • Rodičovské třídy se prohledávají zleva doprava podle pořadí v definici třídy

$$

Metoda .mro() a atribut __mro__

Podřízená třída dědící z více nadřazených tříd.

1 Beyond the Basic Stuff with Python, Sweigart (pg. 311)
Intermediate Object-Oriented Programming in Python

Pořadí rozlišení metod (MRO)

class Intern(Employee, Student):  # Intern inherits the Employee and the Student classes
  ...

# Find the MRO for Intern
print(Intern.mro())
[<class '__main__.Intern'>, <class '__main__.Employee'>, <class '__main__.Student'>, <class 'object'>]
# Find the MRO using __mro__
print(Intern.__mro__)
(<class '__main__.Intern'>, <class '__main__.Employee'>, <class '__main__.Student'>, <class 'object'>)
Intermediate Object-Oriented Programming in Python

Pojďme procvičovat!

Intermediate Object-Oriented Programming in Python

Preparing Video For Download...