Siêu python __init__ không được gọi

” là một phương thức dành riêng trong các lớp python. Nó được biết đến như một hàm tạo trong thuật ngữ Hướng đối tượng. Phương thức này khi được gọi sẽ cho phép lớp khởi tạo các thuộc tính của lớp

Siêu Python[]

Hàm super[] cho phép chúng ta tránh sử dụng tên lớp cơ sở một cách rõ ràng. Trong Python, hàm super[] được gọi động không giống như các ngôn ngữ khác, vì nó là một ngôn ngữ động. Hàm super[] trả về một đối tượng đại diện cho lớp cha.

 

Chức năng này có thể được sử dụng để kích hoạt cả Kế thừa đơn và Nhiều kế thừa

 

super[] trong Kế thừa duy nhất với __init__[]

Thí dụ

#base class
class State[]:
   def __init__[self]:
      print["In the state class"]
      self.state1= "Main State"

#derived class
class HappyState[]:
   def __init__[self]:
      print["In the happystate class"]
      self.state2= "Happy State"
      super[].__init__[]

a=HappyState[]
print[a.state1]
print[a.state2]

đầu ra

 

super[] với Đa kế thừa với __init__[]

Trong một lớp con, một lớp cha có thể được tham chiếu bằng cách sử dụng hàm super[]. Nó trả về một đối tượng tạm thời của lớp cha và điều này cho phép lớp con của nó truy cập vào tất cả các phương thức của nó. Trong khi sử dụng từ khóa super[], chúng ta không cần chỉ định tên lớp cha để truy cập các phương thức của nó.

 

Thí dụ

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]

đầu ra

 

Trong chương trình trên, chúng ta có thể thấy rằng chỉ có lớp cơ sở đầu tiên __init__[] được gọi khi chúng ta sử dụng super[]. __trong đó__[].

 

Để gọi lớp cơ sở thứ hai, chúng ta sẽ phải thêm một tổ hợp mới của phương thức super[] và __init__[]. Nhìn vào ví dụ dưới đây

 

Thí dụ

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

class HappyState[State,Event]:
   def __init__[self]:
      print["In the Happy State class"]
      super[].__init__[]                # calls base class 1
      super[State,self].__init__[]      # calls base class 2
a=HappyState[]

đầu ra

 

siêu[]. __init__[] gọi phương thức init của lớp cơ sở đầu tiên.

siêu[Bang,bản thân]. __init__[]   gọi phương thức init của lớp cơ sở thứ hai.

 

Vui lòng liên hệ với CWAssignments, một dịch vụ chuyên nghiệp, để nhận trợ giúp bài tập về Python từ các chuyên gia viết mã

Mặc dù Python không hoàn toàn là một ngôn ngữ hướng đối tượng, nhưng nó đủ linh hoạt và đủ mạnh để cho phép bạn xây dựng các ứng dụng của mình bằng mô hình hướng đối tượng. Một trong những cách mà Python đạt được điều này là bằng cách hỗ trợ tính kế thừa, điều mà nó thực hiện với

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0

Trong hướng dẫn này, bạn sẽ tìm hiểu về những điều sau đây

  • Khái niệm kế thừa trong Python
  • Đa kế thừa trong Python
  • Cách thức hoạt động của hàm
    class Rectangle:
        def __init__[self, length, width]:
            self.length = length
            self.width = width
    
        def area[self]:
            return self.length * self.width
    
        def perimeter[self]:
            return 2 * self.length + 2 * self.width
    
    # Here we declare that the Square class inherits from the Rectangle class
    class Square[Rectangle]:
        def __init__[self, length]:
            super[].__init__[length, length]
    
    0
  • Cách hoạt động của hàm
    class Rectangle:
        def __init__[self, length, width]:
            self.length = length
            self.width = width
    
        def area[self]:
            return self.length * self.width
    
        def perimeter[self]:
            return 2 * self.length + 2 * self.width
    
    # Here we declare that the Square class inherits from the Rectangle class
    class Square[Rectangle]:
        def __init__[self, length]:
            super[].__init__[length, length]
    
    0 trong thừa kế đơn lẻ
  • Cách hoạt động của hàm
    class Rectangle:
        def __init__[self, length, width]:
            self.length = length
            self.width = width
    
        def area[self]:
            return self.length * self.width
    
        def perimeter[self]:
            return 2 * self.length + 2 * self.width
    
    # Here we declare that the Square class inherits from the Rectangle class
    class Square[Rectangle]:
        def __init__[self, length]:
            super[].__init__[length, length]
    
    0 trong đa kế thừa

Tiền thưởng miễn phí. 5 Suy nghĩ về Làm chủ Python, một khóa học miễn phí dành cho các nhà phát triển Python cho bạn thấy lộ trình và tư duy mà bạn sẽ cần để đưa các kỹ năng Python của mình lên một tầm cao mới

Tổng quan về Hàm
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 của Python

Nếu bạn có kinh nghiệm với các ngôn ngữ hướng đối tượng, bạn có thể đã quen thuộc với chức năng của

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0

