Đa kế thừa có được phép trong Python không?

Trong chương trước của hướng dẫn, chúng ta đã đề cập đến tính kế thừa, hay cụ thể hơn là "kế thừa đơn lẻ". Như chúng ta đã thấy, trong trường hợp này, một lớp kế thừa từ một lớp. Mặt khác, đa kế thừa là một tính năng trong đó một lớp có thể kế thừa các thuộc tính và phương thức từ nhiều hơn một lớp cha. Các nhà phê bình chỉ ra rằng đa thừa kế đi kèm với mức độ phức tạp và mơ hồ cao trong các tình huống như bài toán kim cương. Chúng ta sẽ giải quyết vấn đề này sau trong chương này

Định kiến ​​phổ biến rằng đa kế thừa là một thứ gì đó "nguy hiểm" hoặc "xấu" hầu hết được nuôi dưỡng bởi các ngôn ngữ lập trình với cơ chế đa kế thừa được triển khai kém và trên hết là do sử dụng nó không đúng cách. Java thậm chí không hỗ trợ đa kế thừa, trong khi C++ hỗ trợ nó. Python có một cách tiếp cận phức tạp và được thiết kế tốt để đa kế thừa

Đa kế thừa có được phép trong Python không?

Một định nghĩa lớp, trong đó một lớp con SubClassName kế thừa từ các lớp cha BaseClass1, BaseClass2, BaseClass3, v.v., trông như thế này

class SubclassName(BaseClass1, BaseClass2, BaseClass3, ...):
    pass

Rõ ràng là tất cả các lớp cha BaseClass1, BaseClass2, BaseClass3,. cũng có thể kế thừa từ các lớp cha khác. Những gì chúng ta nhận được là một cây thừa kế

Đa kế thừa có được phép trong Python không?

Đào tạo Python trực tiếp

Đa kế thừa có được phép trong Python không?

Thưởng thức trang này?

Nhìn thấy. Tổng quan về các khóa học Python trực tiếp

đăng ký tại đây

Thí dụ. LịchĐồng hồ

Chúng tôi muốn giới thiệu các nguyên tắc đa thừa kế với một ví dụ. Với mục đích này, chúng tôi sẽ triển khai cho các lớp độc lập. lớp "Đồng hồ" và lớp "Lịch". Sau đó, chúng tôi sẽ giới thiệu một lớp "CalendarClock", đúng như tên gọi của nó, là sự kết hợp của "Clock" và "Calendar". CalendarClock kế thừa cả từ "Đồng hồ" và "Lịch"

Đa kế thừa có được phép trong Python không?

Lớp Clock mô phỏng tiếng tích tắc của đồng hồ. Một thể hiện của lớp này chứa thời gian, được lưu trữ trong thuộc tính self. giờ, tự. phút và bản thân. giây. Về cơ bản, chúng ta có thể viết phương thức

x = Clock(24, 45, 17)
6 và phương thức set như thế này

