Ghi đè phương thức trong Python là khi bạn có hai phương thức có cùng tên, mỗi phương thức thực hiện các tác vụ khác nhau. Ghi đè phương thức là một khả năng của bất kỳ ngôn ngữ lập trình hướng đối tượng nào cho phép một lớp con hoặc lớp con cung cấp một triển khai cụ thể của một phương thức đã được cung cấp bởi một trong các lớp cha hoặc lớp cha của nó. Khi một phương thức trong lớp con có cùng tên, cùng tham số hoặc chữ ký và cùng kiểu trả về [hoặc kiểu con] như một phương thức trong siêu lớp của nó, thì phương thức trong lớp con được cho là sẽ ghi đè phương thức trong siêu lớp.
Mã nguồn
# Function Overriding class Employee: def WorkingHrs[self]: self.hrs = 50 def printHrs[self]: print["Total Working Hrs : ", self.hrs] class Trainee[Employee]: def WorkingHrs[self]: self.hrs = 60 def resetHrs[self]: super[].WorkingHrs[] employee = Employee[] employee.WorkingHrs[] employee.printHrs[] trainee=Trainee[] trainee.WorkingHrs[] trainee.printHrs[] # Reset Trainee Hrs trainee.resetHrs[] trainee.printHrs[]Để tải xuống tệp thô Bấm vào đây
đầu ra
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 50
Danh sách chương trình
Chương trình mẫu
Kết nối cơ sở dữ liệu Python
bình trăn
Hướng dẫn Python Tkinter
Ghi đè phương thức là một khái niệm của lập trình hướng đối tượng, cho phép chúng ta thay đổi cách thực thi của một hàm trong lớp con đã được định nghĩa trong lớp cha. Đó là khả năng của một lớp con thay đổi việc triển khai bất kỳ phương thức nào đã được cung cấp bởi một trong các lớp cha [tổ tiên] của nó
Các điều kiện sau phải được đáp ứng để ghi đè một chức năng
- Kế thừa nên có. Chức năng ghi đè không thể được thực hiện trong một lớp. Chúng ta cần lấy một lớp con từ một lớp cha
- Hàm được định nghĩa lại trong lớp con phải có cùng chữ ký như trong lớp cha i. e. cùng một số tham số
Như chúng ta đã tìm hiểu về khái niệm Kế thừa, chúng ta biết rằng khi một lớp con kế thừa một lớp cha, nó cũng có quyền truy cập vào các biến và phương thức
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5016 và
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5017[access modifiers in python], ví dụ:
# parent class
class Parent:
# some random function
def anything[self]:
print['Function defined in parent class!']
# child class
class Child[Parent]:
# empty class definition
pass
obj2 = Child[]
obj2.anything[]
Hàm được định nghĩa trong lớp cha
Trong khi lớp con có thể truy cập các phương thức của lớp cha, nó cũng có thể cung cấp một triển khai mới cho các phương thức của lớp cha, được gọi là ghi đè phương thức
Ví dụ ghi đè phương thức Python
Hãy lấy một ví dụ rất thú vị mà chúng ta cũng đã có trong hướng dẫn thừa kế. Có một lớp cha tên là
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5018
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
Hãy tạo một lớp con
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5019 sẽ mở rộng lớp
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5018
class Herbivorous[Animal]:
# function feed
def feed[self]:
print["I eat only plants. I am vegetarian."]
Trong lớp con
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5019, chúng ta đã ghi đè phương thức
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
0Vì vậy, bây giờ khi chúng ta tạo một đối tượng của lớp
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5019 và gọi phương thức
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
0, phiên bản bị ghi đè sẽ được thực thi Bản tóm tắt. trong hướng dẫn này, bạn sẽ học cách sử dụng phương thức ghi đè Python để cho phép lớp con cung cấp triển khai cụ thể của phương thức được cung cấp bởi một trong các lớp cha của nó
Giới thiệu về phương thức ghi đè Python
Phương thức ghi đè cho phép một lớp con cung cấp một triển khai cụ thể của một phương thức đã được cung cấp bởi một trong các lớp cha của nó
Hãy lấy một ví dụ để hiểu rõ hơn về phương thức ghi đè
Đầu tiên, định nghĩa lớp
8Code language: Python [python]
class SalesEmployee[Employee]: def __init__[self, name, base_pay, sales_incentive]: self.name = name self.base_pay = base_pay self.sales_incentive = sales_incentive
Lớp
8 có hai biến đối tượng làCode language: Python [python]
class SalesEmployee[Employee]: def __init__[self, name, base_pay, sales_incentive]: self.name = name self.base_pay = base_pay self.sales_incentive = sales_incentive
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5000 và
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5001. Nó cũng có phương thức
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5002 trả về
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5001
Thứ hai, xác định
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5004 kế thừa từ lớp
8Code language: Python [python]
class SalesEmployee[Employee]: def __init__[self, name, base_pay, sales_incentive]: self.name = name self.base_pay = base_pay self.sales_incentive = sales_incentive
Code language: Python [python]
class SalesEmployee[Employee]: def __init__[self, name, base_pay, sales_incentive]: self.name = name self.base_pay = base_pay self.sales_incentive = sales_incentive
Lớp
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5004 có ba thuộc tính thể hiện.
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5000,
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5001 và
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5009
Thứ ba, tạo một phiên bản mới của lớp
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5004 và hiển thị khoản thanh toán
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 500
đầu ra
# parent class
class Parent:
# some random function
def anything[self]:
print['Function defined in parent class!']
# child class
class Child[Parent]:
# empty class definition
pass
obj2 = Child[]
obj2.anything[]
3Phương thức
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5002 chỉ trả về
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5001, không phải tổng của
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5001 và
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5009
Khi bạn gọi
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5002 từ thể hiện của lớp
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5004, Python thực thi phương thức
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5002 của lớp
8, phương thức này trả vềCode language: Python [python]
class SalesEmployee[Employee]: def __init__[self, name, base_pay, sales_incentive]: self.name = name self.base_pay = base_pay self.sales_incentive = sales_incentive
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5001
Để đưa phần thưởng bán hàng vào lương, bạn cần định nghĩa lại phương thức
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5002 trong lớp
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5004 như sau
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
5Trong trường hợp này, chúng ta nói rằng phương thức
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5002 trong lớp
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5004 ghi đè phương thức
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5002 trong lớp
8Code language: Python [python]
class SalesEmployee[Employee]: def __init__[self, name, base_pay, sales_incentive]: self.name = name self.base_pay = base_pay self.sales_incentive = sales_incentive
Khi bạn gọi phương thức
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5002 của đối tượng
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5004, Python sẽ gọi phương thức
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5002 trong lớp
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5004
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 500
đầu ra
class Herbivorous[Animal]:
# function feed
def feed[self]:
print["I eat only plants. I am vegetarian."]
5Nếu bạn tạo một thể hiện của lớp
8, Python sẽ gọi phương thứcCode language: Python [python]
class SalesEmployee[Employee]: def __init__[self, name, base_pay, sales_incentive]: self.name = name self.base_pay = base_pay self.sales_incentive = sales_incentive
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5002 của lớp
8, không phải phương thứcCode language: Python [python]
class SalesEmployee[Employee]: def __init__[self, name, base_pay, sales_incentive]: self.name = name self.base_pay = base_pay self.sales_incentive = sales_incentive
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5002 của lớp
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5004. Ví dụ
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
1Đặt nó tất cả cùng nhau
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
2Ví dụ ghi đè phương thức nâng cao
Sau đây định nghĩa lớp
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5005
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
4Lớp
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5005 có thuộc tính
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5007 chỉ định một đoạn văn bản sẽ được phân tích cú pháp. Ngoài ra, lớp
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5005 có ba phương thức
- Phương thức
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 50
09 phân tích văn bản và trả về email - Phương thức
50 phân tích văn bản và trả về một số điện thoại ở định dạngclass Herbivorous[Animal]: # function feed def feed[self]: print["I eat only plants. I am vegetarian."]
51 trong đóclass Herbivorous[Animal]: # function feed def feed[self]: print["I eat only plants. I am vegetarian."]
52 là một số từ 0 đến 9 e. g. ,class Herbivorous[Animal]: # function feed def feed[self]: print["I eat only plants. I am vegetarian."]
53class Herbivorous[Animal]: # function feed def feed[self]: print["I eat only plants. I am vegetarian."]
- Phương thức
54 trả về một từ điển chứa hai phần tửclass Herbivorous[Animal]: # function feed def feed[self]: print["I eat only plants. I am vegetarian."]
55 vàclass Herbivorous[Animal]: # function feed def feed[self]: print["I eat only plants. I am vegetarian."]
56. Nó gọi phương thứcclass Herbivorous[Animal]: # function feed def feed[self]: print["I eat only plants. I am vegetarian."]
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 50
09 và
50 để trích xuất email và điện thoại từ thuộc tínhclass Herbivorous[Animal]: # function feed def feed[self]: print["I eat only plants. I am vegetarian."]
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 50
07
Sau đây sử dụng lớp
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5005 để trích xuất email và điện thoại
0Code language: Python [python]
class SalesEmployee[Employee]: def __init__[self, name, base_pay, sales_incentive]: self.name = name self.base_pay = base_pay self.sales_incentive = sales_incentive
đầu ra
1Code language: Python [python]
class SalesEmployee[Employee]: def __init__[self, name, base_pay, sales_incentive]: self.name = name self.base_pay = base_pay self.sales_incentive = sales_incentive
Giả sử bạn cần trích xuất số điện thoại ở định dạng
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
11, đây là định dạng số điện thoại của Vương quốc Anh. Ngoài ra, bạn muốn sử dụng email trích xuất như lớp Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5005
Để làm điều đó, bạn có thể định nghĩa một lớp mới có tên là
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
13 kế thừa từ lớp Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5005. Trong lớp
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
13, bạn override phương thức class Herbivorous[Animal]:
# function feed
def feed[self]:
print["I eat only plants. I am vegetarian."]
50 như sau2Code language: Python [python]
class SalesEmployee[Employee]: def __init__[self, name, base_pay, sales_incentive]: self.name = name self.base_pay = base_pay self.sales_incentive = sales_incentive
Sau đây sử dụng lớp
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
13 để trích xuất số điện thoại [ở định dạng Vương quốc Anh] và email từ văn bản3Code language: Python [python]
class SalesEmployee[Employee]: def __init__[self, name, base_pay, sales_incentive]: self.name = name self.base_pay = base_pay self.sales_incentive = sales_incentive
đầu ra
4Code language: Python [python]
class SalesEmployee[Employee]: def __init__[self, name, base_pay, sales_incentive]: self.name = name self.base_pay = base_pay self.sales_incentive = sales_incentive
Trong ví dụ này,
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
18 gọi phương thức class Herbivorous[Animal]:
# function feed
def feed[self]:
print["I eat only plants. I am vegetarian."]
54 từ lớp cha là lớp Parser. Đổi lại, phương thức class Herbivorous[Animal]:
# function feed
def feed[self]:
print["I eat only plants. I am vegetarian."]
54 gọi các phương thức Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5009 và
class Herbivorous[Animal]:
# function feed
def feed[self]:
print["I eat only plants. I am vegetarian."]
50Tuy nhiên,
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
23 không gọi phương thức class Herbivorous[Animal]:
# function feed
def feed[self]:
print["I eat only plants. I am vegetarian."]
50 của lớp Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5005 mà gọi phương thức
class Herbivorous[Animal]:
# function feed
def feed[self]:
print["I eat only plants. I am vegetarian."]
50 của lớp class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
135Code language: Python [python]
class SalesEmployee[Employee]: def __init__[self, name, base_pay, sales_incentive]: self.name = name self.base_pay = base_pay self.sales_incentive = sales_incentive
Lý do là bên trong phương thức
class Herbivorous[Animal]:
# function feed
def feed[self]:
print["I eat only plants. I am vegetarian."]
54, class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
29 là class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
18 là một thể hiện của lớp class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
13Do đó, khi bạn gọi phương thức
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
42 bên trong phương thức class Herbivorous[Animal]:
# function feed
def feed[self]:
print["I eat only plants. I am vegetarian."]
54, Python sẽ tìm kiếm phương thức class Herbivorous[Animal]:
# function feed
def feed[self]:
print["I eat only plants. I am vegetarian."]
50 được liên kết với thể hiện của class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
13Đặt nó tất cả cùng nhau
6Code language: Python [python]
class SalesEmployee[Employee]: def __init__[self, name, base_pay, sales_incentive]: self.name = name self.base_pay = base_pay self.sales_incentive = sales_incentive
Thuộc tính ghi đè
Phần sau đây cho thấy cách triển khai các lớp Trình phân tích cú pháp và UkParser bằng cách ghi đè các thuộc tính
7Code language: Python [python]
class SalesEmployee[Employee]: def __init__[self, name, base_pay, sales_incentive]: self.name = name self.base_pay = base_pay self.sales_incentive = sales_incentive
Trong ví dụ này,
Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5005 có biến lớp
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
47. Phương thức class Herbivorous[Animal]:
# function feed
def feed[self]:
print["I eat only plants. I am vegetarian."]
50 trong lớp Total Working Hrs : 50 Total Working Hrs : 60 Total Working Hrs : 5005 sử dụng
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
47 để trích xuất một số điện thoạiLớp con
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
13 định nghĩa lại [hoặc ghi đè] thuộc tính lớp class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
47Nếu bạn gọi phương thức
class Herbivorous[Animal]:
# function feed
def feed[self]:
print["I eat only plants. I am vegetarian."]
54 từ thể hiện của class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
13, thì phương thức class Herbivorous[Animal]:
# function feed
def feed[self]:
print["I eat only plants. I am vegetarian."]
54 sẽ gọi phương thức class Herbivorous[Animal]:
# function feed
def feed[self]:
print["I eat only plants. I am vegetarian."]
50 sử dụng class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
47 được định nghĩa trong lớp class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breathe
def breathe[self]:
print["I breathe oxygen."]
# function feed
def feed[self]:
print["I eat food."]
13