Nếu không, đừng sợ. Mặc dù tài liệu chính thức khá kỹ thuật, nhưng ở cấp độ cao,

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 cho phép bạn truy cập vào các phương thức trong một lớp cha từ lớp con kế thừa từ nó

Riêng

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 trả về một đối tượng tạm thời của siêu lớp mà sau đó cho phép bạn gọi các phương thức của siêu lớp đó

Tại sao bạn muốn làm bất kỳ điều này?

Việc gọi các phương thức đã tạo trước đó bằng

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 giúp bạn không cần phải viết lại các phương thức đó trong lớp con của mình và cho phép bạn hoán đổi các lớp cha với các thay đổi mã tối thiểu

Loại bỏ các quảng cáo

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 trong tài sản thừa kế duy nhất

Nếu bạn chưa quen với các khái niệm lập trình hướng đối tượng, thì kế thừa có thể là một thuật ngữ xa lạ. Kế thừa là một khái niệm trong lập trình hướng đối tượng, trong đó một lớp dẫn xuất [hoặc kế thừa] các thuộc tính và hành vi từ một lớp khác mà không cần thực hiện lại chúng

Ít nhất đối với tôi, việc hiểu các khái niệm này khi xem mã sẽ dễ dàng hơn, vì vậy hãy viết các lớp mô tả một số hình dạng

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
3

Ở đây, có hai lớp tương tự.

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
40 và
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41

Bạn có thể sử dụng chúng như dưới đây

>>>

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
6

Trong ví dụ này, bạn có hai hình có liên quan với nhau. hình vuông là một loại hình chữ nhật đặc biệt. Tuy nhiên, mã không phản ánh mối quan hệ đó và do đó có mã về cơ bản được lặp lại

Bằng cách sử dụng tính kế thừa, bạn có thể giảm số lượng mã bạn viết đồng thời phản ánh mối quan hệ trong thế giới thực giữa hình chữ nhật và hình vuông

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]

Ở đây, bạn đã sử dụng

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 để gọi
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
43 của lớp
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
40, cho phép bạn sử dụng nó trong lớp
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41 mà không cần lặp lại mã. Dưới đây, chức năng cốt lõi vẫn còn sau khi thực hiện thay đổi

>>>

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
4

Trong ví dụ này,

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
40 là lớp cha và
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41 là lớp con

Bởi vì các phương thức

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41 và
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
40
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

class HappyState[State,Event]:
   def __init__[self]:
      print["In the Happy State class"]
      super[].__init__[]                # calls base class 1
      super[State,self].__init__[]      # calls base class 2
a=HappyState[]
70 rất giống nhau, bạn có thể chỉ cần gọi phương thức
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

class HappyState[State,Event]:
   def __init__[self]:
      print["In the Happy State class"]
      super[].__init__[]                # calls base class 1
      super[State,self].__init__[]      # calls base class 2
a=HappyState[]
70 của siêu lớp [
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

class HappyState[State,Event]:
   def __init__[self]:
      print["In the Happy State class"]
      super[].__init__[]                # calls base class 1
      super[State,self].__init__[]      # calls base class 2
a=HappyState[]
72] từ phương thức của
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41 bằng cách sử dụng
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0. Điều này đặt các thuộc tính
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

class HappyState[State,Event]:
   def __init__[self]:
      print["In the Happy State class"]
      super[].__init__[]                # calls base class 1
      super[State,self].__init__[]      # calls base class 2
a=HappyState[]
75 và
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

class HappyState[State,Event]:
   def __init__[self]:
      print["In the Happy State class"]
      super[].__init__[]                # calls base class 1
      super[State,self].__init__[]      # calls base class 2
a=HappyState[]
76 mặc dù bạn chỉ cần cung cấp một tham số
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

class HappyState[State,Event]:
   def __init__[self]:
      print["In the Happy State class"]
      super[].__init__[]                # calls base class 1
      super[State,self].__init__[]      # calls base class 2
a=HappyState[]
77 duy nhất cho hàm tạo
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41

Khi bạn chạy cái này, mặc dù lớp

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41 của bạn không triển khai nó một cách rõ ràng, lệnh gọi tới
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
70 sẽ sử dụng phương thức
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
70 trong lớp cha và in ra
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
72. Lớp
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41 kế thừa lớp
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
70 từ lớp
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
40

Ghi chú. Để tìm hiểu thêm về các khái niệm kế thừa và hướng đối tượng trong Python, hãy nhớ xem Kế thừa và Thành phần. Hướng dẫn OOP Python và Lập trình hướng đối tượng [OOP] trong Python 3

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 có thể làm gì cho bạn?

Vì vậy,

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 có thể làm gì cho bạn trong tài sản thừa kế duy nhất?

Giống như các ngôn ngữ hướng đối tượng khác, nó cho phép bạn gọi các phương thức của lớp cha trong lớp con của bạn. Trường hợp sử dụng chính của điều này là để mở rộng chức năng của phương thức kế thừa

