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 : SITA0Father : RAM Mother : SITA1
Father : RAM Mother : SITA2Father : RAM Mother : SITA3Father : RAM Mother : SITA4Father : RAM Mother : SITA5Father : RAM Mother : SITA6
class Father : RAM Mother : SITA8
def Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince1Father : RAM Mother : SITA0Father : RAM Mother : SITA1
Father : RAM Mother : SITA2Father : RAM Mother : SITA3Father : RAM Mother : SITA4Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince7Father : 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 : Prince9This 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 : Prince9This 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.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.4Father : RAM Mother : SITA0Father : RAM Mother : SITA1
Father : RAM Mother : SITA2Father : RAM Mother : SITA3Father : RAM Mother : SITA4Father : RAM Mother : SITA0class1
class class3
class5This 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:0Father : RAM Mother : SITA0Father : RAM Mother : SITA1
Father : RAM Mother : SITA2Father : RAM Mother : SITA3Father : RAM Mother : SITA4Father : RAM Mother : SITA0Parent:7
class Parent:9
def 2Father : RAM Mother : SITA0Father : RAM Mother : SITA1
Father : RAM Mother : SITA2Father : RAM Mother : SITA3Father : RAM Mother : SITA4 8 9Father : RAM Mother : SITA0Parent:7
Father : RAM Mother : SITA2Father : RAM Mother : SITA3Father : RAM Mother : SITA4def5 9Father : RAM Mother : SITA0class1
def9This 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(1
func1(2This 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(4
func1(5This 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(7
func1(8
Output:
Father : RAM Mother : SITAThừ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 : SITA03Father : RAM Mother : SITA0Father : RAM Mother : SITA05
Father : RAM Mother : SITA2Father : RAM Mother : SITA010108This 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 : SITA03Father : RAM Mother : SITA0Father : RAM Mother : SITA17
Father : RAM Mother : SITA2Father : RAM Mother : SITA0Father : RAM Mother : SITA20This 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 : SITA2Father : RAM Mother : SITA24Father : RAM Mother : SITA0Father : RAM Mother : SITA26
class Father : RAM Mother : SITA28
def Father : RAM Mother : SITA03Father : RAM Mother : SITA0Father : RAM Mother : SITA33
Father : RAM Mother : SITA2Father : RAM Mother : SITA0Father : RAM Mother : SITA36This 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 : SITA2Father : RAM Mother : SITA40Father : RAM Mother : SITA0Father : RAM Mother : SITA42
def Father : RAM Mother : SITA45Father : RAM Mother : SITA0Father : RAM Mother : SITA1
Father : RAM Mother : SITA2Father : RAM Mother : SITA3Father : RAM Mother : SITA4Father : RAM Mother : SITA51 9Father : RAM Mother : SITA0Father : RAM Mother : SITA54
Father : RAM Mother : SITA2Father : RAM Mother : SITA3Father : RAM Mother : SITA4Father : RAM Mother : SITA58 9Father : RAM Mother : SITA0Parent:7
Father : RAM Mother : SITA2Father : RAM Mother : SITA3Father : RAM Mother : SITA4Father : RAM Mother : SITA65 9Father : RAM Mother : SITA0Father : RAM Mother : SITA68
def9This 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 : SITA71Father : RAM Mother : SITA72 9Father : RAM Mother : SITA74 9Father : RAM Mother : SITA76Father : RAM Mother : SITA6
Father : RAM Mother : SITA3Father : RAM Mother : SITA79
Father : RAM Mother : SITA80
Output:
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : PrinceDi 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 : SITA0Father : RAM Mother : SITA1
class Father : RAM Mother : SITA8
def Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince1Father : RAM Mother : SITA0Father : RAM Mother : SITA1
def Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince1Father : RAM Mother : SITA0Father : RAM Mother : SITA1
Father : RAM Mother : SITA2Father : RAM Mother : SITA3Father : RAM Mother : SITA4Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince03Father : 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 : SITA2Father : RAM Mother : SITA3Father : RAM Mother : SITA4Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince15Father : 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.4Father : RAM Mother : SITA0Father : RAM Mother : SITA1
Example:
Python3
class class3
def func1(Father : RAM Mother : SITA0Father : RAM Mother : SITA1
Father : RAM Mother : SITA2Father : RAM Mother : SITA3Father : RAM Mother : SITA4Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince37Father : RAM Mother : SITA6
class Father : RAM Mother : SITA8
def Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince1Father : RAM Mother : SITA0Father : RAM Mother : SITA1
Father : RAM Mother : SITA2Father : RAM Mother : SITA3Father : RAM Mother : SITA4Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince49Father : 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 : SITA2Father : RAM Mother : SITA3Father : RAM Mother : SITA4Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince61Father : 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 : SITA2Father : RAM Mother : SITA3Father : RAM Mother : SITA4Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince73Father : 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.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
Lal mani Grandfather name : Lal mani Father name : Rampal Son name : Prince9This 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 : Prince9This 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.