Kế thừa đa lớp

Lập trình Hướng đối tượng Nâng cao với Python

Jake Roach

Data Engineer

Kế thừa đa lớp

Cho phép một lớp kế thừa chức năng từ hơn một lớp

  • Intern có thể kế thừa cả EmployeeStudent
  • Duy trì quan hệ "là-một"

Lớp Intern kế thừa từ lớp Employee và 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)
Lập trình Hướng đối tượng Nâng cao với Python

Kế thừa đa lớp

class Intern(Employee, Student):
    def __init__(self, department, school, duration):
        # Gọi CẢ HAI constructor
        Employee.__init__(self, department)
        Student.__init__(self, school)
        self.duration = duration

    def onboard(self, mentor):
        # Cài đặt một phương thức mới
        ...
Lập trình Hướng đối tượng Nâng cao với Python

Tạo đối tượng 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"]
Lập trình Hướng đối tượng Nâng cao với Python

Kế thừa nhiều cấp

Kế thừa từ một lớp vốn đã kế thừa lớp khác → trở thành cháu

  • Duy trì quan hệ "là-một"

Minh họa kế thừa nhiều cấp.

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
Lập trình Hướng đối tượng Nâng cao với Python

Kế thừa nhiều cấp

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
Lập trình Hướng đối tượng Nâng cao với Python

Thứ tự phân giải phương thức (MRO)

Thứ tự Python quyết định phương thức nào dùng khi lớp cha và con có cùng tên phương thức

Quy tắc xác định MRO:

  • Tìm trong lớp con trước
  • Tìm các lớp cha từ trái sang phải theo khai báo trong câu lệnh class

$$

Phương thức .mro()__mro__

Lớp con kế thừa từ nhiều lớp cha.

1 Beyond the Basic Stuff with Python, Sweigart (tr. 311)
Lập trình Hướng đối tượng Nâng cao với Python

Thứ tự phân giải phương thức (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'>)
Lập trình Hướng đối tượng Nâng cao với Python

Ayo berlatih!

Lập trình Hướng đối tượng Nâng cao với Python

Preparing Video For Download...