Trong ví dụ dưới đây, bạn sẽ tạo một lớp

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
78 kế thừa từ lớp
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41 và mở rộng chức năng của lớp
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
70 [kế thừa từ lớp
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
40 đến lớp
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41] để tính diện tích bề mặt và thể tích của một thực thể
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
78

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

class HappyState[State,Event]:
   def __init__[self]:
      print["In the Happy State class"]
      super[].__init__[]                # calls base class 1
      super[State,self].__init__[]      # calls base class 2
a=HappyState[]
7

Bây giờ bạn đã xây dựng các lớp, hãy xem diện tích bề mặt và thể tích của hình lập phương có chiều dài cạnh là

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
24

>>>

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
7

thận trọng. Lưu ý rằng trong ví dụ của chúng tôi ở trên, một mình

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 sẽ không thực hiện cuộc gọi phương thức cho bạn. bạn phải gọi phương thức trên chính đối tượng proxy

Ở đây bạn đã triển khai hai phương thức cho lớp

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
78.
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
27 và
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
28. Cả hai phép tính này đều dựa trên việc tính diện tích của một mặt, do đó, thay vì thực hiện lại phép tính diện tích, bạn sử dụng
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 để mở rộng phép tính diện tích

Cũng lưu ý rằng định nghĩa lớp

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
78 không có
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

class HappyState[State,Event]:
   def __init__[self]:
      print["In the Happy State class"]
      super[].__init__[]                # calls base class 1
      super[State,self].__init__[]      # calls base class 2
a=HappyState[]
70. Bởi vì
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
78 kế thừa từ
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41 và
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

class HappyState[State,Event]:
   def __init__[self]:
      print["In the Happy State class"]
      super[].__init__[]                # calls base class 1
      super[State,self].__init__[]      # calls base class 2
a=HappyState[]
70 không thực sự làm bất cứ điều gì khác biệt cho
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
78 so với những gì đã làm cho
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41, bạn có thể bỏ qua việc xác định nó và
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

class HappyState[State,Event]:
   def __init__[self]:
      print["In the Happy State class"]
      super[].__init__[]                # calls base class 1
      super[State,self].__init__[]      # calls base class 2
a=HappyState[]
70 của siêu lớp [
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41] sẽ được gọi tự động

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 trả về một đối tượng ủy nhiệm cho một lớp cha, vì vậy bạn gọi phương thức bạn muốn trực tiếp trên nó.
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
90

Điều này không chỉ giúp chúng tôi không phải viết lại các phép tính diện tích mà còn cho phép chúng tôi thay đổi logic

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
70 bên trong ở một vị trí duy nhất. Điều này đặc biệt hữu ích khi bạn có một số lớp con kế thừa từ một lớp cha

Loại bỏ các quảng cáo

Một chuyến lặn sâu
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0

Trước khi đi vào đa thừa kế, chúng ta hãy tìm hiểu nhanh về cơ chế của

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0

Trong khi các ví dụ trên [và bên dưới] gọi

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 mà không có bất kỳ tham số nào, thì
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 cũng có thể nhận hai tham số. đầu tiên là lớp con và tham số thứ hai là một đối tượng là một thể hiện của lớp con đó

Đầu tiên, hãy xem hai ví dụ cho thấy việc thao tác với biến đầu tiên có thể làm gì, bằng cách sử dụng các lớp đã được hiển thị

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
2

Trong Python 3, lệnh gọi

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
96 tương đương với lệnh gọi
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 không tham số. Tham số đầu tiên đề cập đến phân lớp
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41, trong khi tham số thứ hai đề cập đến một đối tượng
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41, trong trường hợp này là
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
00. Bạn cũng có thể gọi
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 với các lớp khác

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
2

Trong ví dụ này, bạn đang đặt

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41 làm đối số phân lớp thành
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0, thay vì
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
78. Điều này khiến
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 bắt đầu tìm kiếm một phương thức so khớp [trong trường hợp này là
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
70] ở một cấp trên
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41 trong hệ thống phân cấp cá thể, trong trường hợp này là
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
40

Trong ví dụ cụ thể này, hành vi không thay đổi. Nhưng hãy tưởng tượng rằng

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41 cũng đã triển khai một hàm
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
70 mà bạn muốn đảm bảo rằng
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
78 không sử dụng. Gọi
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 theo cách này cho phép bạn làm điều đó

thận trọng. Mặc dù chúng tôi đang loay hoay với các tham số của

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 để khám phá cách thức hoạt động ẩn của nó, nhưng tôi thận trọng không nên làm điều này thường xuyên

Cuộc gọi không tham số tới

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 được khuyến nghị và đủ cho hầu hết các trường hợp sử dụng và việc cần thay đổi cấu trúc phân cấp tìm kiếm thường xuyên có thể là dấu hiệu của một vấn đề thiết kế lớn hơn

Còn tham số thứ hai thì sao? . Ví dụ,

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
605 phải trả về
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
606

