Hướng dẫn what is hybrid inheritance in oops python? - kế thừa lai trong oops python là gì?

Kế thừa

  • Kế thừa là quá trình tạo một lớp mới từ một lớp hiện có. is the process of creating a new class from an existing class.

  • Lớp được kế thừa được gọi là lớp Super/Parent/Base và lớp kế thừa được gọi là lớp phụ/trẻ em/có nguồn gốc.

  • Một lớp dẫn xuất có thể truy cập các thuộc tính của lớp cơ sở.

  • Kế thừa cung cấp khả năng mở rộng và khả năng tái sử dụng.

Các loại kế thừa

  1. Thừa kế duy nhất
  2. Thừa kế đa cấp
  3. Nhiều kế thừa
  4. Di truyền phân cấp
  5. Di truyền lai

Di truyền lai

Các tính năng của nhiều loại kế thừa được trộn lẫn để hình thành kế thừa lai.

Thí dụ

# Creating a Base class named University:

class University:

def __init__[self]:

print["Contructor of the Base class"]

# Initializing a class variable named univ to store university name:

self.univ = "MIT"

def display[self]: # Method to print the University Name:

print[f"The University name is: {self.univ}"]

Trong kịch bản Python ở trên:

  • Chúng tôi tạo một lớp cơ sở có tên là trường đại học bằng cách sử dụng từ khóa lớp lớp, với một hàm tạo [INIT] sẽ được gọi là khi đối tượng của lớp được tạo [khởi tạo đối tượng].
  • Tiếp theo, trong hàm tạo, chúng tôi sẽ khởi tạo các thành viên dữ liệu, như Univ, để lưu trữ bất kỳ giá trị nào.
  • Tiếp theo, chúng tôi có một phương thức hiển thị để hiển thị giá trị của thành viên dữ liệu.

Các thành viên của lớp "Đại học"

class Course[University]:

def __init__[self]:

# using "super" keyword to access members of the parent class having same name:

print["Constructor of the Child Class 1 of Class University"]

University.__init__[self]

self.course = "CSE"

def display[self]: # Method to print the Course Name:

# using "super" keyword to access display method defined in the parent class:

print[f"The Course name is: {self.course}"]

University.display[self]

  • Bây giờ, chúng tôi tạo ra một lớp khác có tên là khóa học là lớp học của trường đại học.
  • Ở đây, vì lớp học là lớp đại học xuất phát, lớp này có thể truy cập tất cả các thành viên của lớp phụ huynh.
  • Do đó, chúng tôi sử dụng tên lớp để truy cập các thành viên khác nhau của lớp cha, như được hiển thị trong số dòng 5 và 10 trong đoạn mã trên.

Các thành viên của lớp "khóa học"

# 2nd Derived or Child Class of University Class:

class Branch[University]:

def __init__[self]:

print["Constructor of the Child Class 2 of Class University"]

self.branch = "Data Science"

def display[self]: # Method to print the Branch Name:

print[f"The Branch name is: {self.branch}"]

Tương tự, chúng tôi xác định lớp có nguồn gốc thứ 2, được đặt tên là Branch Branch, của lớp phụ huynh đại học.

Các thành viên của lớp "Chi nhánh"

# Derived or Child Class of Class Course and Branch:

class Student[Course, Branch]:

def __init__[self]:

print["Constructor of Child class of Course and Branch is called"]

self.name = "Tonny"

Branch.__init__[self]

Course.__init__[self]

def display[self]:

print[f"The Name of the student is: {self.name}"]

Branch.display[self]

Course.display[self]

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.

Các thành viên của lớp "Sinh viên"

Mã số

# Creating a Base class named University:

class University:

def __init__[self]:

print["Contructor of the Base class"]

# Initializing a class variable named univ to store university name:

self.univ = "MIT"

def display[self]: # Method to print the University Name:

print[f"The University name is: {self.univ}"]

# 1st Derived or Child Class of University Class:

class Course[University]:

def __init__[self]:

# using "super" keyword to access members of the parent class having same name:

print["Constructor of the Child Class 1 of Class University"]

University.__init__[self]

self.course = "CSE"

def display[self]: # Method to print the Course Name:

# using "super" keyword to access display method defined in the parent class:

print[f"The Course name is: {self.course}"]

University.display[self]

# 2nd Derived or Child Class of University Class:

class Branch[University]:

def __init__[self]:

print["Constructor of the Child Class 2 of Class University"]

self.branch = "Data Science"

def display[self]: # Method to print the Branch Name:

print[f"The Branch name is: {self.branch}"]

# Derived or Child Class of Class Course and Branch:

class Student[Course, Branch]:

def __init__[self]:

print["Constructor of Child class of Course and Branch is called"]

self.name = "Tonny"

Branch.__init__[self]

Course.__init__[self]

def display[self]:

print[f"The Name of the student is: {self.name}"]

Branch.display[self]

Course.display[self]

# Object Instantiation:

ob = Student[] # Object named ob of the class Student.

print[]

ob.display[] # Calling the display method of Student class.

THẺ LIÊN QUAN

Python

di sản

OOP

cộng đồng

Người đóng góp

Aakanksha Govindaraju

3 loại kế thừa là gì?

Các loại di truyền khác nhau.a blend of more than one type of inheritance. The class is derived from the two classes as in the multiple inheritance. However, one of the parent classes is not the base class. It is a derived class. This feature enables the user to utilize the feature of inheritance at its best.

Kế thừa lai trong OOPS là gì?

Di truyền lai là sự kết hợp của nhiều kế thừa và thừa kế đa cấp.Một lớp có nguồn gốc từ hai lớp như trong nhiều kế thừa.Tuy nhiên, một trong những lớp mẹ không phải là lớp cơ sở.Đó là một lớp học có nguồn gốc.a combination of multiple inheritance and multilevel inheritance. A class is derived from two classes as in multiple inheritance. However, one of the parent classes is not a base class. It is a derived class.

Các loại kế thừa trong Python là gì?

Có năm loại thừa kế:..
Thừa kế duy nhất ..
Nhiều kế thừa ..
Thừa kế đa cấp ..
Thừa kế phân cấp ..
Thừa kế lai ..

3 loại kế thừa là gì?

Các loại di truyền khác nhau..
Thừa kế duy nhất ..
Thừa kế đa cấp ..
Nhiều kế thừa ..
Thừa kế đa đường ..
Thừa kế phân cấp ..
Thừa kế lai ..

Chủ Đề