Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc Inheritance in Python
Bàn luậnoverride the method in the super-class.
Điều kiện tiên quyết: Di truyền trong Python
Example:
Phương pháp ghi đè là 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 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 siêu hoặc lớp của nó. Khi một phương thức trong lớp con có cùng tên, cùng một tham số hoặc chữ ký và cùng loại trả về [hoặc loại con] làm phương thức trong siêu lớp của nó, thì phương thức trong lớp con được cho là ghi đè phương thức trong siêu lớp.
Phiên bản của một phương thức được thực thi sẽ được xác định bởi đối tượng được sử dụng để gọi nó. Nếu một đối tượng của lớp cha được sử dụng để gọi phương thức, thì phiên bản trong lớp cha sẽ được thực thi, nhưng nếu một đối tượng của lớp con được sử dụng để gọi phương thức, thì phiên bản trong lớp con sẽ được thực thi. Nói cách khác, đó là loại đối tượng được đề cập [không phải là loại của biến tham chiếu] xác định phiên bản nào của phương thức ghi đè sẽ được thực thi.
class
Parent[]:
def
Inside Child Inside Parent20
Inside Child Inside Parent21
Inside Child Inside Parent22
Inside Child Inside Parent23
Inside GrandChild Inside Parent4
Inside GrandChild Inside Parent5
Inside Child Inside Parent21
Inside GrandChild Inside Parent7
Inside Child Inside Parent23
Inside Child Inside Parent21
Inside Child Inside Parent25
Inside Child Inside Parent26
Inside Child Inside Parent27
Phiên bản của một phương thức được thực thi sẽ được xác định bởi đối tượng được sử dụng để gọi nó. Nếu một đối tượng của lớp cha được sử dụng để gọi phương thức, thì phiên bản trong lớp cha sẽ được thực thi, nhưng nếu một đối tượng của lớp con được sử dụng để gọi phương thức, thì phiên bản trong lớp con sẽ được thực thi. Nói cách khác, đó là loại đối tượng được đề cập [không phải là loại của biến tham chiếu] xác định phiên bản nào của phương thức ghi đè sẽ được thực thi.
class
Parent[]:
def
Inside Child Inside Parent20
Inside Child Inside Parent21
Inside Child Inside Parent22
Inside Child Inside Parent23
Inside GrandChild Inside Parent4
Inside GrandChild Inside Parent5
Inside Child Inside Parent21
Inside GrandChild Inside Parent7
Inside Child Inside Parent23
Inside Child Inside Parent21
Inside Child Inside Parent25
Inside Child Inside Parent26
Inside Child Inside Parent27
def
Inside GrandChild Inside Parent0
Inside Child Inside Parent21
Inside Child Inside Parent22
HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 126
HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 127
Output:
Inside Parent Inside Child
class
Inside GrandChild
Inside Parent
9
Inside Child Inside Parent2
3Inside Child Inside Parent2
1Inside Child Inside Parent2
5Inside Child Inside Parent2
6Inside Parent Inside Child
9 When a class is derived from more than one base class it is called multiple Inheritance.HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 12
0Inside Child Inside Parent2
6HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 12
2 Let’s consider an example where we want to override a method of one parent class only. Below is the implementation.HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 12
3Inside Child Inside Parent2
6HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 12
5def
Inside Child Inside Parent2
0Inside Child Inside Parent2
1Inside Child Inside Parent2
2Inside Child Inside Parent2
3Inside GrandChild Inside Parent
4Inside GrandChild Inside Parent
5class
8class
9Inside Child Inside Parent2
3Inside Child Inside Parent2
1Inside Child Inside Parent2
5Inside Child Inside Parent2
6Inside Child Inside Parent2
7def
Inside GrandChild Inside Parent
0Inside Child Inside Parent2
1Inside Child Inside Parent2
2Inside Child Inside Parent2
3Inside GrandChild Inside Parent
4Inside GrandChild Inside Parent
5class
9class
Inside GrandChild Inside Parent
9def
Inside Child Inside Parent2
0Inside Child Inside Parent2
1Inside Child Inside Parent2
2Inside Child Inside Parent2
3Inside GrandChild Inside Parent
4Inside GrandChild Inside Parent
5Inside Parent Inside Child
9class
9Inside Child Inside Parent2
3Inside Child Inside Parent2
1Inside Child Inside Parent2
5Inside Child Inside Parent2
6Inside Child Inside Parent2
7def
7def
8Output:
Inside Child Inside Parent2
def
Inside GrandChild Inside Parent
0Inside Child Inside Parent2
1Inside Child Inside Parent2
2 When we have a child and grandchild relationship.class
Inside GrandChild Inside Parent
9 Let’s consider an example where we want to override only one method of one of its parent classes. Below is the implementation.Inside Child Inside Parent2
3Inside Child Inside Parent2
1Inside Child Inside Parent2
5Inside Child Inside Parent2
6Inside Parent Inside Child
9def
Inside GrandChild Inside Parent
0Inside Child Inside Parent2
1Inside Child Inside Parent2
2Inside Child Inside Parent2
3Inside GrandChild Inside Parent
4Inside GrandChild Inside Parent
5Inside Child Inside Parent2
7class
9class
Inside GrandChild Inside Parent
9def
Inside Child Inside Parent2
0Inside Child Inside Parent2
1Inside Child Inside Parent2
2Inside Child Inside Parent2
3Inside GrandChild Inside Parent
4Inside GrandChild Inside Parent
5Inside Parent Inside Child
9class
9Inside Child Inside Parent2
3Inside Child Inside Parent2
1Inside Child Inside Parent2
5Inside Child Inside Parent2
6Inside Child Inside Parent2
7def
Inside Child Inside Parent2
0Inside Child Inside Parent2
1Inside Child Inside Parent2
2Inside Child Inside Parent2
3Inside GrandChild Inside Parent
4Inside GrandChild Inside Parent
5Inside Child Inside Parent2
33Inside Child Inside Parent2
34Inside Child Inside Parent2
3Inside Child Inside Parent2
1Inside Child Inside Parent2
5Inside Child Inside Parent2
6Inside Child Inside Parent2
7Inside Child Inside Parent2
38Inside Child Inside Parent2
39Output:
Inside GrandChild Inside Parent
def
Inside GrandChild
Inside Parent
0Inside Child
Inside Parent2
1Inside Child
Inside Parent2
2
class
Inside GrandChild Inside Parent9
Inside Child Inside Parent2
3Inside Child Inside Parent2
1Inside Child Inside Parent2
5Inside Child Inside Parent2
6Inside Parent Inside Child
9 Parent’s class methods can be called by using the ParentInside Child Inside Parent2
40 inside the overridden method.Example:
Phương pháp ghi đè là 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 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 siêu hoặc lớp của nó. Khi một phương thức trong lớp con có cùng tên, cùng một tham số hoặc chữ ký và cùng loại trả về [hoặc loại con] làm phương thức trong siêu lớp của nó, thì phương thức trong lớp con được cho là ghi đè phương thức trong siêu lớp.
def
Inside Child Inside Parent2
0Inside Child Inside Parent2
1Inside Child Inside Parent2
2Inside Child Inside Parent2
3Inside GrandChild Inside Parent
4Inside GrandChild Inside Parent
5Inside Child Inside Parent2
7class
9Inside Child Inside Parent2
3Inside Child Inside Parent2
1Inside Child Inside Parent2
5Inside Child Inside Parent2
6Inside Child Inside Parent2
7def
Inside Child Inside Parent2
0Inside Child Inside Parent2
1Inside Child Inside Parent2
2Inside Child Inside Parent2
3Inside Child Inside Parent2
61Inside Child Inside Parent2
1class
9Inside Child Inside Parent2
3Inside GrandChild Inside Parent
4Inside GrandChild Inside Parent
5Inside Parent Inside Child
9class
9Inside Child Inside Parent2
3Inside Child Inside Parent2
1Inside Child Inside Parent2
5Inside Child Inside Parent2
6Inside Child Inside Parent2
7def
7Output:
Inside Parent Inside Child
def
Inside GrandChild Inside Parent
0Inside Child Inside Parent2
1Inside Child Inside Parent2
2 PythonInside Child Inside Parent2
73 function provides us the facility to refer to the parent class explicitly. It is basically useful where we have to call superclass functions. It returns the proxy object that allows us to refer parent class by ‘super’.class
Inside GrandChild Inside Parent
9Phương pháp ghi đè là 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 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 siêu hoặc lớp của nó. Khi một phương thức trong lớp con có cùng tên, cùng một tham số hoặc chữ ký và cùng loại trả về [hoặc loại con] làm phương thức trong siêu lớp của nó, thì phương thức trong lớp con được cho là ghi đè phương thức trong siêu lớp.
def
Inside Child Inside Parent2
0Inside Child Inside Parent2
1Inside Child Inside Parent2
2Inside Child Inside Parent2
3Inside GrandChild Inside Parent
4Inside GrandChild Inside Parent
5Inside Child Inside Parent2
7class
9Inside Child Inside Parent2
3Inside Child Inside Parent2
1Inside Child Inside Parent2
5Inside Child Inside Parent2
6Inside Child Inside Parent2
7def
Inside Child Inside Parent2
0Inside Child Inside Parent2
1Inside Child Inside Parent2
2Inside Child Inside Parent2
3Inside Child Inside Parent2
94Inside Child Inside Parent2
95Inside Child Inside Parent2
3Inside GrandChild Inside Parent
4Inside GrandChild Inside Parent
5Inside Parent Inside Child
9class
9Inside Child Inside Parent2
3Inside Child Inside Parent2
1Inside Child Inside Parent2
5Inside Child Inside Parent2
6Inside Child Inside Parent2
7def
7Output:
Inside Parent Inside Child
def
Inside GrandChild Inside Parent
0Inside Child Inside Parent2
1Inside Child Inside Parent2
2class
Inside GrandChild Inside Parent
9Inside Child Inside Parent2
3Inside Child Inside Parent2
1Inside Child Inside Parent2
5Inside Child Inside Parent2
6Inside Parent Inside Child
9Inside Child Inside Parent2
3Inside GrandChild Inside Parent
4Inside GrandChild Inside Parent
5Inside GrandChild Inside Parent
15Inside GrandChild Inside Parent
16HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 12
0Inside Child Inside Parent2
6HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 12
2Inside Child Inside Parent2
3Inside GrandChild Inside Parent
4Inside GrandChild Inside Parent
5Inside GrandChild Inside Parent
25Inside GrandChild Inside Parent
26HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 12
3Inside Child Inside Parent2
6HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 12
5Inside Child Inside Parent2
3Inside Child Inside Parent2
1Inside Child Inside Parent2
5Inside Child Inside Parent2
6Inside Parent Inside Child
9Inside Child Inside Parent2
3Inside GrandChild Inside Parent
4Inside GrandChild Inside Parent
5Inside GrandChild Inside Parent
37Inside GrandChild Inside Parent
16Inside Child Inside Parent2
3Inside Child Inside Parent2
94Inside GrandChild Inside Parent
41def
Inside GrandChild Inside Parent
19Inside Child Inside Parent2
1Inside GrandChild Inside Parent
21Inside Child Inside Parent2
3Inside GrandChild Inside Parent
4Inside GrandChild Inside Parent
5Inside GrandChild Inside Parent
50Inside GrandChild Inside Parent
26Inside Child Inside Parent2
3Inside Child Inside Parent2
94Inside GrandChild Inside Parent
54Inside GrandChild Inside Parent
55Inside GrandChild Inside Parent
56Inside GrandChild Inside Parent
16class
Inside GrandChild Inside Parent
59def
Inside Child Inside Parent2
0Inside Child Inside Parent2
1Inside GrandChild Inside Parent
11Inside Child Inside Parent2
3Inside GrandChild Inside Parent
4Inside GrandChild Inside Parent
5Inside GrandChild Inside Parent
68Inside GrandChild Inside Parent
16Inside Child Inside Parent2
3Inside Child Inside Parent2
94Inside GrandChild Inside Parent
41def
Inside GrandChild Inside Parent
19Inside Child Inside Parent2
1Inside GrandChild Inside Parent
21Inside Child Inside Parent2
3Inside GrandChild Inside Parent
4Inside GrandChild Inside Parent
5Inside GrandChild Inside Parent
81Inside GrandChild Inside Parent
26Inside Child Inside Parent2
3Inside Child Inside Parent2
94Inside GrandChild Inside Parent
54Inside GrandChild Inside Parent
55Inside GrandChild Inside Parent
56Inside GrandChild Inside Parent
16class
Inside GrandChild Inside Parent
59def
Inside Child Inside Parent2
0Inside Child Inside Parent2
1Inside GrandChild Inside Parent
11Inside Parent Inside Child
00Inside Parent Inside Child
01class
9Output:
HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 12