Bằng cách bao gồm một đối tượng được khởi tạo,

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 trả về một phương thức ràng buộc. một phương thức được liên kết với đối tượng, cung cấp cho phương thức ngữ cảnh của đối tượng, chẳng hạn như bất kỳ thuộc tính thể hiện nào. Nếu không bao gồm tham số này, phương thức được trả về chỉ là một hàm, không được liên kết với ngữ cảnh của đối tượng

Để biết thêm thông tin về các phương thức liên kết, phương thức không liên kết và chức năng, hãy đọc tài liệu Python trên hệ thống mô tả của nó

Ghi chú. Về mặt kỹ thuật,

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 không trả về một phương thức. Nó trả về một đối tượng proxy. Đây là một đối tượng ủy nhiệm các cuộc gọi đến các phương thức lớp chính xác mà không cần tạo một đối tượng bổ sung để làm như vậy

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 trong Đa kế thừa

Bây giờ bạn đã xem qua phần tổng quan và một số ví dụ về

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 và thừa kế đơn lẻ, bạn sẽ được giới thiệu về tổng quan và một số ví dụ sẽ chứng minh cách thức hoạt động của đa thừa kế và cách
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 kích hoạt chức năng đó

Tổng quan về đa kế thừa

Có một trường hợp sử dụng khác trong đó ________ 30 thực sự tỏa sáng và trường hợp này không phổ biến như trường hợp thừa kế đơn lẻ. Ngoài tính năng kế thừa đơn lẻ, Python hỗ trợ tính năng đa kế thừa, trong đó một lớp con có thể kế thừa từ nhiều lớp cha mà không nhất thiết phải kế thừa lẫn nhau [còn được gọi là lớp anh chị em]

Tôi là một người rất trực quan và tôi thấy sơ đồ cực kỳ hữu ích để hiểu các khái niệm như thế này. Hình ảnh bên dưới cho thấy một kịch bản đa kế thừa rất đơn giản, trong đó một lớp kế thừa từ hai siêu lớp [anh chị em] không liên quan

Một ví dụ sơ đồ về đa kế thừa [Hình ảnh. Kyle Stratis]

Để minh họa rõ hơn về hoạt động của đa kế thừa, đây là một số mã để bạn dùng thử, chỉ ra cách bạn có thể xây dựng một hình chóp bên phải [hình chóp có đáy hình vuông] từ một

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
613 và một
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
9

Ghi chú. Thuật ngữ chiều cao nghiêng có thể không quen thuộc, đặc biệt nếu đã lâu rồi bạn chưa học lớp hình học hoặc làm việc trên bất kỳ kim tự tháp nào

Chiều cao xiên là chiều cao tính từ tâm đáy của một vật thể [như kim tự tháp] lên mặt của nó đến đỉnh của vật thể đó. Bạn có thể đọc thêm về độ cao nghiêng tại Wolfram MathWorld

Ví dụ này khai báo một lớp

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
613 và một lớp
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
616 kế thừa từ cả hai lớp
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41 và
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
613

Bạn sẽ thấy một phương thức

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
70 khác sử dụng
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 giống như trong kế thừa đơn lẻ, với mục đích là nó tiếp cận các phương thức
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
621 và
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
70 đã được xác định trong lớp
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
40

Ghi chú. Bạn có thể nhận thấy rằng đoạn mã trên chưa sử dụng bất kỳ thuộc tính kế thừa nào từ lớp

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
613. Các ví dụ sau sẽ tận dụng tối đa tính kế thừa từ cả
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
613 và
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41

Tuy nhiên, vấn đề là cả hai siêu lớp [

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
613 và
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41] đều định nghĩa một
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
70. Hãy dành một chút thời gian và nghĩ xem điều gì có thể xảy ra khi bạn gọi cho
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
70 trên số
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
616, sau đó thử gọi như bên dưới

>>>

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0

Bạn có đoán rằng Python sẽ cố gọi

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
632 không?

Ghi chú. Làm thế nào chúng tôi nhận thấy rằng

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
632 được gọi chứ không phải, như chúng tôi hy vọng,
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
634?

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
60

Bạn có thể nhận ra điều này từ lớp hình học là công thức tính diện tích tam giác. Mặt khác, nếu bạn giống tôi, bạn có thể đã cuộn đến các định nghĩa lớp

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
613 và
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
40 và thấy mã tương tự này trong
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
632

Loại bỏ các quảng cáo

Thứ tự giải quyết phương pháp

Thứ tự phân giải phương thức [hoặc MRO] cho Python biết cách tìm kiếm các phương thức kế thừa. Điều này rất hữu ích khi bạn đang sử dụng

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 vì MRO cho bạn biết chính xác nơi Python sẽ tìm phương thức bạn đang gọi với
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 và theo thứ tự nào

Mỗi lớp có một thuộc tính

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
641 cho phép chúng tôi kiểm tra thứ tự, vì vậy hãy làm điều đó

>>>

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
61

