Hướng dẫn hybrid inheritance in python diagram - thừa kế lai trong sơ đồ python

Kế thừa

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

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

Kế thừa là quá trình tạo một lớp mới từ một lớp hiện có. ... Thí dụ..

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

Ví dụ về di truyền lai trong C ++ loại A như lớp động vật, loại B là động vật có vú, lớp C như động vật ăn cỏ, lớp D như bò.Động vật có vú có thể có nguồn gốc từ lớp động vật, và bò là sự kết hợp giữa động vật ăn cỏ và động vật có vú.Mối quan hệ này xác định tốt sự kết hợp của nhiều kế thừa và thừa kế đơn.Class A as Animal Class, Class B as Mammals, Class C as Herbivores, Class D as Cow. Mammals can be derived from Animal class, and Cow is a combination of Herbivores and Mammals. This relationship well defines the combination of Multiple Inheritance and Single Inheritance.

Sự thừa kế trong Python và đưa ra các loại của nó là gì?

Kế thừa là một quá trình thu được các thuộc tính và đặc điểm (biến và phương pháp) của một lớp khác.Theo thứ tự phân cấp này, lớp kế thừa một lớp khác được gọi là lớp con hoặc lớp con và lớp khác là lớp cha.a process of obtaining properties and characteristics(variables and methods) of another class. In this hierarchical order, the class which inherits another class is called subclass or child class, and the other class is the parent class.

4 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 ..