Hướng dẫn demonstrate hybrid inheritance using python classes - chứng minh kế thừa kết hợp bằng cách sử dụng các lớp python

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

Kế thừa lai với ví dụ trong Python 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 giải thích 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.the combination of Multiple Inheritance and Single Inheritance.

Kế thừa giải thích với một 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.

Điều gì MRO chứng minh nó với sự kế thừa lai trong Python?

MRO là một khái niệm được sử dụng trong kế thừa.Đó là thứ tự trong đó một phương thức được tìm kiếm trong phân cấp lớp và đặc biệt hữu ích trong Python vì Python hỗ trợ nhiều kế thừa.the order in which a method is searched for in a classes hierarchy and is especially useful in Python because Python supports multiple inheritance.

Bài Viết Liên Quan

Chủ Đề