def __init__(self,hours=0, minutes=0, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
    def set(self,hours, minutes, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds

Chúng tôi đã quyết định không triển khai điều này vì chúng tôi đã thêm mã bổ sung để kiểm tra tính hợp lý của dữ liệu thời gian vào phương thức đã đặt. Chúng tôi cũng gọi phương thức set từ phương thức

x = Clock(24, 45, 17)
6 vì chúng tôi muốn tránh mã dự phòng. Lớp Đồng hồ hoàn chỉnh

""" 
The class Clock is used to simulate a clock.
"""
class Clock(object):
    def __init__(self, hours, minutes, seconds):
        """
        The paramaters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        self.set_Clock(hours, minutes, seconds)
    def set_Clock(self, hours, minutes, seconds):
        """
        The parameters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        if type(hours) == int and 0 <= hours and hours < 24:
            self._hours = hours
        else:
            raise TypeError("Hours have to be integers between 0 and 23!")
        if type(minutes) == int and 0 <= minutes and minutes < 60:
            self.__minutes = minutes 
        else:
            raise TypeError("Minutes have to be integers between 0 and 59!")
        if type(seconds) == int and 0 <= seconds and seconds < 60:
            self.__seconds = seconds
        else:
            raise TypeError("Seconds have to be integers between 0 and 59!")
    def __str__(self):
        return "{0:02d}:{1:02d}:{2:02d}".format(self._hours,
                                                self.__minutes,
                                                self.__seconds)
    def tick(self):
        """
        This method lets the clock "tick", this means that the 
        internal time will be advanced by one second.
        Examples:
        >>> x = Clock(12,59,59)
        >>> print(x)
        12:59:59
        >>> x.tick()
        >>> print(x)
        13:00:00
        >>> x.tick()
        >>> print(x)
        13:00:01
        """
        if self.__seconds == 59:
            self.__seconds = 0
            if self.__minutes == 59:
                self.__minutes = 0
                if self._hours == 23:
                    self._hours = 0
                else:
                    self._hours += 1
            else:
                self.__minutes += 1
        else:
            self.__seconds += 1
if __name__ == "__main__":
    x = Clock(23,59,59)
    print(x)
    x.tick()
    print(x)
    y = str(x)
    print(type(y))

ĐẦU RA

23:59:59
00:00:00

Hãy kiểm tra việc xử lý ngoại lệ của chúng ta bằng cách nhập số float và chuỗi làm đầu vào. Chúng tôi cũng kiểm tra xem điều gì sẽ xảy ra nếu chúng tôi vượt quá giới hạn của các giá trị dự kiến

x = Clock(7.7, 45, 17)

ĐẦU RA

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1x = Clock(7.7,45,17)
 in __init__(self, hours, minutes, seconds)
     14         """
     15 
---> 16self.set_Clock(hours, minutes, seconds)
     17 
     18     def set_Clock(self, hours, minutes, seconds):
 in set_Clock(self, hours, minutes, seconds)
     28             self._hours = hours
     29         else:
---> 30raise TypeError("Hours have to be integers between 0 and 23!")
     31         if type(minutes) == int and 0 <= minutes and minutes < 60:
     32             self.__minutes = minutes
TypeError: Hours have to be integers between 0 and 23!

x = Clock(24, 45, 17)

ĐẦU RA

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1x = Clock(24, 45, 17)
 in __init__(self, hours, minutes, seconds)
     14         """
     15 
---> 16self.set_Clock(hours, minutes, seconds)
     17 
     18     def set_Clock(self, hours, minutes, seconds):
 in set_Clock(self, hours, minutes, seconds)
     28             self._hours = hours
     29         else:
---> 30raise TypeError("Hours have to be integers between 0 and 23!")
     31         if type(minutes) == int and 0 <= minutes and minutes < 60:
     32             self.__minutes = minutes
TypeError: Hours have to be integers between 0 and 23!

x = Clock(24, 45, 17)
0

ĐẦU RA

x = Clock(24, 45, 17)
1

def __init__(self,hours=0, minutes=0, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
    def set(self,hours, minutes, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
0

ĐẦU RA

def __init__(self,hours=0, minutes=0, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
    def set(self,hours, minutes, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
1

def __init__(self,hours=0, minutes=0, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
    def set(self,hours, minutes, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
2

ĐẦU RA

def __init__(self,hours=0, minutes=0, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
    def set(self,hours, minutes, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
3

Bây giờ chúng ta sẽ tạo một lớp "Calendar", lớp này có nhiều điểm tương đồng với lớp Clock đã được định nghĩa trước đó. Thay vì "đánh dấu", chúng tôi có một phương thức "nâng cao", tăng ngày trước một ngày, bất cứ khi nào nó được gọi. Thêm một ngày vào một ngày là khá khó khăn. Chúng ta phải kiểm tra xem ngày đó có phải là ngày cuối cùng trong tháng và số ngày trong các tháng có khác nhau không. Như thể điều này vẫn chưa đủ tồi tệ, chúng ta có vấn đề về tháng Hai và năm nhuận

Quy tắc tính năm nhuận như sau

  • Nếu một năm chia hết cho 400, đó là một năm nhuận
  • Nếu một năm không chia hết cho 400 nhưng chia hết cho 100 thì đó không phải là năm nhuận
  • Năm chia hết cho 4 nhưng không chia hết cho 100 là năm nhuận
  • Tất cả các số năm khác là năm chung, tôi. e. không có năm nhuận

Là một mánh lới quảng cáo hữu ích, chúng tôi đã thêm khả năng xuất ngày theo kiểu Anh hoặc kiểu Mỹ (Canada)

def __init__(self,hours=0, minutes=0, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
    def set(self,hours, minutes, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
4

ĐẦU RA

def __init__(self,hours=0, minutes=0, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
    def set(self,hours, minutes, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
5

Cuối cùng, chúng tôi sẽ giới thiệu ví dụ đa thừa kế của chúng tôi. Bây giờ chúng tôi có khả năng triển khai lớp CalendarClock được dự định ban đầu, lớp này sẽ kế thừa từ cả Đồng hồ và Lịch. Phương thức "tick" của Đồng hồ sẽ phải được ghi đè. Tuy nhiên, phương thức đánh dấu mới của CalendarClock phải gọi phương thức đánh dấu của Đồng hồ. Cái đồng hồ. đánh dấu (bản thân)

def __init__(self,hours=0, minutes=0, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
    def set(self,hours, minutes, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
6

ĐẦU RA

def __init__(self,hours=0, minutes=0, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
    def set(self,hours, minutes, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
7

Vấn đề Kim cương hay,,viên kim cương chết người''

Đa kế thừa có được phép trong Python không?

"Vấn đề kim cương" (đôi khi được gọi là "vấn đề kim cương chết người") là thuật ngữ thường được sử dụng cho sự mơ hồ phát sinh khi hai lớp B và C kế thừa từ lớp cha A và lớp D khác kế thừa từ cả B và C. Nếu có một phương thức "m" trong A mà B hoặc C (hoặc thậm chí cả hai) đã ghi đè và hơn nữa, nếu nó không ghi đè phương thức này, thì câu hỏi đặt ra là D sẽ kế thừa phiên bản nào của phương thức?

Hãy xem Python. Cấu hình Vấn đề Kim cương đầu tiên là như thế này. Cả B và C đều ghi đè phương thức m của A

def __init__(self,hours=0, minutes=0, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
    def set(self,hours, minutes, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
8

Nếu bạn gọi phương thức m trên một thể hiện x của D, i. e. x. m(), chúng ta sẽ nhận được kết quả là "m của B được gọi". Nếu chúng ta chuyển đổi thứ tự của các lớp trong tiêu đề lớp của D trong "lớp D(C,B). ", chúng ta sẽ nhận được đầu ra "m của C được gọi là"

Trường hợp m sẽ chỉ bị ghi đè ở một trong các lớp B hoặc C, e. g. trong C

def __init__(self,hours=0, minutes=0, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
    def set(self,hours, minutes, seconds=0):
        self._hours = hours
        self.__minutes = minutes
        self.__seconds = seconds
9

ĐẦU RA

""" 
The class Clock is used to simulate a clock.
"""
class Clock(object):
    def __init__(self, hours, minutes, seconds):
        """
        The paramaters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        self.set_Clock(hours, minutes, seconds)
    def set_Clock(self, hours, minutes, seconds):
        """
        The parameters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        if type(hours) == int and 0 <= hours and hours < 24:
            self._hours = hours
        else:
            raise TypeError("Hours have to be integers between 0 and 23!")
        if type(minutes) == int and 0 <= minutes and minutes < 60:
            self.__minutes = minutes 
        else:
            raise TypeError("Minutes have to be integers between 0 and 59!")
        if type(seconds) == int and 0 <= seconds and seconds < 60:
            self.__seconds = seconds
        else:
            raise TypeError("Seconds have to be integers between 0 and 59!")
    def __str__(self):
        return "{0:02d}:{1:02d}:{2:02d}".format(self._hours,
                                                self.__minutes,
                                                self.__seconds)
    def tick(self):
        """
        This method lets the clock "tick", this means that the 
        internal time will be advanced by one second.
        Examples:
        >>> x = Clock(12,59,59)
        >>> print(x)
        12:59:59
        >>> x.tick()
        >>> print(x)
        13:00:00
        >>> x.tick()
        >>> print(x)
        13:00:01
        """
        if self.__seconds == 59:
            self.__seconds = 0
            if self.__minutes == 59:
                self.__minutes = 0
                if self._hours == 23:
                    self._hours = 0
                else:
                    self._hours += 1
            else:
                self.__minutes += 1
        else:
            self.__seconds += 1
if __name__ == "__main__":
    x = Clock(23,59,59)
    print(x)
    x.tick()
    print(x)
    y = str(x)
    print(type(y))
0

Về cơ bản, hai khả năng có thể tưởng tượng được. Có thể sử dụng "m of C" hoặc "m of A"

Chúng tôi gọi tập lệnh này bằng Python2. 7 (python) và với Python3 (python3) để xem điều gì đang xảy ra

""" 
The class Clock is used to simulate a clock.
"""
class Clock(object):
    def __init__(self, hours, minutes, seconds):
        """
        The paramaters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        self.set_Clock(hours, minutes, seconds)
    def set_Clock(self, hours, minutes, seconds):
        """
        The parameters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        if type(hours) == int and 0 <= hours and hours < 24:
            self._hours = hours
        else:
            raise TypeError("Hours have to be integers between 0 and 23!")
        if type(minutes) == int and 0 <= minutes and minutes < 60:
            self.__minutes = minutes 
        else:
            raise TypeError("Minutes have to be integers between 0 and 59!")
        if type(seconds) == int and 0 <= seconds and seconds < 60:
            self.__seconds = seconds
        else:
            raise TypeError("Seconds have to be integers between 0 and 59!")
    def __str__(self):
        return "{0:02d}:{1:02d}:{2:02d}".format(self._hours,
                                                self.__minutes,
                                                self.__seconds)
    def tick(self):
        """
        This method lets the clock "tick", this means that the 
        internal time will be advanced by one second.
        Examples:
        >>> x = Clock(12,59,59)
        >>> print(x)
        12:59:59
        >>> x.tick()
        >>> print(x)
        13:00:00
        >>> x.tick()
        >>> print(x)
        13:00:01
        """
        if self.__seconds == 59:
            self.__seconds = 0
            if self.__minutes == 59:
                self.__minutes = 0
                if self._hours == 23:
                    self._hours = 0
                else:
                    self._hours += 1
            else:
                self.__minutes += 1
        else:
            self.__seconds += 1
if __name__ == "__main__":
    x = Clock(23,59,59)
    print(x)
    x.tick()
    print(x)
    y = str(x)
    print(type(y))
1

Chỉ dành cho những ai quan tâm đến Python version2. Để có cùng hành vi kế thừa trong Python2 như trong Python3, mọi lớp phải kế thừa từ "đối tượng" của lớp. Lớp A của chúng tôi không kế thừa từ đối tượng, vì vậy chúng tôi nhận được cái gọi là lớp kiểu cũ, nếu chúng tôi gọi tập lệnh bằng python2. Đa kế thừa với các lớp kiểu cũ được điều chỉnh bởi hai quy tắc. chiều sâu trước rồi từ trái sang phải. Nếu bạn thay đổi dòng tiêu đề của A thành "class A(object). ", chúng ta sẽ có hành vi giống nhau trong cả hai phiên bản Python

Đào tạo Python trực tiếp

Đa kế thừa có được phép trong Python không?

Thưởng thức trang này?

Nhìn thấy. Tổng quan về các khóa học Python trực tiếp

Các khóa học trực tuyến sắp tới

Khóa học nâng cao chuyên sâu

Lập trình hướng đối tượng với Python

đăng ký tại đây

siêu và MRO

Chúng ta đã thấy trong quá trình triển khai vấn đề kim cương trước đây, cách Python "giải quyết" vấn đề, tôi. e. theo thứ tự các lớp cơ sở được duyệt qua. Thứ tự được xác định bởi cái gọi là "Thứ tự giải quyết phương pháp" hay gọi tắt là MRO*

Chúng ta sẽ mở rộng ví dụ trước để mỗi lớp định nghĩa phương thức riêng m

""" 
The class Clock is used to simulate a clock.
"""
class Clock(object):
    def __init__(self, hours, minutes, seconds):
        """
        The paramaters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        self.set_Clock(hours, minutes, seconds)
    def set_Clock(self, hours, minutes, seconds):
        """
        The parameters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        if type(hours) == int and 0 <= hours and hours < 24:
            self._hours = hours
        else:
            raise TypeError("Hours have to be integers between 0 and 23!")
        if type(minutes) == int and 0 <= minutes and minutes < 60:
            self.__minutes = minutes 
        else:
            raise TypeError("Minutes have to be integers between 0 and 59!")
        if type(seconds) == int and 0 <= seconds and seconds < 60:
            self.__seconds = seconds
        else:
            raise TypeError("Seconds have to be integers between 0 and 59!")
    def __str__(self):
        return "{0:02d}:{1:02d}:{2:02d}".format(self._hours,
                                                self.__minutes,
                                                self.__seconds)
    def tick(self):
        """
        This method lets the clock "tick", this means that the 
        internal time will be advanced by one second.
        Examples:
        >>> x = Clock(12,59,59)
        >>> print(x)
        12:59:59
        >>> x.tick()
        >>> print(x)
        13:00:00
        >>> x.tick()
        >>> print(x)
        13:00:01
        """
        if self.__seconds == 59:
            self.__seconds = 0
            if self.__minutes == 59:
                self.__minutes = 0
                if self._hours == 23:
                    self._hours = 0
                else:
                    self._hours += 1
            else:
                self.__minutes += 1
        else:
            self.__seconds += 1
if __name__ == "__main__":
    x = Clock(23,59,59)
    print(x)
    x.tick()
    print(x)
    y = str(x)
    print(type(y))
2

Hãy áp dụng phương pháp m trên một thể hiện của D. Chúng ta có thể thấy rằng chỉ mã của phương thức m của D sẽ được thực thi. Chúng ta cũng có thể gọi một cách rõ ràng các phương thức m của các lớp khác thông qua tên lớp, như chúng ta đã chứng minh trong phiên Python tương tác sau đây

""" 
The class Clock is used to simulate a clock.
"""
class Clock(object):
    def __init__(self, hours, minutes, seconds):
        """
        The paramaters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        self.set_Clock(hours, minutes, seconds)
    def set_Clock(self, hours, minutes, seconds):
        """
        The parameters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        if type(hours) == int and 0 <= hours and hours < 24:
            self._hours = hours
        else:
            raise TypeError("Hours have to be integers between 0 and 23!")
        if type(minutes) == int and 0 <= minutes and minutes < 60:
            self.__minutes = minutes 
        else:
            raise TypeError("Minutes have to be integers between 0 and 59!")
        if type(seconds) == int and 0 <= seconds and seconds < 60:
            self.__seconds = seconds
        else:
            raise TypeError("Seconds have to be integers between 0 and 59!")
    def __str__(self):
        return "{0:02d}:{1:02d}:{2:02d}".format(self._hours,
                                                self.__minutes,
                                                self.__seconds)
    def tick(self):
        """
        This method lets the clock "tick", this means that the 
        internal time will be advanced by one second.
        Examples:
        >>> x = Clock(12,59,59)
        >>> print(x)
        12:59:59
        >>> x.tick()
        >>> print(x)
        13:00:00
        >>> x.tick()
        >>> print(x)
        13:00:01
        """
        if self.__seconds == 59:
            self.__seconds = 0
            if self.__minutes == 59:
                self.__minutes = 0
                if self._hours == 23:
                    self._hours = 0
                else:
                    self._hours += 1
            else:
                self.__minutes += 1
        else:
            self.__seconds += 1
if __name__ == "__main__":
    x = Clock(23,59,59)
    print(x)
    x.tick()
    print(x)
    y = str(x)
    print(type(y))
3

ĐẦU RA

""" 
The class Clock is used to simulate a clock.
"""
class Clock(object):
    def __init__(self, hours, minutes, seconds):
        """
        The paramaters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        self.set_Clock(hours, minutes, seconds)
    def set_Clock(self, hours, minutes, seconds):
        """
        The parameters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        if type(hours) == int and 0 <= hours and hours < 24:
            self._hours = hours
        else:
            raise TypeError("Hours have to be integers between 0 and 23!")
        if type(minutes) == int and 0 <= minutes and minutes < 60:
            self.__minutes = minutes 
        else:
            raise TypeError("Minutes have to be integers between 0 and 59!")
        if type(seconds) == int and 0 <= seconds and seconds < 60:
            self.__seconds = seconds
        else:
            raise TypeError("Seconds have to be integers between 0 and 59!")
    def __str__(self):
        return "{0:02d}:{1:02d}:{2:02d}".format(self._hours,
                                                self.__minutes,
                                                self.__seconds)
    def tick(self):
        """
        This method lets the clock "tick", this means that the 
        internal time will be advanced by one second.
        Examples:
        >>> x = Clock(12,59,59)
        >>> print(x)
        12:59:59
        >>> x.tick()
        >>> print(x)
        13:00:00
        >>> x.tick()
        >>> print(x)
        13:00:01
        """
        if self.__seconds == 59:
            self.__seconds = 0
            if self.__minutes == 59:
                self.__minutes = 0
                if self._hours == 23:
                    self._hours = 0
                else:
                    self._hours += 1
            else:
                self.__minutes += 1
        else:
            self.__seconds += 1
if __name__ == "__main__":
    x = Clock(23,59,59)
    print(x)
    x.tick()
    print(x)
    y = str(x)
    print(type(y))
4

""" 
The class Clock is used to simulate a clock.
"""
class Clock(object):
    def __init__(self, hours, minutes, seconds):
        """
        The paramaters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        self.set_Clock(hours, minutes, seconds)
    def set_Clock(self, hours, minutes, seconds):
        """
        The parameters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        if type(hours) == int and 0 <= hours and hours < 24:
            self._hours = hours
        else:
            raise TypeError("Hours have to be integers between 0 and 23!")
        if type(minutes) == int and 0 <= minutes and minutes < 60:
            self.__minutes = minutes 
        else:
            raise TypeError("Minutes have to be integers between 0 and 59!")
        if type(seconds) == int and 0 <= seconds and seconds < 60:
            self.__seconds = seconds
        else:
            raise TypeError("Seconds have to be integers between 0 and 59!")
    def __str__(self):
        return "{0:02d}:{1:02d}:{2:02d}".format(self._hours,
                                                self.__minutes,
                                                self.__seconds)
    def tick(self):
        """
        This method lets the clock "tick", this means that the 
        internal time will be advanced by one second.
        Examples:
        >>> x = Clock(12,59,59)
        >>> print(x)
        12:59:59
        >>> x.tick()
        >>> print(x)
        13:00:00
        >>> x.tick()
        >>> print(x)
        13:00:01
        """
        if self.__seconds == 59:
            self.__seconds = 0
            if self.__minutes == 59:
                self.__minutes = 0
                if self._hours == 23:
                    self._hours = 0
                else:
                    self._hours += 1
            else:
                self.__minutes += 1
        else:
            self.__seconds += 1
if __name__ == "__main__":
    x = Clock(23,59,59)
    print(x)
    x.tick()
    print(x)
    y = str(x)
    print(type(y))
5

ĐẦU RA

""" 
The class Clock is used to simulate a clock.
"""
class Clock(object):
    def __init__(self, hours, minutes, seconds):
        """
        The paramaters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        self.set_Clock(hours, minutes, seconds)
    def set_Clock(self, hours, minutes, seconds):
        """
        The parameters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        if type(hours) == int and 0 <= hours and hours < 24:
            self._hours = hours
        else:
            raise TypeError("Hours have to be integers between 0 and 23!")
        if type(minutes) == int and 0 <= minutes and minutes < 60:
            self.__minutes = minutes 
        else:
            raise TypeError("Minutes have to be integers between 0 and 59!")
        if type(seconds) == int and 0 <= seconds and seconds < 60:
            self.__seconds = seconds
        else:
            raise TypeError("Seconds have to be integers between 0 and 59!")
    def __str__(self):
        return "{0:02d}:{1:02d}:{2:02d}".format(self._hours,
                                                self.__minutes,
                                                self.__seconds)
    def tick(self):
        """
        This method lets the clock "tick", this means that the 
        internal time will be advanced by one second.
        Examples:
        >>> x = Clock(12,59,59)
        >>> print(x)
        12:59:59
        >>> x.tick()
        >>> print(x)
        13:00:00
        >>> x.tick()
        >>> print(x)
        13:00:01
        """
        if self.__seconds == 59:
            self.__seconds = 0
            if self.__minutes == 59:
                self.__minutes = 0
                if self._hours == 23:
                    self._hours = 0
                else:
                    self._hours += 1
            else:
                self.__minutes += 1
        else:
            self.__seconds += 1
if __name__ == "__main__":
    x = Clock(23,59,59)
    print(x)
    x.tick()
    print(x)
    y = str(x)
    print(type(y))
0

""" 
The class Clock is used to simulate a clock.
"""
class Clock(object):
    def __init__(self, hours, minutes, seconds):
        """
        The paramaters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        self.set_Clock(hours, minutes, seconds)
    def set_Clock(self, hours, minutes, seconds):
        """
        The parameters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        if type(hours) == int and 0 <= hours and hours < 24:
            self._hours = hours
        else:
            raise TypeError("Hours have to be integers between 0 and 23!")
        if type(minutes) == int and 0 <= minutes and minutes < 60:
            self.__minutes = minutes 
        else:
            raise TypeError("Minutes have to be integers between 0 and 59!")
        if type(seconds) == int and 0 <= seconds and seconds < 60:
            self.__seconds = seconds
        else:
            raise TypeError("Seconds have to be integers between 0 and 59!")
    def __str__(self):
        return "{0:02d}:{1:02d}:{2:02d}".format(self._hours,
                                                self.__minutes,
                                                self.__seconds)
    def tick(self):
        """
        This method lets the clock "tick", this means that the 
        internal time will be advanced by one second.
        Examples:
        >>> x = Clock(12,59,59)
        >>> print(x)
        12:59:59
        >>> x.tick()
        >>> print(x)
        13:00:00
        >>> x.tick()
        >>> print(x)
        13:00:01
        """
        if self.__seconds == 59:
            self.__seconds = 0
            if self.__minutes == 59:
                self.__minutes = 0
                if self._hours == 23:
                    self._hours = 0
                else:
                    self._hours += 1
            else:
                self.__minutes += 1
        else:
            self.__seconds += 1
if __name__ == "__main__":
    x = Clock(23,59,59)
    print(x)
    x.tick()
    print(x)
    y = str(x)
    print(type(y))
7

ĐẦU RA

""" 
The class Clock is used to simulate a clock.
"""
class Clock(object):
    def __init__(self, hours, minutes, seconds):
        """
        The paramaters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        self.set_Clock(hours, minutes, seconds)
    def set_Clock(self, hours, minutes, seconds):
        """
        The parameters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        if type(hours) == int and 0 <= hours and hours < 24:
            self._hours = hours
        else:
            raise TypeError("Hours have to be integers between 0 and 23!")
        if type(minutes) == int and 0 <= minutes and minutes < 60:
            self.__minutes = minutes 
        else:
            raise TypeError("Minutes have to be integers between 0 and 59!")
        if type(seconds) == int and 0 <= seconds and seconds < 60:
            self.__seconds = seconds
        else:
            raise TypeError("Seconds have to be integers between 0 and 59!")
    def __str__(self):
        return "{0:02d}:{1:02d}:{2:02d}".format(self._hours,
                                                self.__minutes,
                                                self.__seconds)
    def tick(self):
        """
        This method lets the clock "tick", this means that the 
        internal time will be advanced by one second.
        Examples:
        >>> x = Clock(12,59,59)
        >>> print(x)
        12:59:59
        >>> x.tick()
        >>> print(x)
        13:00:00
        >>> x.tick()
        >>> print(x)
        13:00:01
        """
        if self.__seconds == 59:
            self.__seconds = 0
            if self.__minutes == 59:
                self.__minutes = 0
                if self._hours == 23:
                    self._hours = 0
                else:
                    self._hours += 1
            else:
                self.__minutes += 1
        else:
            self.__seconds += 1
if __name__ == "__main__":
    x = Clock(23,59,59)
    print(x)
    x.tick()
    print(x)
    y = str(x)
    print(type(y))
8

Bây giờ, hãy giả sử rằng phương thức m của D cũng sẽ thực thi mã của m của B, C và A, khi nó được gọi. Chúng ta có thể thực hiện nó như thế này

""" 
The class Clock is used to simulate a clock.
"""
class Clock(object):
    def __init__(self, hours, minutes, seconds):
        """
        The paramaters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        self.set_Clock(hours, minutes, seconds)
    def set_Clock(self, hours, minutes, seconds):
        """
        The parameters hours, minutes and seconds have to be 
        integers and must satisfy the following equations:
        0 <= h < 24
        0 <= m < 60
        0 <= s < 60
        """
        if type(hours) == int and 0 <= hours and hours < 24:
            self._hours = hours
        else:
            raise TypeError("Hours have to be integers between 0 and 23!")
        if type(minutes) == int and 0 <= minutes and minutes < 60:
            self.__minutes = minutes 
        else:
            raise TypeError("Minutes have to be integers between 0 and 59!")
        if type(seconds) == int and 0 <= seconds and seconds < 60:
            self.__seconds = seconds
        else:
            raise TypeError("Seconds have to be integers between 0 and 59!")
    def __str__(self):
        return "{0:02d}:{1:02d}:{2:02d}".format(self._hours,
                                                self.__minutes,
                                                self.__seconds)
    def tick(self):
        """
        This method lets the clock "tick", this means that the 
        internal time will be advanced by one second.
        Examples:
        >>> x = Clock(12,59,59)
        >>> print(x)
        12:59:59
        >>> x.tick()
        >>> print(x)
        13:00:00
        >>> x.tick()
        >>> print(x)
        13:00:01
        """
        if self.__seconds == 59:
            self.__seconds = 0
            if self.__minutes == 59:
                self.__minutes = 0
                if self._hours == 23:
                    self._hours = 0
                else:
                    self._hours += 1
            else:
                self.__minutes += 1
        else:
            self.__seconds += 1
if __name__ == "__main__":
    x = Clock(23,59,59)
    print(x)
    x.tick()
    print(x)
    y = str(x)
    print(type(y))
9

Đầu ra là những gì chúng tôi đã được tìm kiếm

23:59:59
00:00:00

0

ĐẦU RA

23:59:59
00:00:00

1

Nhưng hóa ra một lần nữa mọi thứ lại phức tạp hơn chúng tưởng. Làm thế nào chúng ta có thể đối phó với tình huống, nếu cả m của B và m của C cũng sẽ phải gọi m của A. Trong trường hợp này, chúng ta phải loại bỏ cuộc gọi A. m(tự) từ m trong D. Mã có thể trông như thế này, nhưng vẫn có một lỗi ẩn trong đó

23:59:59
00:00:00

2

Lỗi là phương thức m của A sẽ được gọi hai lần

23:59:59
00:00:00

3

ĐẦU RA

23:59:59
00:00:00

4

Một cách để giải quyết vấn đề này - phải thừa nhận rằng không phải là Pythonic - bao gồm việc chia các phương thức m của B và C thành hai phương thức. Phương thức đầu tiên, được gọi là

x = Clock(24, 45, 17)
8 bao gồm mã cụ thể cho B và C và phương thức khác vẫn được gọi là m, nhưng hiện bao gồm cuộc gọi
x = Clock(24, 45, 17)
9 và cuộc gọi
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1x = Clock(24, 45, 17)
 in __init__(self, hours, minutes, seconds)
     14         """
     15 
---> 16self.set_Clock(hours, minutes, seconds)
     17 
     18     def set_Clock(self, hours, minutes, seconds):
 in set_Clock(self, hours, minutes, seconds)
     28             self._hours = hours
     29         else:
---> 30raise TypeError("Hours have to be integers between 0 and 23!")
     31         if type(minutes) == int and 0 <= minutes and minutes < 60:
     32             self.__minutes = minutes
TypeError: Hours have to be integers between 0 and 23!
0. Mã của phương thức m của D hiện bao gồm mã cụ thể của D 'print("m của D được gọi là")' và các cuộc gọi
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1x = Clock(24, 45, 17)
 in __init__(self, hours, minutes, seconds)
     14         """
     15 
---> 16self.set_Clock(hours, minutes, seconds)
     17 
     18     def set_Clock(self, hours, minutes, seconds):
 in set_Clock(self, hours, minutes, seconds)
     28             self._hours = hours
     29         else:
---> 30raise TypeError("Hours have to be integers between 0 and 23!")
     31         if type(minutes) == int and 0 <= minutes and minutes < 60:
     32             self.__minutes = minutes
TypeError: Hours have to be integers between 0 and 23!
1,
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1x = Clock(24, 45, 17)
 in __init__(self, hours, minutes, seconds)
     14         """
     15 
---> 16self.set_Clock(hours, minutes, seconds)
     17 
     18     def set_Clock(self, hours, minutes, seconds):
 in set_Clock(self, hours, minutes, seconds)
     28             self._hours = hours
     29         else:
---> 30raise TypeError("Hours have to be integers between 0 and 23!")
     31         if type(minutes) == int and 0 <= minutes and minutes < 60:
     32             self.__minutes = minutes
TypeError: Hours have to be integers between 0 and 23!
2 và
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1x = Clock(24, 45, 17)
 in __init__(self, hours, minutes, seconds)
     14         """
     15 
---> 16self.set_Clock(hours, minutes, seconds)
     17 
     18     def set_Clock(self, hours, minutes, seconds):
 in set_Clock(self, hours, minutes, seconds)
     28             self._hours = hours
     29         else:
---> 30raise TypeError("Hours have to be integers between 0 and 23!")
     31         if type(minutes) == int and 0 <= minutes and minutes < 60:
     32             self.__minutes = minutes
TypeError: Hours have to be integers between 0 and 23!
0

23:59:59
00:00:00

5

Vấn đề của chúng tôi đã được giải quyết, nhưng - như chúng tôi đã đề cập - không phải theo cách Pythonic

23:59:59
00:00:00

6

ĐẦU RA

23:59:59
00:00:00

1

Cách tối ưu để giải quyết vấn đề, đó là cách "siêu" Pythonic, sẽ gọi hàm siêu

23:59:59
00:00:00

8

Nó cũng giải quyết vấn đề của chúng tôi, nhưng cũng trong một thiết kế đẹp

23:59:59
00:00:00

9

ĐẦU RA

23:59:59
00:00:00

1

Hàm siêu thường được sử dụng khi các phiên bản được khởi tạo bằng phương thức

x = Clock(24, 45, 17)
6

x = Clock(7.7, 45, 17)
1

Chúng tôi chứng minh cách làm việc trong phiên tương tác sau

x = Clock(7.7, 45, 17)
2

ĐẦU RA

x = Clock(7.7, 45, 17)
3

x = Clock(7.7, 45, 17)
4

ĐẦU RA

x = Clock(7.7, 45, 17)
5

x = Clock(7.7, 45, 17)
6

ĐẦU RA

x = Clock(7.7, 45, 17)
7

x = Clock(7.7, 45, 17)
8

ĐẦU RA

x = Clock(7.7, 45, 17)
9

Câu hỏi đặt ra về cách các siêu chức năng đưa ra quyết định. Làm thế nào để nó quyết định lớp nào phải được sử dụng? . Nó dựa trên thuật toán "tuyến tính hóa siêu lớp C3". Điều này được gọi là tuyến tính hóa, bởi vì cấu trúc cây được chia thành một trật tự tuyến tính. Phương pháp mro có thể được sử dụng để tạo danh sách này

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1x = Clock(7.7,45,17)
 in __init__(self, hours, minutes, seconds)
     14         """
     15 
---> 16self.set_Clock(hours, minutes, seconds)
     17 
     18     def set_Clock(self, hours, minutes, seconds):
 in set_Clock(self, hours, minutes, seconds)
     28             self._hours = hours
     29         else:
---> 30raise TypeError("Hours have to be integers between 0 and 23!")
     31         if type(minutes) == int and 0 <= minutes and minutes < 60:
     32             self.__minutes = minutes
TypeError: Hours have to be integers between 0 and 23!
0

ĐẦU RA

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1x = Clock(7.7,45,17)
 in __init__(self, hours, minutes, seconds)
     14         """
     15 
---> 16self.set_Clock(hours, minutes, seconds)
     17 
     18     def set_Clock(self, hours, minutes, seconds):
 in set_Clock(self, hours, minutes, seconds)
     28             self._hours = hours
     29         else:
---> 30raise TypeError("Hours have to be integers between 0 and 23!")
     31         if type(minutes) == int and 0 <= minutes and minutes < 60:
     32             self.__minutes = minutes
TypeError: Hours have to be integers between 0 and 23!
1

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1x = Clock(7.7,45,17)
 in __init__(self, hours, minutes, seconds)
     14         """
     15 
---> 16self.set_Clock(hours, minutes, seconds)
     17 
     18     def set_Clock(self, hours, minutes, seconds):
 in set_Clock(self, hours, minutes, seconds)
     28             self._hours = hours
     29         else:
---> 30raise TypeError("Hours have to be integers between 0 and 23!")
     31         if type(minutes) == int and 0 <= minutes and minutes < 60:
     32             self.__minutes = minutes
TypeError: Hours have to be integers between 0 and 23!
2

ĐẦU RA

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1x = Clock(7.7,45,17)
 in __init__(self, hours, minutes, seconds)
     14         """
     15 
---> 16self.set_Clock(hours, minutes, seconds)
     17 
     18     def set_Clock(self, hours, minutes, seconds):
 in set_Clock(self, hours, minutes, seconds)
     28             self._hours = hours
     29         else:
---> 30raise TypeError("Hours have to be integers between 0 and 23!")
     31         if type(minutes) == int and 0 <= minutes and minutes < 60:
     32             self.__minutes = minutes
TypeError: Hours have to be integers between 0 and 23!
3

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1x = Clock(7.7,45,17)
 in __init__(self, hours, minutes, seconds)
     14         """
     15 
---> 16self.set_Clock(hours, minutes, seconds)
     17 
     18     def set_Clock(self, hours, minutes, seconds):
 in set_Clock(self, hours, minutes, seconds)
     28             self._hours = hours
     29         else:
---> 30raise TypeError("Hours have to be integers between 0 and 23!")
     31         if type(minutes) == int and 0 <= minutes and minutes < 60:
     32             self.__minutes = minutes
TypeError: Hours have to be integers between 0 and 23!
4

ĐẦU RA

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1x = Clock(7.7,45,17)
 in __init__(self, hours, minutes, seconds)
     14         """
     15 
---> 16self.set_Clock(hours, minutes, seconds)
     17 
     18     def set_Clock(self, hours, minutes, seconds):
 in set_Clock(self, hours, minutes, seconds)
     28             self._hours = hours
     29         else:
---> 30raise TypeError("Hours have to be integers between 0 and 23!")
     31         if type(minutes) == int and 0 <= minutes and minutes < 60:
     32             self.__minutes = minutes
TypeError: Hours have to be integers between 0 and 23!
5

đa hình

Đa kế thừa có được phép trong Python không?

Đa hình được hiểu từ hai từ Hy Lạp. "Poly" là viết tắt của "much" hoặc "many" và "morph" có nghĩa là hình dạng hoặc hình thức. Tính đa hình là trạng thái hoặc điều kiện của tính đa hình, hoặc nếu chúng ta sử dụng bản dịch của các thành phần "khả năng có nhiều hình dạng hoặc dạng. Đa hình là một thuật ngữ được sử dụng trong nhiều lĩnh vực khoa học. Trong tinh thể học, nó xác định trạng thái, nếu một thứ gì đó kết tinh thành hai hoặc nhiều dạng giống hệt nhau về mặt hóa học nhưng khác biệt về mặt tinh thể. Các nhà sinh học biết tính đa hình là sự tồn tại của một sinh vật ở một số dạng hoặc màu sắc khác nhau. Người La Mã thậm chí còn có một vị thần tên là Morpheus, người có thể biến thành bất kỳ hình dạng con người nào. Morheus xuất hiện trong các biến thái của Ovid và là con trai của Somnus, thần ngủ. Bạn có thể chiêm ngưỡng Morpheus và Iris ở hình bên phải

Vì vậy, trước khi đi ngủ, chúng ta quay lại với Python và ý nghĩa của tính đa hình trong ngữ cảnh của các ngôn ngữ lập trình. Tính đa hình trong Khoa học máy tính là khả năng hiển thị cùng một giao diện cho các dạng cơ bản khác nhau. Ví dụ, chúng ta có thể có các hàm hoặc phương thức đa hình trong một số ngôn ngữ lập trình. Các hàm hoặc phương thức đa hình có thể được áp dụng cho các đối số thuộc các loại khác nhau và chúng có thể hoạt động khác nhau tùy thuộc vào loại đối số mà chúng được áp dụng. Chúng ta cũng có thể định nghĩa cùng một tên hàm với một số lượng tham số khác nhau

Chúng ta hãy xem hàm Python sau

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1x = Clock(7.7,45,17)
 in __init__(self, hours, minutes, seconds)
     14         """
     15 
---> 16self.set_Clock(hours, minutes, seconds)
     17 
     18     def set_Clock(self, hours, minutes, seconds):
 in set_Clock(self, hours, minutes, seconds)
     28             self._hours = hours
     29         else:
---> 30raise TypeError("Hours have to be integers between 0 and 23!")
     31         if type(minutes) == int and 0 <= minutes and minutes < 60:
     32             self.__minutes = minutes
TypeError: Hours have to be integers between 0 and 23!
6

ĐẦU RA

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1x = Clock(7.7,45,17)
 in __init__(self, hours, minutes, seconds)
     14         """
     15 
---> 16self.set_Clock(hours, minutes, seconds)
     17 
     18     def set_Clock(self, hours, minutes, seconds):
 in set_Clock(self, hours, minutes, seconds)
     28             self._hours = hours
     29         else:
---> 30raise TypeError("Hours have to be integers between 0 and 23!")
     31         if type(minutes) == int and 0 <= minutes and minutes < 60:
     32             self.__minutes = minutes
TypeError: Hours have to be integers between 0 and 23!
7

Chúng ta có thể gọi hàm này với nhiều loại khác nhau, như minh họa trong ví dụ. Trong các ngôn ngữ lập trình có kiểu như Java hoặc C++, chúng ta sẽ phải quá tải f để thực hiện các kết hợp kiểu khác nhau

Ví dụ của chúng tôi có thể được thực hiện như thế này trong C++

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1x = Clock(7.7,45,17)
 in __init__(self, hours, minutes, seconds)
     14         """
     15 
---> 16self.set_Clock(hours, minutes, seconds)
     17 
     18     def set_Clock(self, hours, minutes, seconds):
 in set_Clock(self, hours, minutes, seconds)
     28             self._hours = hours
     29         else:
---> 30raise TypeError("Hours have to be integers between 0 and 23!")
     31         if type(minutes) == int and 0 <= minutes and minutes < 60:
     32             self.__minutes = minutes
TypeError: Hours have to be integers between 0 and 23!
8

Python hoàn toàn đa hình. Chúng ta có thể áp dụng hàm f đã xác định trước đó cho cả danh sách, chuỗi hoặc các loại khác, có thể in được

Đa kế thừa có khả thi trong Python không?

Có, Python hỗ trợ đa kế thừa . Giống như C++, một lớp có thể được bắt nguồn từ nhiều lớp cơ sở trong Python. Điều này được gọi là Đa kế thừa.

Tại sao đa thừa kế được cho phép trong Python?

Kế thừa là cơ chế để đạt được khả năng sử dụng lại mã vì một lớp (lớp con) có thể lấy thuộc tính của lớp khác (lớp cha) . Nó cũng cung cấp tính bắc cầu tức là. nếu lớp C kế thừa từ P thì tất cả các lớp con của C cũng kế thừa từ P.

Kế thừa nào không được phép trong Python?

Câu trả lời. Không giống như các ngôn ngữ lập trình hướng đối tượng khác như Java, Python hỗ trợ tất cả các kiểu kế thừa, thậm chí là đa kế thừa. Và mặc dù C++ cũng hỗ trợ kiểu kế thừa này, nhưng nó không có cách tiếp cận phức tạp và được thiết kế tốt như Python

Các lớp Python có thể kế thừa nhiều lớp không?

Trong Python một lớp có thể kế thừa từ nhiều lớp . Nếu một lớp kế thừa, nó có các phương thức và biến từ các lớp cha. Thực chất gọi là đa kế thừa vì một lớp có thể kế thừa từ nhiều lớp. Đây là một khái niệm từ lập trình hướng đối tượng.