Python组合示例

时间:2018-01-29 07:27:04

标签: python composition

我一直在努力学习用于练习和学习Python的示例代码。理解下面的代码并不难,但是,如果我逐行进行,我不确定下面的代码是什么。

course_running.add_student(self)

我们如何将CourseRunning类的add_student方法运行到参数course_running(学生类,注册方法)? 如果您能解释这行代码是如何工作的,我将非常感激。

完整代码如下。



class Student:
    def __init__(self, name, student_number):
        self.name = name
        self.student_number = student_number
        self.classes = []

    def enrol(self, course_running):
        self.classes.append(course_running)
        course_running.add_student(self)


class Department:
    def __init__(self, name, department_code):
        self.name = name
        self.department_code = department_code
        self.courses = {}

    def add_course(self, description, course_code, credits):
        self.courses[course_code] = Course(description, course_code, credits, self)
        return self.courses[course_code]


class Course:
    def __init__(self, description, course_code, credits, department):
        self.description = description
        self.course_code = course_code
        self.credits = credits
        self.department = department
        self.department.add_course(self)

        self.runnings = []

    def add_running(self, year):
        self.runnings.append(CourseRunning(self, year))
        return self.runnings[-1]


class CourseRunning:
    def __init__(self, course, year):
        self.course = course
        self.year = year
        self.students = []

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




提前致谢! 伊戈尔

2 个答案:

答案 0 :(得分:0)

CourseRunning对象具有其参加学生的列表,而Student对象具有学生当前正在学习的课程列表。

说有student1running_course1。我们希望将student1注册到running_course1。我们跑:

student1.enroll(running_course1)

enroll方法做了两件事:

  1. self.classes.append(course_running) - 翻译为`student1.classes.append(running_course1)。所以现在课程已在学生的名单中注册。
  2. course_running.add_student(self) - 转换为running_course1.add_student(student1)。这会调用running_course1.add_student(self, student1),将学生添加到课程列表中。

答案 1 :(得分:0)

我认为这不是一个完整的代码,因为没有任何对象存在,没有方法调用..

我正在为您提供代码,请在您的电脑上运行。

class Student:

    def __init__(self, name, student_number):
        self.name = name
        self.student_number = student_number
        self.classes = []

    def enrol(self, course_running):
        print "In enroll method"
        self.classes.append(course_running)
        course_running.add_student(self)


class Department:

    def __init__(self, name, department_code):
        self.name = name
        self.department_code = department_code
        self.courses = {}

    def add_course(self, description, course_code, credits):
        self.courses[course_code] = Course(description, course_code, credits, self)
        return self.courses[course_code]


class Course:

    def __init__(self, description, course_code, credits, department):
        self.description = description
        self.course_code = course_code
        self.credits = credits
        self.department = department
        self.department.add_course(self)

        self.runnings = []

    def add_running(self, year):
        self.runnings.append(CourseRunning(self, year))
        return self.runnings[-1]


class CourseRunning:

    def __init__(self, course, year):
        self.course = course
        self.year = year
        self.students = []

    def add_student(self, student):
        print "student-->", student
        print "In Add Student"
        self.students.append(student)


course_running = CourseRunning('b.tech', '2011')
obj = Student('student_xyz', "1234")
obj.enrol(course_running)

这里发生的事情是我们正在创建一个“CourseRunning”类的对象。 当我们调用“Student”类的“enroll”方法时,我们传递“CourseRunning”类的对象,这样我们就可以访问“CourseRunning”类的方法,因为“course_running”是“CourseRunning”类的对象所以它将访问“CourseRunning”类的所有方法