Điều này cho chúng ta biết rằng các phương thức sẽ được tìm kiếm đầu tiên trong

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
642, sau đó là
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
613, sau đó là
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41, sau đó là
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
40, và sau đó, nếu không tìm thấy gì, trong
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
646, từ đó tất cả các lớp bắt nguồn

Vấn đề ở đây là trình thông dịch đang tìm kiếm

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
70 trong
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
613 trước
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41 và
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
40, và khi tìm thấy
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
70 trong
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
613, Python gọi nó thay vì tên bạn muốn. Bởi vì
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
632 mong đợi có một thuộc tính
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
654 và một thuộc tính
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
655, nên Python ném một ____1635

May mắn thay, bạn có một số quyền kiểm soát cách MRO được xây dựng. Chỉ bằng cách thay đổi chữ ký của lớp

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
616, bạn có thể tìm kiếm theo thứ tự bạn muốn và các phương thức sẽ giải quyết chính xác

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
62

Lưu ý rằng

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
616 khởi tạo một phần với
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

class HappyState[State,Event]:
   def __init__[self]:
      print["In the Happy State class"]
      super[].__init__[]                # calls base class 1
      super[State,self].__init__[]      # calls base class 2
a=HappyState[]
70 từ lớp
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41. Điều này cho phép
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
70 sử dụng
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

class HappyState[State,Event]:
   def __init__[self]:
      print["In the Happy State class"]
      super[].__init__[]                # calls base class 1
      super[State,self].__init__[]      # calls base class 2
a=HappyState[]
75 trên đối tượng, như được thiết kế

Bây giờ, bạn có thể xây dựng một kim tự tháp, kiểm tra MRO và tính diện tích bề mặt

>>>

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
63

Bạn thấy rằng MRO bây giờ là những gì bạn mong đợi và bạn cũng có thể kiểm tra khu vực của kim tự tháp, nhờ vào

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
70 và
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
621

Vẫn còn một vấn đề ở đây, mặc dù. Để đơn giản, tôi đã làm sai một vài điều trong ví dụ này. điều đầu tiên và được cho là quan trọng nhất là tôi có hai lớp riêng biệt có cùng tên phương thức và chữ ký

Điều này gây ra sự cố với giải pháp phương pháp, vì phiên bản đầu tiên của

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
70 gặp phải trong danh sách MRO sẽ được gọi

Khi bạn đang sử dụng

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 với nhiều kế thừa, bắt buộc phải thiết kế các lớp của bạn để hợp tác. Một phần của điều này là đảm bảo rằng các phương thức của bạn là duy nhất để chúng được giải quyết trong MRO, bằng cách đảm bảo chữ ký của phương thức là duy nhất—cho dù bằng cách sử dụng tên phương thức hay tham số phương thức

Trong trường hợp này, để tránh sửa chữa toàn bộ mã của bạn, bạn có thể đổi tên phương thức

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
70 của lớp
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
613 thành
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
669. Bằng cách này, các phương thức vùng có thể tiếp tục sử dụng các thuộc tính của lớp thay vì lấy các tham số bên ngoài

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
64

Hãy tiếp tục và sử dụng điều này trong lớp

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
616

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
65

Vấn đề tiếp theo ở đây là mã không có đối tượng

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
613 được ủy quyền giống như đối với đối tượng
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41, vì vậy, việc gọi
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
673 sẽ cho chúng ta một
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
635 vì
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
655 và
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
654 không có bất kỳ giá trị nào

Bạn cần làm hai việc để khắc phục điều này

  1. Tất cả các phương thức được gọi với

    class Rectangle:
        def __init__[self, length, width]:
            self.length = length
            self.width = width
    
        def area[self]:
            return self.length * self.width
    
        def perimeter[self]:
            return 2 * self.length + 2 * self.width
    
    # Here we declare that the Square class inherits from the Rectangle class
    class Square[Rectangle]:
        def __init__[self, length]:
            super[].__init__[length, length]
    
    0 cần phải có một cuộc gọi đến phiên bản siêu lớp của phương thức đó. Điều này có nghĩa là bạn sẽ cần thêm
    #base class1
    class State[]:
       def __init__[self]:
          print["In the State class"]
          self.state1= "Main State"
    
    #base class2
    class Event[]:
       def __init__[self]:
          print["In the Event class"]
          self.event= "Main Event"
    
    #derived class
    class HappyState[State,Event]:
       def __init__[self]:
          print["In the happystate class"]
          super[].__init__[]       #Only calls Base Class 1
    a=HappyState[]
    678 vào phương pháp
    #base class1
    class State[]:
       def __init__[self]:
          print["In the State class"]
          self.state1= "Main State"
    
    #base class2
    class Event[]:
       def __init__[self]:
          print["In the Event class"]
          self.event= "Main Event"
    
    class HappyState[State,Event]:
       def __init__[self]:
          print["In the Happy State class"]
          super[].__init__[]                # calls base class 1
          super[State,self].__init__[]      # calls base class 2
    a=HappyState[]
    70 của
    #base class1
    class State[]:
       def __init__[self]:
          print["In the State class"]
          self.state1= "Main State"
    
    #base class2
    class Event[]:
       def __init__[self]:
          print["In the Event class"]
          self.event= "Main Event"
    
    #derived class
    class HappyState[State,Event]:
       def __init__[self]:
          print["In the happystate class"]
          super[].__init__[]       #Only calls Base Class 1
    a=HappyState[]
    613 và
    #base class1
    class State[]:
       def __init__[self]:
          print["In the State class"]
          self.state1= "Main State"
    
    #base class2
    class Event[]:
       def __init__[self]:
          print["In the Event class"]
          self.event= "Main Event"
    
    #derived class
    class HappyState[State,Event]:
       def __init__[self]:
          print["In the happystate class"]
          super[].__init__[]       #Only calls Base Class 1
    a=HappyState[]
    40

  2. Thiết kế lại tất cả các cuộc gọi

    #base class1
    class State[]:
       def __init__[self]:
          print["In the State class"]
          self.state1= "Main State"
    
    #base class2
    class Event[]:
       def __init__[self]:
          print["In the Event class"]
          self.event= "Main Event"
    
    class HappyState[State,Event]:
       def __init__[self]:
          print["In the Happy State class"]
          super[].__init__[]                # calls base class 1
          super[State,self].__init__[]      # calls base class 2
    a=HappyState[]
    70 để lấy từ điển từ khóa. Xem mã hoàn chỉnh bên dưới

