Kế thừa đa cấp trong Python có nghĩa là gì?

*Xin lưu ý* - tất cả các bài thuyết trình và thử thách mã + giải pháp đều có sẵn cho các thành viên đăng ký. Bây giờ là lúc để tìm hiểu sâu hơn về khái niệm kế thừa đẹp đẽ này và khám phá kế thừa đa cấp độ. Điều quan trọng là bạn phải hiểu sự khác biệt giữa các loại kế thừa để có thể áp dụng kiến ​​thức và giải pháp mã của mình. Bạn cũng nên làm quen với cách các lớp hoạt động trong thực tế và cách bạn có thể sử dụng chúng trong các dự án của mình. Hãy xem video bên dưới để giải thích khái niệm này, xem qua ví dụ mã hóa và sau đó tự mình thử các thử thách được trình bày bên trong mã

Video

Mật mã và Thử thách #1

"""
MULTILEVEL INHERITANCE
Real world example: If your father played chess, you inherited this ability, but you also like to dance. Your
son, if you have one, could inherit both your father's ability to play chess as well as your dance moves!

While multiple inheritance is when a child class derives attributes and methods from more than one
parent class, multilevel inheritance is referring to a series of inheritance, with a class at the first level
and a class at the second level. Creation of a class at a third level would mean that this class would
inherit all the attributes and methods of the class at the second level [and in so doing, access to the
attributes and methods of the class at the first level, as the second level class inherits from it!]

Definition:  Mechanism by which a derived class inherits from a base class which has been derived from
another base class

TASK:

To test out the functionality of multilevel inheritance, do the following and test it:
1. Create a method in the TopComputingStudent Class called "Eligible_for_Reward_Trip". This method checks to
see if the student is above the age of 13 AND if their total marks is above 90. If elibility metL: print: You are eligible for the reward trip to London!
2. Create an object called s1, and call the "Eligible_for_Reward_Trip" method to see if it works. Note that this method would call
on attributes defined in parent classes.

"""

class Student: #Define a Student Class
    def __init__[self]:
        self.name = input["Name: "]
        self.age = int[input["Age: "]]
        self.gender = input["Gender: "]
    def printStudent[self]:
        print["=====Printing Student Details========"]
        print[self.name]
        print[self.age]
        print[self.gender]

class ComputingStudent[Student]: #the ComputintStudent Class inherits from the Student Class
    def __init__[self]:
        super[ComputingStudent, self].__init__[]
        print["Enter the marks for your first three tests:"]
        self.test1= int[input["Test1: "]]
        self.test2 = int[input["Test2: "]]
        self.test3 = int[input["Test3: "]]

    def getMarks[self]:
        print["====PRINTING YOUR TEST SCORES=="]
        print[self.test1]
        print[self.test2]
        print[self.test3]
        

class TopComputingStudent[ComputingStudent]:#The TopComputingStudent Class inherits from the Computing Student which in turn inherits from Student!
    def __init__[self]:
        super[TopComputingStudent, self].__init__[]
        self.total_marks = self.test1 + self.test2 + self.test3

    def display[self]:
        print["\n\nName: ",self.name]
        print["Age: ",self.age]
        print["Gender: ",self.gender]

        print["Total Marks: ", self.test1 + self.test2 + self.test3]

    def eligible_for_reward[self]:
        pass
    #YOUR CODE GOES HERE 

#======DEMONSTRATION OF MULTIPLE INHERITANCE ==========
if __name__ == "__main__":

    s2=TopComputingStudent[]
    s2.printStudent[]
    s2.getMarks[]



    


"""
Useful notes:
#1 inheritance tree is [from a design POV] important to consider logically
Inheritance, is semantically, a "is a" relationship [if B inherit from A, then B is a A].
In this case, a top computing student is a computing student and a computing student is a Student!

#2     def __init__[self]:
        super[TopComputingStudent, self].__init__[]

        super[] calls the __init__ method of the parent class. So the above example passes the name
        of the current class which in turn will call the __init__ method of the parent class.
"""



Tải xuống Python 3 tại đây


  • Kế thừa đa cấp cũng có thể thực hiện được trong Python không giống như các ngôn ngữ lập trình khác
  • Bạn có thể kế thừa một lớp dẫn xuất từ ​​một lớp dẫn xuất khác
  • Điều này được gọi là kế thừa đa cấp. Trong Python, kế thừa đa cấp có thể được thực hiện ở bất kỳ độ sâu nào

  • Một ví dụ với trực quan hóa tương ứng được đưa ra dưới đây

class Base:
    pass

class Derived1[Base]:
    pass

class Derived2[Derived1]:
    pass

Thí dụ. 1 Kế thừa đa cấp Python

#!/usr/bin/evn python

# Define a class as 'student'
class student:
    # Method
    def getStudent[self]:
        self.name = input["Name: "]
        self.age = input["Age: "]
        self.gender = input["Gender: "]

# Define a class as 'test' and inherit base class 'student'
class test[student]:
    # Method
    def getMarks[self]:
        self.stuClass = input["Class: "]
        print["Enter the marks of the respective subjects"]
        self.literature = int[input["Literature: "]]
        self.math = int[input["Math: "]]
        self.biology = int[input["Biology: "]]
        self.physics = int[input["Physics: "]]

# Define a class as 'marks' and inherit derived class 'test'
class marks[test]:
    # Method
    def display[self]:
        print["\n\nName: ",self.name]
        print["Age: ",self.age]
        print["Gender: ",self.gender]
        print["Study in: ",self.stuClass]
        print["Total Marks: ", self.literature + self.math + self.biology + self.physics]


m1 = marks[]
# Call base class method 'getStudent[]'
m1.getStudent[]
# Call first derived class method 'getMarks[]'
m1.getMarks[]
# Call second derived class method 'display[]'
m1.display[]

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211

Ví dụ2. Kế thừa đa cấp Python

class Animal:
def eat[self]:
print 'Eating...'
class Dog[Animal]:
def bark[self]:
print 'Barking...'
class BabyDog[Dog]:
def weep[self]:
print 'Weeping...'
d=BabyDog[]
d.eat[]
d.bark[]
d.weep[]

Eating...
Barking...
Weeping

Ý nghĩa của kế thừa đa cấp là gì?

Thừa kế đa cấp bao gồm sự tham gia của ít nhất hai hoặc nhiều hơn hai lớp . Một lớp kế thừa các tính năng từ lớp cha và lớp con mới được tạo trở thành lớp cơ sở cho một lớp mới khác.

Sự khác biệt giữa kế thừa đa cấp và đa cấp trong Python là gì?

Sự khác biệt chính giữa Kế thừa đa cấp và Đa cấp là Đa kế thừa là khi một lớp kế thừa từ nhiều lớp cơ sở trong khi Kế thừa đa cấp là khi một lớp kế thừa từ một lớp dẫn xuất làm cho lớp dẫn xuất đó trở thành lớp cơ sở cho một lớp mới

Chủ Đề