Hướng dẫn which of following inheritance is supported in python? - thừa kế nào sau đây được hỗ trợ trong python?

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 : SITA
0
Father : RAM
Mother : SITA
1

Father : RAM
Mother : SITA
2
Father : RAM
Mother : SITA
3
Father : RAM
Mother : SITA
4
Father : RAM
Mother : SITA
5
Father : RAM
Mother : SITA
6

class

Father : RAM
Mother : SITA
8

    def

Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
1
Father : RAM
Mother : SITA
0
Father : RAM
Mother : SITA
1

Father : RAM
Mother : SITA
2
Father : RAM
Mother : SITA
3
Father : RAM
Mother : SITA
4
Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
7
Father : RAM
Mother : SITA
6

Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
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 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 : Prince
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.
3

Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
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.
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 : SITA
0
Father : RAM
Mother : SITA
1

Father : RAM
Mother : SITA
2
Father : RAM
Mother : SITA
3
Father : RAM
Mother : SITA
4
Father : RAM
Mother : SITA
0class1

class class3

    class5

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 : SITA
0
Father : RAM
Mother : SITA
1

Father : RAM
Mother : SITA
2
Father : RAM
Mother : SITA
3
Father : RAM
Mother : SITA
4
Father : RAM
Mother : SITA
0Parent:7

class Parent:9

    def     2

Father : RAM
Mother : SITA
0
Father : RAM
Mother : SITA
1

Father : RAM
Mother : SITA
2
Father : RAM
Mother : SITA
3
Father : RAM
Mother : SITA
4    8    9
Father : RAM
Mother : SITA
0Parent:7

Father : RAM
Mother : SITA
2
Father : RAM
Mother : SITA
3
Father : RAM
Mother : SITA
4def5    9
Father : RAM
Mother : SITA
0class1