Ví dụ về mã hoàn chỉnhHiển thị/Ẩn

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
66

Có một số khác biệt quan trọng trong mã này

  • **______1683 được sửa đổi ở một số chỗ [chẳng hạn như

    #base class1
    class State[]:
       def __init__[self]:
          print["In the State class"]
          self.state1= "Main State"
    
    #base class2
    class Event[]:
       def __init__[self]:
          print["In the Event class"]
          self.event= "Main Event"
    
    #derived class
    class HappyState[State,Event]:
       def __init__[self]:
          print["In the happystate class"]
          super[].__init__[]       #Only calls Base Class 1
    a=HappyState[]
    684]. ** Điều này sẽ cho phép người dùng của các đối tượng này chỉ khởi tạo chúng bằng các đối số có ý nghĩa đối với đối tượng cụ thể đó

  • Thiết lập các đối số được đặt tên trước

    #base class1
    class State[]:
       def __init__[self]:
          print["In the State class"]
          self.state1= "Main State"
    
    #base class2
    class Event[]:
       def __init__[self]:
          print["In the Event class"]
          self.event= "Main Event"
    
    #derived class
    class HappyState[State,Event]:
       def __init__[self]:
          print["In the happystate class"]
          super[].__init__[]       #Only calls Base Class 1
    a=HappyState[]
    685. Bạn có thể thấy điều này trong
    #base class1
    class State[]:
       def __init__[self]:
          print["In the State class"]
          self.state1= "Main State"
    
    #base class2
    class Event[]:
       def __init__[self]:
          print["In the Event class"]
          self.event= "Main Event"
    
    #derived class
    class HappyState[State,Event]:
       def __init__[self]:
          print["In the happystate class"]
          super[].__init__[]       #Only calls Base Class 1
    a=HappyState[]
    684. Điều này có tác dụng gọn gàng là lấy ngay khóa đó ra khỏi từ điển
    #base class1
    class State[]:
       def __init__[self]:
          print["In the State class"]
          self.state1= "Main State"
    
    #base class2
    class Event[]:
       def __init__[self]:
          print["In the Event class"]
          self.event= "Main Event"
    
    #derived class
    class HappyState[State,Event]:
       def __init__[self]:
          print["In the happystate class"]
          super[].__init__[]       #Only calls Base Class 1
    a=HappyState[]
    685, do đó, vào thời điểm nó kết thúc ở cuối MRO trong lớp
    #base class1
    class State[]:
       def __init__[self]:
          print["In the State class"]
          self.state1= "Main State"
    
    #base class2
    class Event[]:
       def __init__[self]:
          print["In the Event class"]
          self.event= "Main Event"
    
    #derived class
    class HappyState[State,Event]:
       def __init__[self]:
          print["In the happystate class"]
          super[].__init__[]       #Only calls Base Class 1
    a=HappyState[]
    646, thì
    #base class1
    class State[]:
       def __init__[self]:
          print["In the State class"]
          self.state1= "Main State"
    
    #base class2
    class Event[]:
       def __init__[self]:
          print["In the Event class"]
          self.event= "Main Event"
    
    #derived class
    class HappyState[State,Event]:
       def __init__[self]:
          print["In the happystate class"]
          super[].__init__[]       #Only calls Base Class 1
    a=HappyState[]
    685 trống

Ghi chú. Theo dõi trạng thái của

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
683 ở đây có thể phức tạp, vì vậy đây là bảng gồm các lệnh gọi của
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

