Trong bài viết này, bạn sẽ tìm hiểu về thừa kế Python. Bạn sẽ hiểu sâu hơn về cách một lớp bắt nguồn từ một lớp khác kế thừa các tính năng của nó
Kế thừa trong Python
Kế thừa, như tên cho thấy, là một quá trình kế thừa các tính năng
Đây là một kỹ thuật xây dựng các lớp mới được gọi là lớp dẫn xuất từ lớp hiện có được gọi là lớp cơ sở bằng cách kế thừa các tính năng của lớp cơ sở. Các tính năng bao gồm các thuộc tính và phương pháp hành vi khác nhau
Nhìn hình này để hiểu rõ hơn
Kế thừa là một trong những tính năng mạnh mẽ của OOP. Trong lớp dẫn xuất, chúng ta có thể thêm các tính năng bổ sung và cũng có thể ghi đè các thành viên dữ liệu và phương thức từ lớp cha
Ví dụ: nếu phương tiện là lớp cha, thì ô tô, xe buýt và xe tải là các lớp dẫn xuất được kế thừa tất cả các tính năng của lớp cha phương tiện với nhiều tính năng bổ sung hơn.
Cú pháp kế thừa Python
class derived_class_name[base_class_name]:
##Body of the derived class
Ví dụ kế thừa Python
Hãy lấy Xe làm lớp cha từ đó chúng ta sẽ lấy được một lớp Thể loại. Lớp Category sẽ kế thừa các tính năng của lớp cha Xe và cũng gọi hàm từ lớp cha
Đây là mã
class Vehicle: #parent class
"Parent Class"
def __init__[self, price]:
self.price = price
def display[self]:
print ['Price = $',self.price]
class Category[Vehicle]: #derived class
"Child/Derived class"
def __init__[self, price, name]:
Vehicle.__init__[self, price]
self.name = name
def disp_name[self]:
print ['Vehicle = ',self.name]
obj = Category[1200, 'BMW']
obj.disp_name[]
obj.display[]
đầu ra
Vehicle = BMW Price = $1200
Ở đây trong chương trình, chúng ta chỉ khởi tạo lớp dẫn xuất Category. Vì Category là lớp con của Vehicle, chúng ta có thể sử dụng hàm display[]
của lớp cơ sở bằng cách sử dụng đối tượng của lớp dẫn xuất
Python isinstance[] và issubclass[]
Các hàm issubclass[]
hoặc isinstance[]
trong Python dùng để kiểm tra mối quan hệ của hai lớp và thể hiện
- issubclass[] được sử dụng để kiểm tra tính kế thừa của lớp. Ví dụ:
class Vehicle: #parent class "Parent Class" def __init__[self, price]: self.price = price def display[self]: print ['Price = $',self.price] class Category[Vehicle]: #derived class "Child/Derived class" def __init__[self, price, name]: Vehicle.__init__[self, price] self.name = name def disp_name[self]: print ['Vehicle = ',self.name] obj = Category[1200, 'BMW'] obj.disp_name[] obj.display[]
0 sẽ chỉ trả về giá trị đúng nếuclass Vehicle: #parent class "Parent Class" def __init__[self, price]: self.price = price def display[self]: print ['Price = $',self.price] class Category[Vehicle]: #derived class "Child/Derived class" def __init__[self, price, name]: Vehicle.__init__[self, price] self.name = name def disp_name[self]: print ['Vehicle = ',self.name] obj = Category[1200, 'BMW'] obj.disp_name[] obj.display[]
1 là lớp con củaclass Vehicle: #parent class "Parent Class" def __init__[self, price]: self.price = price def display[self]: print ['Price = $',self.price] class Category[Vehicle]: #derived class "Child/Derived class" def __init__[self, price, name]: Vehicle.__init__[self, price] self.name = name def disp_name[self]: print ['Vehicle = ',self.name] obj = Category[1200, 'BMW'] obj.disp_name[] obj.display[]
2.>>> issubclass[Category, Vehicle] True >>> issubclass[Vehicle, Category] #Vehicle is not subclass of Category False >>> issubclass[1, int] #1 belongs to class int True
- isinstance[ ] được sử dụng để kiểm tra thể hiện của một lớp. Ví dụ:
class Vehicle: #parent class "Parent Class" def __init__[self, price]: self.price = price def display[self]: print ['Price = $',self.price] class Category[Vehicle]: #derived class "Child/Derived class" def __init__[self, price, name]: Vehicle.__init__[self, price] self.name = name def disp_name[self]: print ['Vehicle = ',self.name] obj = Category[1200, 'BMW'] obj.disp_name[] obj.display[]
3 sẽ chỉ trả về giá trị đúng nếuclass Vehicle: #parent class "Parent Class" def __init__[self, price]: self.price = price def display[self]: print ['Price = $',self.price] class Category[Vehicle]: #derived class "Child/Derived class" def __init__[self, price, name]: Vehicle.__init__[self, price] self.name = name def disp_name[self]: print ['Vehicle = ',self.name] obj = Category[1200, 'BMW'] obj.disp_name[] obj.display[]
4 là thể hiện củaclass Vehicle: #parent class "Parent Class" def __init__[self, price]: self.price = price def display[self]: print ['Price = $',self.price] class Category[Vehicle]: #derived class "Child/Derived class" def __init__[self, price, name]: Vehicle.__init__[self, price] self.name = name def disp_name[self]: print ['Vehicle = ',self.name] obj = Category[1200, 'BMW'] obj.disp_name[] obj.display[]
5 hoặc của bất kỳ lớp nào bắt nguồn từclass Vehicle: #parent class "Parent Class" def __init__[self, price]: self.price = price def display[self]: print ['Price = $',self.price] class Category[Vehicle]: #derived class "Child/Derived class" def __init__[self, price, name]: Vehicle.__init__[self, price] self.name = name def disp_name[self]: print ['Vehicle = ',self.name] obj = Category[1200, 'BMW'] obj.disp_name[] obj.display[]
6.>>> isinstance[obj, Vehicle] True >>> isinstance[obj, Category] True
Ghi đè các phương thức trong kế thừa Python
Đôi khi chúng ta cần thay đổi hoạt động chức năng của một phương thức được định nghĩa trong lớp cơ sở. Thay vì thay đổi phương thức trong lớp cơ sở, chúng ta có thể định nghĩa cùng chức năng đó trong lớp dẫn xuất và thay đổi mã. Chức năng của lớp cơ sở bị ghi đè bởi chức năng của lớp dẫn xuất
Để ghi đè một phương thức trong lớp cơ sở, chúng ta phải định nghĩa một phương thức mới có cùng tên và cùng tham số trong lớp dẫn xuất
Đây là một ví dụ
class A: #parent class
"Parent Class"
def display[self]:
print ['This is base class.']
class B[A]: #derived class
"Child/Derived class"
def display[self]:
print ['This is derived class.']
obj = B[]
obj.display[]
đầu ra
This is derived class.
Lưu ý rằng cơ sở
class Vehicle: #parent class
"Parent Class"
def __init__[self, price]:
self.price = price
def display[self]:
print ['Price = $',self.price]
class Category[Vehicle]: #derived class
"Child/Derived class"
def __init__[self, price, name]:
Vehicle.__init__[self, price]
self.name = name
def disp_name[self]:
print ['Vehicle = ',self.name]
obj = Category[1200, 'BMW']
obj.disp_name[]
obj.display[]
5 và dẫn xuất class Vehicle: #parent class
"Parent Class"
def __init__[self, price]:
self.price = price
def display[self]:
print ['Price = $',self.price]
class Category[Vehicle]: #derived class
"Child/Derived class"
def __init__[self, price, name]:
Vehicle.__init__[self, price]
self.name = name
def disp_name[self]:
print ['Vehicle = ',self.name]
obj = Category[1200, 'BMW']
obj.disp_name[]
obj.display[]
8 có cùng một phương pháp class Vehicle: #parent class
"Parent Class"
def __init__[self, price]:
self.price = price
def display[self]:
print ['Price = $',self.price]
class Category[Vehicle]: #derived class
"Child/Derived class"
def __init__[self, price, name]:
Vehicle.__init__[self, price]
self.name = name
def disp_name[self]:
print ['Vehicle = ',self.name]
obj = Category[1200, 'BMW']
obj.disp_name[]
obj.display[]
9. Nhưng khi chúng ta gọi hàm với thể hiện là class Vehicle: #parent class
"Parent Class"
def __init__[self, price]:
self.price = price
def display[self]:
print ['Price = $',self.price]
class Category[Vehicle]: #derived class
"Child/Derived class"
def __init__[self, price, name]:
Vehicle.__init__[self, price]
self.name = name
def disp_name[self]:
print ['Vehicle = ',self.name]
obj = Category[1200, 'BMW']
obj.disp_name[]
obj.display[]
8, thì hàm trong lớp cơ sở có cùng tên sẽ bị ghi đè bởi hàm trong lớp dẫn xuất