Trong bài viết trước của chúng ta, khi chúng ta đang nói về thừa kế, một
Features of the mother class
Features of the father class
4 đang kế thừa các thuộc tính và chức năng của một Features of the mother class
Features of the father class
4 khác. Vậy đa thừa kế là gì? Giống như một đứa trẻ ngoài đời có những đặc điểm của cả bố và mẹ. Hãy bắt tay vào viết mã ngay lập tức, bạn sẽ hiểu rõ hơn nhiều
class Mother:
def print_Mother[self]:
print["Features of the mother class"]
class Father:
def print_Father[self]:
print["Features of the father class"]
class Child[Mother, Father]:
pass
object_d = Child[]
object_d.print_Mother[]
object_d.print_Father[]
Bây giờ chúng ta hãy xem đầu ra;
Features of the mother class
Features of the father class
Như bạn có thể thấy, chúng tôi đã lấy và sử dụng các chức năng của nhiều lớp
Nhưng có một điểm rất quan trọng mà chúng ta cần chú ý là trong multifunction, chúng ta chỉ có thể sử dụng một
Features of the mother class
Features of the father class
4, một thuộc tính hoặc một hàm trùng tên thôi, ví dụ ngay thôiclass Mother:
def eye_colour[self]:
print["Features of the mother class"]
class Father:
def eye_colour[self]:
print["Features of the father class"]
class Child[Mother, Father]:
pass
object_d = Child[]
object_d.eye_colour[]
Cả lớp
Features of the mother class
Features of the father class
9 và lớp class Mother:
def eye_colour[self]:
print["Features of the mother class"]
class Father:
def eye_colour[self]:
print["Features of the father class"]
class Child[Mother, Father]:
pass
object_d = Child[]
object_d.eye_colour[]
0 đều có thuộc tính class Mother:
def eye_colour[self]:
print["Features of the mother class"]
class Father:
def eye_colour[self]:
print["Features of the father class"]
class Child[Mother, Father]:
pass
object_d = Child[]
object_d.eye_colour[]
1Vì vậy, hãy xem lớp
class Mother:
def eye_colour[self]:
print["Features of the mother class"]
class Father:
def eye_colour[self]:
print["Features of the father class"]
class Child[Mother, Father]:
pass
object_d = Child[]
object_d.eye_colour[]
2 sẽ kế thừa từ lớp nàoFeatures of the mother class
Features of the father class
2Đúng như chúng tôi đoán, cô ấy mang đặc điểm của lớp
class Mother:
def eye_colour[self]:
print["Features of the mother class"]
class Father:
def eye_colour[self]:
print["Features of the father class"]
class Child[Mother, Father]:
pass
object_d = Child[]
object_d.eye_colour[]
3. Bởi vì trước tiên, lớp Features of the mother class
Features of the father class
9 được kế thừa các thuộc tính của nó. Bây giờ hãy thay đổi nó và lần này kế thừa lớp class Mother:
def eye_colour[self]:
print["Features of the mother class"]
class Father:
def eye_colour[self]:
print["Features of the father class"]
class Child[Mother, Father]:
pass
object_d = Child[]
object_d.eye_colour[]
0 trướcFeatures of the mother class
Features of the father class
6đầu tiên, chúng tôi kế thừa lớp
class Mother:
def eye_colour[self]:
print["Features of the mother class"]
class Father:
def eye_colour[self]:
print["Features of the father class"]
class Child[Mother, Father]:
pass
object_d = Child[]
object_d.eye_colour[]
0, hãy xem đầu ra của chúng tôiFeatures of the mother class
Features of the father class
8Bây giờ bạn sẽ nói rằng tại sao tôi phải tạo một hàm có cùng tên, tôi sẽ tạo một hàm có tên khác. Nhưng hãy nhớ rằng, bạn có thể không đơn độc trong một dự án. Bạn có thể gặp phải những vấn đề như vậy khi làm việc với nhiều người. Hãy nói về những việc cần làm nếu hàm xây dựng
class Mother:
def eye_colour[self]:
print["Features of the mother class"]
class Father:
def eye_colour[self]:
print["Features of the father class"]
class Child[Mother, Father]:
pass
object_d = Child[]
object_d.eye_colour[]
7 tồn tại trong cả hai lớp. Hãy thể hiện nó ngay bây giờ;Features of the mother class
Features of the father class
0Như bạn có thể thấy khi chúng tôi chạy mã của mình
Features of the mother class
Features of the father class
1Chúng tôi nhận được kết quả như vậy Vì vậy, vì chúng tôi kế thừa lớp
class Mother:
def eye_colour[self]:
print["Features of the mother class"]
class Father:
def eye_colour[self]:
print["Features of the father class"]
class Child[Mother, Father]:
pass
object_d = Child[]
object_d.eye_colour[]
0 trước nên thuộc tính của lớp class Mother:
def eye_colour[self]:
print["Features of the mother class"]
class Father:
def eye_colour[self]:
print["Features of the father class"]
class Child[Mother, Father]:
pass
object_d = Child[]
object_d.eye_colour[]
0 được ưu tiên. Chà, bây giờ bạn sẽ nói rằng chúng ta không thể tự thay đổi thứ tự ưu tiên này khi thích hợp? Hàm xây dựng trong đa kế thừa
Features of the mother class
Features of the father class
4Hãy nhìn vào đầu ra;
Features of the mother class
Features of the father class
5Ở đây chúng ta đã ưu tiên hàm
class Mother:
def eye_colour[self]:
print["Features of the mother class"]
class Father:
def eye_colour[self]:
print["Features of the father class"]
class Child[Mother, Father]:
pass
object_d = Child[]
object_d.eye_colour[]
7 của lớp Features of the mother class
Features of the father class
9. Các bạn để ý một chút sẽ thấy câu này, chúng ta chỉ nhận được hàmclass Mother:
def eye_colour[self]:
print["Features of the mother class"]
class Father:
def eye_colour[self]:
print["Features of the father class"]
class Child[Mother, Father]:
pass
object_d = Child[]
object_d.eye_colour[]
7 chứ không phải tất cả các thuộc tính và hàm của lớp Features of the mother class
Features of the father class
9. Làm thế nào chúng ta có thể nhận được thông tin từ đây? Hãy cho thấy nó với một ví dụ
Features of the mother class
Features of the father class
0đầu ra
Features of the mother class
Features of the father class
1Như bạn có thể thấy, đứa trẻ đã nhận được đặc điểm màu mắt từ Mẹ và đặc điểm chiều cao từ Cha. Lý do là vì chúng ta viết lớp
class Mother:
def eye_colour[self]:
print["Features of the mother class"]
class Father:
def eye_colour[self]:
print["Features of the father class"]
class Child[Mother, Father]:
pass
object_d = Child[]
object_d.eye_colour[]
0 trước nên nó kế thừa hàm Features of the mother class
Features of the father class
28 của lớp class Mother:
def eye_colour[self]:
print["Features of the mother class"]
class Father:
def eye_colour[self]:
print["Features of the father class"]
class Child[Mother, Father]:
pass
object_d = Child[]
object_d.eye_colour[]
0. Nhưng vì chúng ta đã chỉ định hàm class Mother:
def eye_colour[self]:
print["Features of the mother class"]
class Father:
def eye_colour[self]:
print["Features of the father class"]
class Child[Mother, Father]:
pass
object_d = Child[]
object_d.eye_colour[]
7 của lớp Features of the mother class
Features of the father class
9 là ưu tiên, nên nó kế thừa hàm class Mother:
def eye_colour[self]:
print["Features of the mother class"]
class Father:
def eye_colour[self]:
print["Features of the father class"]
class Child[Mother, Father]:
pass
object_d = Child[]
object_d.eye_colour[]
7 của lớp Mother là ưu tiênTheo cách tương tự, nếu chúng ta ưu tiên hàm
Features of the mother class
Features of the father class
28 của lớp Features of the mother class
Features of the father class
9, thì hàm Features of the mother class
Features of the father class
28 của lớp Features of the mother class
Features of the father class
9 sẽ được ưu tiênFeatures of the mother class
Features of the father class
2Khi chúng ta nhìn vào đầu ra, bạn sẽ thấy rằng nó kế thừa cả hai thuộc tính từ lớp
Features of the mother class
Features of the father class
9Features of the mother class
Features of the father class
3Như bạn có thể thấy, mặc dù chúng ta đã viết lớp
class Mother:
def eye_colour[self]:
print["Features of the mother class"]
class Father:
def eye_colour[self]:
print["Features of the father class"]
class Child[Mother, Father]:
pass
object_d = Child[]
object_d.eye_colour[]
0 trước nhưng nó lại lấy các thuộc tính của lớp Features of the mother class
Features of the father class
9 vì chúng ta đã ưu tiên cho các thuộc tính của lớp Features of the mother class
Features of the father class
9