理解继承中的钻石问题

问题描述 投票:0回答:1

我试图通过以下代码来理解钻石问题:

#implement Hybrid Inheritance

class University:
    def __init__(self,univ_name):
        self.univ_name=univ_name
    def show_details(self):
        print(f"University Name : {self.univ_name}")

class Course(University):
    def __init__(self, univ_name,course_name):
        super().__init__(univ_name)
        self.course_name=course_name
    
    def show_details(self):
        super().show_details()
        print(f"Course_name : {self.course_name}")

class Branch(University):
    def __init__(self, univ_name, branch_name):
        super().__init__(univ_name)
        self.branch_name=branch_name

    def show_details(self):
        super().show_details()
        print(f"Branch_name : {self.branch_name}")

class Student(Course,Branch):
    def __init__(self, univ_name, course_name,branch_name):
        Course.__init__(self,univ_name, course_name)
        Branch.__init__(self,univ_name,branch_name)
    def show_details(self):
        Course.show_details(self)
        Branch.show_details(self)

std_1=Student("UTL","Masters","CSE")
std_1.show_details()

当我运行代码时,出现以下错误:

Traceback (most recent call last):
  File "\Assignements\Exercise_4.py", line 35, in <module>
    std_1=Student("UTL","Masters","CSE")
  File "\Assignements\Exercise_4.py", line 29, in __init__
    Course.__init__(self,univ_name, course_name)
  File "\Assignements\Exercise_4.py", line 11, in __init__
    super().__init__(univ_name)
TypeError: __init__() missing 1 required positional argument: 'branch_name'

上述错误是否是由于钻石问题造成的,如何解决?

python inheritance diamond-problem
1个回答
0
投票

您可以调用

__mro__
方法来查看类层次结构:

print(Student.__mro__)
(<class '__main__.Student'>, <class '__main__.Course'>, <class '__main__.Branch'>, <class '__main__.University'>, <class 'object'>)

因此,对于学生来说,

Course
班级正在调用
super().__init__
班级上的
Branch
。这可以通过像您在
Student
课程中所做的那样来解决,并执行以下操作:

University.__init__(self, univ_name)

Branch
Course
课程中。

解决这个问题的更好方法是使用组合: 大学有课程和分校。课程有学生。分行有学生:

class University:
    def __init__(self, name):
        self.name = name
        self.courses: list[Course] = []
        self.branches: list[Branch] = []

    def add_course(self, course):
        self.courses.append(course)

    def add_branch(self, branch):
        self.branches.append(branch)

    def show_details(self):
        print(f"University Name: {self.name}")
        print("Courses Offered:")
        for course in self.courses:
            course.show_details()
        print("Branches Available:")
        for branch in self.branches:
            branch.show_details()


class Course:
    def __init__(self, name):
        self.name = name
        self.students = []

    def show_details(self):
        print(f"Course Name: {self.name}")
        print("Students: ")
        for student in self.students:
            student.show_details()

    def add_student(self, student):
        self.students.append(student)


class Branch:
    def __init__(self, name):
        self.name = name
        self.students: list[Student] = []

    def show_details(self):
        print(f"Branch Name: {self.name}")
        print("Students: ")
        for student in self.students:
            student.show_details()

    def add_student(self, student):
        self.students.append(student)


class Student:
    def __init__(self, name):
        self.name = name

    def show_details(self):
        print(f"Student Name: {self.name}")


def main():
    university = University("UTL")

    course_masters = Course("Masters")
    branch_cse = Branch("CSE")

    university.add_course(course_masters)
    university.add_branch(branch_cse)

    student = Student("John Doe")
    course_masters.add_student(student)
    branch_cse.add_student(student)
    university.show_details()


if __name__ == "__main__":
    main()

输出:

University Name: UTL
Courses Offered:
Course Name: Masters
Students: 
Student Name: John Doe
Branches Available:
Branch Name: CSE
Students: 
Student Name: John Doe

您可以根据您的用例重新构建它。

© www.soinside.com 2019 - 2024. All rights reserved.