Không có ngôn ngữ lập trình hướng đối tượng nào đáng để xem xét hoặc sử dụng nếu nó không hỗ trợ tính kế thừa. Kế thừa được phát minh vào năm 1969 cho Simula. Python không chỉ hỗ trợ kế thừa mà còn hỗ trợ đa kế thừa. Nói chung, kế thừa là cơ chế tạo ra các lớp mới từ các lớp hiện có. Bằng cách này, chúng ta có được một hệ thống phân cấp các lớp. Trong hầu hết các ngôn ngữ hướng đối tượng dựa trên lớp, một đối tượng được tạo thông qua kế thừa [một "đối tượng con"] có được tất cả - mặc dù có một số ngoại lệ trong một số ngôn ngữ lập trình - thuộc tính và hành vi của đối tượng cha
Kế thừa cho phép lập trình viên tạo các lớp được xây dựng dựa trên các lớp hiện có và điều này cho phép một lớp được tạo thông qua kế thừa kế thừa các thuộc tính và phương thức của lớp cha. Điều này có nghĩa là tính kế thừa hỗ trợ khả năng sử dụng lại mã. Các phương thức hay nói chung là phần mềm được kế thừa bởi một lớp con được coi là được sử dụng lại trong lớp con. Mối quan hệ của các đối tượng hoặc các lớp thông qua kế thừa tạo ra một đồ thị có hướng
Lớp mà một lớp kế thừa được gọi là lớp cha hoặc lớp cha. Một lớp kế thừa từ một lớp cha được gọi là một lớp con, còn được gọi là lớp kế thừa hoặc lớp con. Các siêu lớp đôi khi cũng được gọi là tổ tiên. Tồn tại mối quan hệ thứ bậc giữa các lớp. Nó tương tự như các mối quan hệ hoặc phân loại mà chúng ta biết từ cuộc sống thực. Ví dụ, hãy nghĩ về phương tiện. Xe đạp, ô tô, xe buýt và xe tải là phương tiện. Xe bán tải, xe tải, xe thể thao, xe mui trần và xe bất động sản đều là xe ô tô và với tư cách là ô tô, chúng cũng là phương tiện. Chúng tôi có thể triển khai một lớp phương tiện trong Python, lớp này có thể có các phương thức như tăng tốc và phanh. Ô tô, Xe buýt và Xe tải và Xe đạp có thể được triển khai dưới dạng các lớp con sẽ kế thừa các phương thức này từ phương tiện
Đào tạo Python trực tiếp
Thưởng thức trang này?
Nhìn thấy. Tổng quan về các khóa học Python trực tiếp
đăng ký tại đây
Cú pháp kế thừa trong Python
Cú pháp cho một định nghĩa lớp con trông như thế này
class DerivedClassName[BaseClassName]:
pass
Thay vì câu lệnh
Hi, I am James9 sẽ có các phương thức và thuộc tính như ở các lớp khác. Tên BaseClassName phải được xác định trong phạm vi chứa định nghĩa lớp dẫn xuất
Bây giờ chúng ta đã sẵn sàng cho một ví dụ kế thừa đơn giản với mã Python
Ví dụ kế thừa đơn giản
Chúng tôi sẽ gắn bó với các rô-bốt yêu quý của chúng tôi hoặc tốt hơn là lớp
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]0 từ các chương trước của hướng dẫn Python của chúng tôi để chỉ ra nguyên tắc kế thừa hoạt động như thế nào. Chúng ta sẽ định nghĩa một lớp
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]1, kế thừa từ lớp
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]0
class Robot: def __init__[self, name]: self.name = name def say_hi[self]: print["Hi, I am " + self.name] class PhysicianRobot[Robot]: pass x = Robot["Marvin"] y = PhysicianRobot["James"] print[x, type[x]] print[y, type[y]] y.say_hi[]
ĐẦU RA
Hi, I am James
Nếu bạn nhìn vào mã của lớp
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]1 của chúng tôi, bạn có thể thấy rằng chúng tôi chưa định nghĩa bất kỳ thuộc tính hoặc phương thức nào trong lớp này. Vì lớp
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]1 là lớp con của lớp
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]0, trong trường hợp này, nó kế thừa cả phương thức
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]6 và
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]7. Kế thừa các phương thức này có nghĩa là chúng ta có thể sử dụng chúng như thể chúng đã được định nghĩa trong lớp
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]1. Khi chúng ta tạo một thể hiện của
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]1, hàm
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]6 cũng sẽ tạo một thuộc tính name. Chúng ta có thể áp dụng phương thức
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]7 cho đối tượng
Hi, I am James62
Hi, I am James63, như chúng ta có thể thấy trong kết quả từ đoạn mã trên
Đào tạo Python trực tiếp
Thưởng thức trang này?
Nhìn thấy. Tổng quan về các khóa học Python trực tiếp
Các khóa học trực tuyến sắp tới
Khóa học nâng cao chuyên sâu
Lập trình hướng đối tượng với Python
đăng ký tại đây
Sự khác biệt giữa
Hi, I am James
64 và
Hi, I am James
65
Bạn cũng nên chú ý đến các sự kiện sau, mà chúng tôi cũng đã chỉ ra trong các phần khác của hướng dẫn Python của chúng tôi. Mọi người thường hỏi sự khác biệt giữa kiểm tra loại thông qua chức năng
Hi, I am James64 hoặc chức năng
Hi, I am James65 nằm ở đâu. Sự khác biệt có thể được nhìn thấy trong đoạn mã sau. Chúng ta thấy rằng
Hi, I am James65 trả về
Hi, I am James69 nếu chúng ta so sánh một đối tượng với lớp mà nó thuộc về hoặc với siêu lớp. Trong khi toán tử đẳng thức chỉ trả về
Hi, I am James69, nếu chúng ta so sánh một đối tượng với lớp của chính nó
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]
ĐẦU RA
Hi, I am James6
Điều này thậm chí đúng với tổ tiên tùy ý của lớp trong dòng thừa kế
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]7
ĐẦU RA
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]8
Bây giờ nó đã rõ ràng, tại sao, Hướng dẫn phong cách chính thức cho mã Python, nói. "So sánh loại đối tượng phải luôn sử dụng isinstance[] thay vì so sánh trực tiếp các loại. "
ghi đè
Hãy để chúng tôi quay lại lớp học
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]1 mới của chúng tôi. Bây giờ hãy tưởng tượng rằng một phiên bản của
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]1 sẽ nói xin chào theo một cách khác. Trong trường hợp này, chúng ta phải định nghĩa lại phương thức
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]7 bên trong lớp con
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]1
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]3
ĐẦU RA
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]4
Những gì chúng ta đã làm trong ví dụ trước được gọi là ghi đè. Một phương thức của lớp cha bị ghi đè bằng cách định nghĩa một phương thức có cùng tên trong lớp con
Nếu một phương thức bị ghi đè trong một lớp, thì phương thức ban đầu vẫn có thể được truy cập, nhưng chúng ta phải làm điều đó bằng cách gọi trực tiếp phương thức đó bằng tên lớp, i. e.
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]75. Chúng tôi chứng minh điều này trong đoạn mã sau
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]6
ĐẦU RA
class Robot: def __init__[self, name]: self.name = name def say_hi[self]: print["Hi, I am " + self.name] class PhysicianRobot[Robot]: pass x = Robot["Marvin"] y = PhysicianRobot["James"] print[x, type[x]] print[y, type[y]] y.say_hi[]0
Chúng ta đã thấy rằng một lớp kế thừa có thể kế thừa và ghi đè các phương thức từ lớp cha. Bên cạnh đó, một lớp con thường cần các phương thức bổ sung với các chức năng bổ sung, không tồn tại trong lớp cha. Ví dụ, một thể hiện của lớp
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]1 sẽ cần phương thức
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]77 để bác sĩ có thể thực hiện công việc phù hợp. Chúng ta cũng sẽ thêm thuộc tính
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]78 vào lớp
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]0, thuộc tính này có thể nhận giá trị trong khoảng từ
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]80 đến
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]81. Các rô-bốt sẽ 'sống dậy' với giá trị ngẫu nhiên trong khoảng từ
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]80 đến
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]81. Nếu
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]78 của một
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]0 dưới 0. 8, nó sẽ cần một bác sĩ. Chúng ta viết một phương thức
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]86 trả về
Hi, I am James69 nếu giá trị thấp hơn
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]88 và ngược lại là
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]89. Việc 'chữa bệnh' trong phương pháp
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]77 được thực hiện bằng cách đặt
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]78 thành một giá trị ngẫu nhiên giữa
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]78 cũ và 1. Giá trị này được tính toán bởi hàm thống nhất của mô-đun ngẫu nhiên
class Robot: def __init__[self, name]: self.name = name def say_hi[self]: print["Hi, I am " + self.name] class PhysicianRobot[Robot]: pass x = Robot["Marvin"] y = PhysicianRobot["James"] print[x, type[x]] print[y, type[y]] y.say_hi[]1
ĐẦU RA
class Robot: def __init__[self, name]: self.name = name def say_hi[self]: print["Hi, I am " + self.name] class PhysicianRobot[Robot]: pass x = Robot["Marvin"] y = PhysicianRobot["James"] print[x, type[x]] print[y, type[y]] y.say_hi[]2
Khi ghi đè một phương thức, đôi khi chúng ta muốn sử dụng lại phương thức của lớp cha và một số nội dung mới. Để chứng minh điều này, chúng ta sẽ viết một phiên bản mới của PhysicianRobot.
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]7 sẽ trả lại văn bản từ phiên bản lớp Người máy cộng với văn bản " và tôi là bác sĩ. "
class Robot: def __init__[self, name]: self.name = name def say_hi[self]: print["Hi, I am " + self.name] class PhysicianRobot[Robot]: pass x = Robot["Marvin"] y = PhysicianRobot["James"] print[x, type[x]] print[y, type[y]] y.say_hi[]3
ĐẦU RA
class Robot: def __init__[self, name]: self.name = name def say_hi[self]: print["Hi, I am " + self.name] class PhysicianRobot[Robot]: pass x = Robot["Marvin"] y = PhysicianRobot["James"] print[x, type[x]] print[y, type[y]] y.say_hi[]4
Chúng tôi không muốn viết mã dư thừa và do đó chúng tôi đã gọi
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]34. Chúng ta cũng có thể sử dụng hàm
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]35
class Robot: def __init__[self, name]: self.name = name def say_hi[self]: print["Hi, I am " + self.name] class PhysicianRobot[Robot]: pass x = Robot["Marvin"] y = PhysicianRobot["James"] print[x, type[x]] print[y, type[y]] y.say_hi[]5
ĐẦU RA
class Robot: def __init__[self, name]: self.name = name def say_hi[self]: print["Hi, I am " + self.name] class PhysicianRobot[Robot]: pass x = Robot["Marvin"] y = PhysicianRobot["James"] print[x, type[x]] print[y, type[y]] y.say_hi[]4
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]35 không thực sự cần thiết trong trường hợp này. Người ta có thể lập luận rằng nó làm cho mã dễ bảo trì hơn, bởi vì chúng ta có thể thay đổi tên của lớp cha, nhưng dù sao thì điều này hiếm khi được thực hiện trong các lớp hiện có. Lợi ích thực sự của
x = Robot["Marvin"] y = PhysicianRobot["James"] print[isinstance[x, Robot], isinstance[y, Robot]] print[isinstance[x, PhysicianRobot]] print[isinstance[y, PhysicianRobot]] print[type[y] == Robot, type[y] == PhysicianRobot]35 thể hiện khi chúng ta sử dụng nó với nhiều kế thừa
Đào tạo Python trực tiếp
Thưởng thức trang này?
Nhìn thấy. Tổng quan về các khóa học Python trực tiếp
đăng ký tại đây
Phân biệt giữa ghi đè, nạp chồng và ghi đè
ghi đè
Nếu chúng ta ghi đè lên một chức năng, chức năng ban đầu sẽ biến mất. Chức năng sẽ được xác định lại. Quá trình này không liên quan gì đến hướng đối tượng hoặc kế thừa
class Robot: def __init__[self, name]: self.name = name def say_hi[self]: print["Hi, I am " + self.name] class PhysicianRobot[Robot]: pass x = Robot["Marvin"] y = PhysicianRobot["James"] print[x, type[x]] print[y, type[y]] y.say_hi[]7
ĐẦU RA
class Robot: def __init__[self, name]: self.name = name def say_hi[self]: print["Hi, I am " + self.name] class PhysicianRobot[Robot]: pass x = Robot["Marvin"] y = PhysicianRobot["James"] print[x, type[x]] print[y, type[y]] y.say_hi[]8
quá tải
Chương phụ này sẽ chỉ thú vị đối với các lập trình viên C++ và Java, những người muốn biết cách nạp chồng có thể được thực hiện trong Python. Ai chưa biết về quá tải thì đừng bỏ qua
Trong bối cảnh lập trình hướng đối tượng, bạn cũng có thể đã nghe nói về "quá tải". Mặc dù "quá tải" không được kết nối trực tiếp với OOP. Quá tải là khả năng xác định một hàm có cùng tên nhiều lần. Các định nghĩa khác nhau liên quan đến số lượng tham số và loại tham số. Đó là khả năng của một chức năng thực hiện các tác vụ khác nhau, tùy thuộc vào số lượng tham số hoặc loại tham số. Chúng ta không thể quá tải các hàm như thế này trong Python, nhưng cũng không cần thiết
Tuy nhiên, khóa học này không phải về C++ và cho đến nay chúng tôi đã tránh sử dụng bất kỳ mã C++ nào. Bây giờ chúng tôi muốn tạo một ngoại lệ để bạn có thể thấy quá tải hoạt động như thế nào trong C++
class Robot: def __init__[self, name]: self.name = name def say_hi[self]: print["Hi, I am " + self.name] class PhysicianRobot[Robot]: pass x = Robot["Marvin"] y = PhysicianRobot["James"] print[x, type[x]] print[y, type[y]] y.say_hi[]9
Chúng tôi đã xác định chức năng kế thừa hai lần. Một lần cho int và lần khác với float làm Tham số. Trong Python, hàm có thể được định nghĩa như thế này, chắc chắn bạn sẽ biết
Hi, I am James0
Vì x chỉ là một tham chiếu đến một đối tượng, hàm kế nhiệm Python có thể được gọi với mọi đối tượng, mặc dù nó sẽ tạo ra các ngoại lệ với nhiều loại. Nhưng nó sẽ hoạt động với các giá trị int và float
Có một hàm với một số lượng tham số khác nhau là một cách nạp chồng hàm khác. Chương trình C++ sau đây cho thấy một ví dụ như vậy. Hàm f có thể được gọi với một hoặc hai đối số nguyên
Hi, I am James1
Điều này không hoạt động trong Python, như chúng ta có thể thấy trong ví dụ sau. Định nghĩa thứ hai của f với hai tham số xác định lại hoặc ghi đè định nghĩa đầu tiên bằng một đối số. Ghi đè có nghĩa là định nghĩa đầu tiên không còn nữa
Hi, I am James2
ĐẦU RA
Hi, I am James3
Nếu bạn gọi f chỉ với một tham số, bạn sẽ đưa ra một ngoại lệ
Hi, I am James4
Tuy nhiên, có thể mô phỏng hành vi nạp chồng của C++ bằng Python trong trường hợp này với tham số mặc định
Hi, I am James5
ĐẦU RA
Hi, I am James6
Toán tử * có thể được sử dụng như một cách tiếp cận tổng quát hơn cho một họ các hàm có 1, 2, 3 hoặc thậm chí nhiều tham số hơn