class HappyState[State,Event]:
   def __init__[self]:
      print["In the Happy State class"]
      super[].__init__[]                # calls base class 1
      super[State,self].__init__[]      # calls base class 2
a=HappyState[]
70 theo thứ tự, hiển thị lớp sở hữu lệnh gọi đó và nội dung của
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
683 trong cuộc gọi đó

Đối số ClassNamed_______1683

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
616
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
695,
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
696
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
41
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

class HappyState[State,Event]:
   def __init__[self]:
      print["In the Happy State class"]
      super[].__init__[]                # calls base class 1
      super[State,self].__init__[]      # calls base class 2
a=HappyState[]
77
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
695,
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
00
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
40
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

class HappyState[State,Event]:
   def __init__[self]:
      print["In the Happy State class"]
      super[].__init__[]                # calls base class 1
      super[State,self].__init__[]      # calls base class 2
a=HappyState[]
77,
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
03
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
695,
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
00
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
613
#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
695,
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
00

Bây giờ, khi bạn sử dụng các lớp cập nhật này, bạn có

>>>

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
67

nó hoạt động. Bạn đã sử dụng

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 để điều hướng thành công hệ thống phân cấp lớp phức tạp trong khi sử dụng cả kế thừa và thành phần để tạo các lớp mới với việc triển khai lại tối thiểu

Loại bỏ các quảng cáo

Nhiều lựa chọn thay thế kế thừa

Như bạn có thể thấy, đa kế thừa có thể hữu ích nhưng cũng dẫn đến các tình huống rất phức tạp và mã khó đọc. Cũng hiếm khi có các đối tượng kế thừa gọn gàng mọi thứ từ nhiều đối tượng khác

Nếu bạn thấy mình bắt đầu sử dụng đa thừa kế và phân cấp lớp phức tạp, thì bạn nên tự hỏi liệu bạn có thể đạt được mã rõ ràng và dễ hiểu hơn bằng cách sử dụng thành phần thay vì kế thừa hay không. Vì bài viết này tập trung vào tính kế thừa nên tôi sẽ không đi sâu vào chi tiết về thành phần và cách sử dụng nó trong Python. May mắn thay, Real Python đã xuất bản một hướng dẫn chuyên sâu về cả kế thừa và thành phần trong Python sẽ giúp bạn trở thành chuyên gia OOP ngay lập tức

Có một kỹ thuật khác có thể giúp bạn vượt qua sự phức tạp của đa thừa kế trong khi vẫn mang lại nhiều lợi ích. Kỹ thuật này ở dạng một lớp đơn giản, chuyên biệt được gọi là mixin

Một mixin hoạt động như một loại kế thừa, nhưng thay vì xác định mối quan hệ “is-a”, có thể chính xác hơn khi nói rằng nó xác định mối quan hệ “bao gồm-a”. Với một hỗn hợp, bạn có thể viết một hành vi có thể được đưa trực tiếp vào bất kỳ lớp nào khác

Bên dưới, bạn sẽ thấy một ví dụ ngắn sử dụng

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
10 để cung cấp chức năng cụ thể cho các đối tượng 3D của chúng tôi—trong trường hợp này là phép tính thể tích

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
68

Trong ví dụ này, mã đã được làm lại để bao gồm một mixin có tên là

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
10. Mixin sau đó được sử dụng bởi
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
78 và cung cấp cho
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
78 khả năng tính toán khối lượng của nó, được hiển thị bên dưới

>>>

#base class1
class State[]:
   def __init__[self]:
      print["In the State class"]
      self.state1= "Main State"

#base class2
class Event[]:
   def __init__[self]:
      print["In the Event class"]
      self.event= "Main Event"

#derived class
class HappyState[State,Event]:
   def __init__[self]:
      print["In the happystate class"]
      super[].__init__[]       #Only calls Base Class 1
a=HappyState[]
69

Mixin này có thể được sử dụng theo cách tương tự trong bất kỳ lớp nào khác có diện tích được xác định cho nó và công thức

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
14 trả về âm lượng chính xác

Bản tóm tắt
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0

Trong hướng dẫn này, bạn đã học cách tăng tốc các lớp học của mình với

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0. Hành trình của bạn bắt đầu với việc xem xét kế thừa đơn lẻ và sau đó chỉ ra cách gọi các phương thức siêu lớp một cách dễ dàng với
class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0

Sau đó, bạn đã học cách hoạt động của đa kế thừa trong Python và các kỹ thuật để kết hợp

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0 với đa kế thừa. Bạn cũng đã tìm hiểu về cách Python giải quyết các cuộc gọi phương thức bằng cách sử dụng thứ tự phân giải phương thức [MRO], cũng như cách kiểm tra và sửa đổi MRO để đảm bảo các phương thức thích hợp được gọi vào những thời điểm thích hợp

Để biết thêm thông tin về lập trình hướng đối tượng trong Python và sử dụng

class Rectangle:
    def __init__[self, length, width]:
        self.length = length
        self.width = width

    def area[self]:
        return self.length * self.width

    def perimeter[self]:
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
    def __init__[self, length]:
        super[].__init__[length, length]
