Hướng dẫn which type of inheritance is supported by python - kiểu thừa kế nào được hỗ trợ bởi 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.

Đó không phải là một loại thừa kế trong Python?

Phân phối không phải là một loại kế thừa.Một lớp mới, được gọi là lớp dẫn xuất, được tạo ra thông qua kế thừa từ một lớp hiện có, được gọi là lớp cơ sở. is not a type of inheritance. A new class, referred to as the Derived Class, is created through inheritance from an existing class, referred to as the Base Class.

Kế thừa cho Python là gì?

Kế thừa trong Python, nó đề cập đến việc xác định một lớp mới với rất ít hoặc không có sửa đổi cho một lớp hiện có.Lớp mới được gọi là lớp dẫn xuất [hoặc trẻ em] và lớp mà nó thừa hưởng được gọi là lớp cơ sở [hoặc cha mẹ].defining a new class with little or no modification to an existing class. The new class is called derived [or child] class and the one from which it inherits is called the base [or parent] class.

Python có hỗ trợ tất cả sự kế thừa không?

Kế thừa là một tính năng cần thiết của mọi ngôn ngữ lập trình hướng đối tượng.Điều này có nghĩa là Python hỗ trợ thừa kế và như bạn sẽ thấy sau này, đó là một trong số ít ngôn ngữ hỗ trợ nhiều kế thừa.Python supports inheritance, and as you'll see later, it's one of the few languages that supports multiple inheritance.

Python có hỗ trợ kế thừa lai không?

Bây giờ, chúng tôi xác định lớp trẻ có tên là Sinh viên, của khóa học và Chi nhánh, để thực hiện kế thừa này của loại lai.Theo cách tương tự, chúng ta có thể xác định lớp và gọi các thành viên khác nhau của các lớp cha.we define the Child Class named “Student”, of classes Course and Branch, to make this Inheritance of type Hybrid. In a similar way, we can define the class and call various members of the parent classes.

Bài Viết Liên Quan

Chủ Đề