def9

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[1

func1[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[4

func1[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[7

func1[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 : SITA
00

    def

Father : RAM
Mother : SITA
03
Father : RAM
Mother : SITA
0
Father : RAM
Mother : SITA
05

Father : RAM
Mother : SITA
2
Father : RAM
Mother : SITA
010108
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 : SITA
10

class

Father : RAM
Mother : SITA
12

    def

Father : RAM
Mother : SITA
03
Father : RAM
Mother : SITA
0
Father : RAM
Mother : SITA
17

Father : RAM
Mother : SITA
2
Father : RAM
Mother : SITA
0
Father : RAM
Mother : SITA
20
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 : SITA
22

Father : RAM
Mother : SITA
2
Father : RAM
Mother : SITA
24
Father : RAM
Mother : SITA
0
Father : RAM
Mother : SITA
26

class

Father : RAM
Mother : SITA
28

    def

Father : RAM
Mother : SITA
03
Father : RAM
Mother : SITA
0
Father : RAM
Mother : SITA
33

Father : RAM
Mother : SITA
2
Father : RAM
Mother : SITA
0
Father : RAM
Mother : SITA
36
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 : SITA
38

Father : RAM
Mother : SITA
2
Father : RAM
Mother : SITA
40
Father : RAM
Mother : SITA
0
Father : RAM
Mother : SITA
42

    def

Father : RAM
Mother : SITA
45
Father : RAM
Mother : SITA
0
Father : RAM
Mother : SITA
1

Father : RAM
Mother : SITA
2
Father : RAM
Mother : SITA
3
Father : RAM
Mother : SITA
4
Father : RAM
Mother : SITA
51    9
Father : RAM
Mother : SITA
0
Father : RAM
Mother : SITA
54

Father : RAM
Mother : SITA
2
Father : RAM
Mother : SITA
3
Father : RAM
Mother : SITA
4
Father : RAM
Mother : SITA
58    9
Father : RAM
Mother : SITA
0Parent:7

Father : RAM
Mother : SITA
2
Father : RAM
Mother : SITA
3
Father : RAM
Mother : SITA
4
Father : RAM
Mother : SITA
65    9
Father : RAM
Mother : SITA
0
Father : RAM
Mother : SITA
68

def9

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 : SITA
71
Father : RAM
Mother : SITA
72    9
Father : RAM
Mother : SITA
74    9
Father : RAM
Mother : SITA
76
Father : RAM
Mother : SITA
6

Father : RAM
Mother : SITA
3
Father : RAM
Mother : SITA
79

Father : RAM
Mother : SITA
80

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 : SITA
0
Father : RAM
Mother : SITA
1

class

Father : RAM
Mother : SITA
8

    def

Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
1
Father : RAM
Mother : SITA
0
Father : RAM
Mother : SITA
1

    def

Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
1
Father : RAM
Mother : SITA
0
Father : RAM
Mother : SITA
1

Father : RAM
Mother : SITA
2
Father : RAM
Mother : SITA
3
Father : RAM
Mother : SITA
4
Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
03
Father : RAM
Mother : SITA
6

Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
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 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 : SITA
2
Father : RAM
Mother : SITA
3
Father : RAM
Mother : SITA
4
Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
15
Father : RAM
Mother : SITA
6

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 : Prince
23

Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
24

Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
25

Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
26

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.
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 : SITA
0
Father : RAM
Mother : SITA
1

Example:

Python3

class class3

    def func1[

Father : RAM
Mother : SITA
0
Father : RAM
Mother : SITA
1

Father : RAM
Mother : SITA
2
Father : RAM
Mother : SITA
3
Father : RAM
Mother : SITA
4
Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
37
Father : RAM
Mother : SITA
6

class

Father : RAM
Mother : SITA
8

    def

Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
1
Father : RAM
Mother : SITA
0
Father : RAM
Mother : SITA
1

Father : RAM
Mother : SITA
2
Father : RAM
Mother : SITA
3
Father : RAM
Mother : SITA
4
Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
49
Father : RAM
Mother : SITA
6

Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
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 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 : SITA
2
Father : RAM
Mother : SITA
3
Father : RAM
Mother : SITA
4
Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
61
Father : RAM
Mother : SITA
6

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 : SITA
2
Father : RAM
Mother : SITA
3
Father : RAM
Mother : SITA
4
Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
73
Father : RAM
Mother : SITA
6

    

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 : Prince
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.
3

Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
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.
5

Output:

This function is in school.
This function is in student 1.

Việc thừa kế nào được hỗ trợ trong Python?

Trả lời: Không giống như các ngôn ngữ lập trình hướng đối tượng khác như Java, Python hỗ trợ tất cả các loại kế thừa, thậm chí nhiều kế thừa!all types of inheritance, even multiple inheritance!

Là thừa kế đa cấp được hỗ trợ trong Python?

Kế thừa đa cấp Python Chúng ta cũng có thể kế thừa từ một lớp dẫn xuất.Điều này được gọi là kế thừa đa cấp.Nó có thể có bất kỳ độ sâu nào trong Python.We can also inherit from a derived class. This is called multilevel inheritance. It can be of any depth in Python.

Kế thừa với ví dụ trong Python là gì?

Mối quan hệ kế thừa định nghĩa các lớp kế thừa từ các lớp khác là các lớp có nguồn gốc, lớp con hoặc loại phụ.Lớp cơ sở vẫn là nguồn mà một lớp con được thừa hưởng.Ví dụ, bạn có một lớp cơ sở của động vật, và một con sư tử là một lớp có nguồn gốc.Việc thừa kế sẽ là sư tử là một động vật.defines the classes that inherit from other classes as derived, subclass, or sub-type classes. Base class remains to be the source from which a subclass inherits. For example, you have a Base class of “Animal,” and a “Lion” is a Derived class. The inheritance will be Lion is an Animal.

Python có hỗ trợ thừa kế duy nhất không?

Trong kế thừa đơn Python, một lớp dẫn xuất chỉ có nguồn gốc từ một lớp cha mẹ và cho phép lớp lấy hành vi và thuộc tính từ một lớp cơ sở duy nhất.Điều này cho phép tái sử dụng mã của lớp cha và thêm các tính năng mới vào lớp làm cho mã dễ đọc hơn, thanh lịch và ít dự phòng hơn.

Bài Viết Liên Quan

Chủ Đề