0, hãy xem các tài nguyên này

  • Tài liệu chính thức của
    class Rectangle:
        def __init__[self, length, width]:
            self.length = length
            self.width = width
    
        def area[self]:
            return self.length * self.width
    
        def perimeter[self]:
            return 2 * self.length + 2 * self.width
    
    # Here we declare that the Square class inherits from the Rectangle class
    class Square[Rectangle]:
        def __init__[self, length]:
            super[].__init__[length, length]
    
    0
  • class Rectangle:
        def __init__[self, length, width]:
            self.length = length
            self.width = width
    
        def area[self]:
            return self.length * self.width
    
        def perimeter[self]:
            return 2 * self.length + 2 * self.width
    
    # Here we declare that the Square class inherits from the Rectangle class
    class Square[Rectangle]:
        def __init__[self, length]:
            super[].__init__[length, length]
    
    0 của Python Được coi là Siêu nhân của Raymond Hettinger
  • Lập trình hướng đối tượng trong Python 3

Đánh dấu là đã hoàn thành

Xem ngay Hướng dẫn này có một khóa học video liên quan do nhóm Real Python tạo. Xem nó cùng với hướng dẫn bằng văn bản để hiểu sâu hơn. Nâng cấp lớp học của bạn với Python super[]

🐍 Thủ thuật Python 💌

Nhận một Thủ thuật Python ngắn và hấp dẫn được gửi đến hộp thư đến của bạn vài ngày một lần. Không có thư rác bao giờ. Hủy đăng ký bất cứ lúc nào. Được quản lý bởi nhóm Real Python

Gửi cho tôi thủ thuật Python »

Giới thiệu về Kyle Stratis

Kyle là một nhà phát triển tự học đang làm kỹ sư dữ liệu cao cấp tại Vizit Labs. Trong quá khứ, ông đã thành lập DanqEx [trước đây là Nasdanq. sàn giao dịch chứng khoán meme ban đầu] và Encryptid Gaming

» Thông tin thêm về Kyle

Mỗi hướng dẫn tại Real Python được tạo bởi một nhóm các nhà phát triển để nó đáp ứng các tiêu chuẩn chất lượng cao của chúng tôi. Các thành viên trong nhóm đã làm việc trong hướng dẫn này là

Aldren

Geir Arne

Joanna

Bậc thầy Kỹ năng Python trong thế giới thực Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng nghìn hướng dẫn, khóa học video thực hành và cộng đồng các Pythonista chuyên gia

Nâng cao kỹ năng Python của bạn »

Bậc thầy Kỹ năng Python trong thế giới thực
Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn, khóa học video thực hành và cộng đồng các chuyên gia Pythonistas

Nâng cao kỹ năng Python của bạn »

Bạn nghĩ sao?

Đánh giá bài viết này

Tweet Chia sẻ Chia sẻ Email

Bài học số 1 hoặc điều yêu thích mà bạn đã học được là gì?

Mẹo bình luận. Những nhận xét hữu ích nhất là những nhận xét được viết với mục đích học hỏi hoặc giúp đỡ các sinh viên khác. Nhận các mẹo để đặt câu hỏi hay và nhận câu trả lời cho các câu hỏi phổ biến trong cổng thông tin hỗ trợ của chúng tôi

Python có tự động gọi super init không?

Nếu có siêu lớp [init] mà bạn muốn gọi, bạn phải gọi chúng một cách rõ ràng bằng super[]. __init__[] đệ quy. Nếu không có [init] được xác định dọc theo đường dẫn cây, sẽ không có gì được gọi. Đây chỉ là một chức năng thông thường được tự động gọi một lần khi xây dựng .

Super[] __ Init__ trong Python là gì?

__init__[] Gọi trong Python. Khi bạn khởi tạo một lớp con trong Python, bạn có thể gọi phương thức super[]. phương thức __init__[]. Điều này khởi tạo đối tượng lớp cha thành lớp con . Ngoài ra, bạn cũng có thể thêm thông tin dành riêng cho trẻ em vào đối tượng trẻ em.

Tác dụng của việc gọi super[] __Init__ là gì?

Tác dụng của việc gọi super[] __ Init__ là gì? . super[] trả về một đối tượng ủy nhiệm cho một lớp cha, vì vậy bạn gọi phương thức bạn muốn trực tiếp trên nó. siêu[]. will be called automatically. super[] returns a delegate object to a parent class, so you call the method you want directly on it: super[].

Bạn có thể gọi __ init __ bằng Python không?

Bạn không thể gọi chúng một cách rõ ràng . Chẳng hạn, khi bạn tạo một đối tượng mới, Python sẽ tự động gọi phương thức __new__, phương thức này sẽ gọi phương thức __init__. Phương thức __str__ được gọi khi bạn print[] một đối tượng. Mặt khác, các phương thức do người dùng định nghĩa, như stefi.

Chủ Đề