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
- Thừa kế duy nhất
- Thừa kế đa cấp
- Nhiều kế thừa
- Di truyền phân cấp
- 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