Kế thừa được định nghĩa là cơ chế kế thừa các thuộc tính của lớp cơ sở cho lớp con. Ở đây chúng ta sẽ thấy các loại hình ảnh trong Python.inheritance in Python.
Các loại kế thừa trong Python
Các loại kế thừa phụ thuộc vào số lượng các lớp con và phụ huynh có liên quan. Có bốn loại kế thừa trong Python:
Di truyền duy nhất: & nbsp;
Kế thừa duy nhất cho phép một lớp dẫn xuất kế thừa các thuộc tính từ một lớp cha, do đó cho phép tái sử dụng mã và bổ sung các tính năng mới vào mã hiện có.
Example:
Python3
class
Parent:
def
func1[
Father : RAM Mother : SITA0
Father : RAM Mother : SITA1
Father : RAM Mother : SITA2
Father : RAM Mother : SITA3
Father : RAM Mother : SITA4
Father : RAM Mother : SITA5
Father : RAM Mother : SITA6
class
Father : RAM Mother : SITA8
def
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince1
Father : RAM Mother : SITA0
Father : RAM Mother : SITA1
Father : RAM Mother : SITA2
Father : RAM Mother : SITA3
Father : RAM Mother : SITA4
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince7
Father : RAM Mother : SITA6
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince9
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.0
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.1
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince9
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.3
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince9
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.5
Output:
This function is in parent class. This function is in child class.
Nhiều kế thừa: & nbsp;
Khi một lớp có thể được lấy từ nhiều lớp cơ sở, loại thừa kế này được gọi là nhiều kế thừa. Trong nhiều kế thừa, tất cả các tính năng của các lớp cơ sở được kế thừa vào lớp dẫn xuất. & NBSP;
Example:
Python3
class
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.7
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.9
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.0
This function is in school. This function is in student 1.1
def
This function is in school. This function is in student 1.4
Father : RAM Mother : SITA0
Father : RAM Mother : SITA1
Father : RAM Mother : SITA2
Father : RAM Mother : SITA3
Father : RAM Mother : SITA4
Father : RAM Mother : SITA0
class
1class
class
3
class
5
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.0
This function is in school. This function is in student 1.1
def
Parent:
0
Father : RAM Mother : SITA0
Father : RAM Mother : SITA1
Father : RAM Mother : SITA2
Father : RAM Mother : SITA3
Father : RAM Mother : SITA4
Father : RAM Mother : SITA0
Parent:
7class
Parent:
9
def
2
Father : RAM Mother : SITA0
Father : RAM Mother : SITA1
Father : RAM Mother : SITA2
Father : RAM Mother : SITA3
Father : RAM Mother : SITA4
8
9Father : RAM Mother : SITA0
Parent:
7Father : RAM Mother : SITA2
Father : RAM Mother : SITA3
Father : RAM Mother : SITA4
def
5
9Father : RAM Mother : SITA0
class
1def
9
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.0
func1[
1func1[
2
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.0
func1[
4func1[
5
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.0
func1[
7func1[
8
Output:
Father : RAM Mother : SITA
Thừa kế đa cấp & nbsp ;: :
Trong kế thừa đa cấp, các tính năng của lớp cơ sở và lớp dẫn xuất được tiếp tục di truyền vào lớp dẫn xuất mới. Điều này tương tự như một mối quan hệ đại diện cho một đứa trẻ và một ông nội. & Nbsp;
Example:
Python3
class
Father : RAM Mother : SITA00
def
Father : RAM Mother : SITA03
Father : RAM Mother : SITA0
Father : RAM Mother : SITA05
Father : RAM Mother : SITA2
Father : RAM Mother : SITA010108
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.0
Father : RAM Mother : SITA10
class
Father : RAM Mother : SITA12
def
Father : RAM Mother : SITA03
Father : RAM Mother : SITA0
Father : RAM Mother : SITA17
Father : RAM Mother : SITA2
Father : RAM Mother : SITA0
Father : RAM Mother : SITA20
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.0
Father : RAM Mother : SITA22
Father : RAM Mother : SITA2
Father : RAM Mother : SITA24
Father : RAM Mother : SITA0
Father : RAM Mother : SITA26
class
Father : RAM Mother : SITA28
def
Father : RAM Mother : SITA03
Father : RAM Mother : SITA0
Father : RAM Mother : SITA33
Father : RAM Mother : SITA2
Father : RAM Mother : SITA0
Father : RAM Mother : SITA36
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.0
Father : RAM Mother : SITA38
Father : RAM Mother : SITA2
Father : RAM Mother : SITA40
Father : RAM Mother : SITA0
Father : RAM Mother : SITA42
def
Father : RAM Mother : SITA45
Father : RAM Mother : SITA0
Father : RAM Mother : SITA1
Father : RAM Mother : SITA2
Father : RAM Mother : SITA3
Father : RAM Mother : SITA4
Father : RAM Mother : SITA51
9Father : RAM Mother : SITA0
Father : RAM Mother : SITA54
Father : RAM Mother : SITA2
Father : RAM Mother : SITA3
Father : RAM Mother : SITA4
Father : RAM Mother : SITA58
9Father : RAM Mother : SITA0
Parent:
7Father : RAM Mother : SITA2
Father : RAM Mother : SITA3
Father : RAM Mother : SITA4
Father : RAM Mother : SITA65
9Father : RAM Mother : SITA0
Father : RAM Mother : SITA68
def
9
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.0
Father : RAM Mother : SITA71
Father : RAM Mother : SITA72
9Father : RAM Mother : SITA74
9Father : RAM Mother : SITA76
Father : RAM Mother : SITA6
Father : RAM Mother : SITA3
Father : RAM Mother : SITA79
Father : RAM Mother : SITA80
Output:
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince
Di truyền phân cấp: & nbsp;
Khi nhiều hơn một lớp dẫn xuất được tạo ra từ một cơ sở duy nhất, loại thừa kế này được gọi là kế thừa phân cấp. Trong chương trình này, chúng tôi có một lớp cha mẹ [cơ sở] và hai lớp [có nguồn gốc].
Example:
Python3
class
Parent:
def
func1[
Father : RAM Mother : SITA0
Father : RAM Mother : SITA1
class
Father : RAM Mother : SITA8
def
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince1
Father : RAM Mother : SITA0
Father : RAM Mother : SITA1
def
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince1
Father : RAM Mother : SITA0
Father : RAM Mother : SITA1
Father : RAM Mother : SITA2
Father : RAM Mother : SITA3
Father : RAM Mother : SITA4
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince03
Father : RAM Mother : SITA6
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince9
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.0
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.1
Nhiều kế thừa: & nbsp;
Father : RAM Mother : SITA2
Father : RAM Mother : SITA3
Father : RAM Mother : SITA4
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince15
Father : RAM Mother : SITA6
Khi một lớp có thể được lấy từ nhiều lớp cơ sở, loại thừa kế này được gọi là nhiều kế thừa. Trong nhiều kế thừa, tất cả các tính năng của các lớp cơ sở được kế thừa vào lớp dẫn xuất. & NBSP;
class
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.7
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince23
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince24
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince25
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince26
Output:
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.
This function is in parent class.
This function is in child 1.
This function is in parent class.
This function is in child 2.
9This function is in parent class.
This function is in child 1.
This function is in parent class.
This function is in child 2.
0 This function is in school.
This function is in student 1.
1
def
This function is in school. This function is in student 1.4
Father : RAM Mother : SITA0
Father : RAM Mother : SITA1
Example:
Python3
class
class
3
def
func1[
Father : RAM Mother : SITA0
Father : RAM Mother : SITA1
Father : RAM Mother : SITA2
Father : RAM Mother : SITA3
Father : RAM Mother : SITA4
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince37
Father : RAM Mother : SITA6
class
Father : RAM Mother : SITA8
def
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince1
Father : RAM Mother : SITA0
Father : RAM Mother : SITA1
Father : RAM Mother : SITA2
Father : RAM Mother : SITA3
Father : RAM Mother : SITA4
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince49
Father : RAM Mother : SITA6
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince9
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.0
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.1
Nhiều kế thừa: & nbsp;
Father : RAM Mother : SITA2
Father : RAM Mother : SITA3
Father : RAM Mother : SITA4
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince61
Father : RAM Mother : SITA6
Khi một lớp có thể được lấy từ nhiều lớp cơ sở, loại thừa kế này được gọi là nhiều kế thừa. Trong nhiều kế thừa, tất cả các tính năng của các lớp cơ sở được kế thừa vào lớp dẫn xuất. & NBSP;
class
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.7
Father : RAM Mother : SITA2
Father : RAM Mother : SITA3
Father : RAM Mother : SITA4
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince73
Father : RAM Mother : SITA6
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.9
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.0
This function is in school. This function is in student 1.1
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince9
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.3
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince9
This function is in parent class. This function is in child 1. This function is in parent class. This function is in child 2.5
Output:
This function is in school. This function is in student 1.