python创建选课系统-----01.实现学校、课程、学生及班级角色创建

首先实现各个角色的创建,同时将各角色的相关联属性连系起来,详细请看代码:

#后期保存合格对象的数据集合
school_list = []
course_list = {}
class_list = {}
teacher_list = {}
student_list = []


class School(object):
    """
    创建学校
    """

    def __init__(self, s_id, name, address):
        self.s_id = s_id
        self.name = name
        self.address = address

    def create_course(self, course_id, course_name, course_price, course_period):
        new_course = Course(course_id, course_name, course_price, course_period)
        new_course.address = self.address
        return new_course

    def create_class(self, class_id, class_content, class_num):
        new_class = SchoolClass(class_id, class_content, class_num)
        new_class.address = self.address
        return new_class

    def create_teacher(self, id_num, name, age, sex):
        new_teacher = SchoolTeacher(id_num, name, age, sex)
        new_teacher.school = self.address
        return new_teacher


class SchoolMember(object):
    """
    学校成员类
    """

    def __init__(self, id_num, name, age, sex):
        self.id_num = id_num
        self.name = name
        self.age = age
        self.sex = sex


class SchoolTeacher(SchoolMember):
    """
    老师
    """

    def __init__(self, id_num, name, age, sex):
        SchoolMember.__init__(self, id_num, name, age, sex)

    def teacher_connect_school(self, t_course, t_class):
        self.course = t_course.course_name
        self.class_name = t_class.class_num
        print('{} is teaching {} {}'.format(self.name, self.course, self.class_name))


class SchoolStudent(SchoolMember):
    """
    学生
    """

    def __init__(self, id_num, name, age, sex):
        SchoolMember.__init__(self, id_num, name, age, sex)

    def stu_connect_school_class(self, school, school_class):
        print('{} is studying in \
                    school of {}, class of {}'.format(self.name, school, school_class))


class Course(object):
    """
    课程
    """

    def __init__(self, course_id, course_name, course_price, course_period):
        self.course_id = course_id
        self.course_name = course_name
        self.course_price = course_price
        self.course_period = course_period


class SchoolClass(object):
    """
    班级
    """

    def __init__(self, class_id, class_content, class_num):
        self.class_id = class_id
        self.class_content = class_content
        self.class_num = class_num

    def cla_connect_course_teacher(self, course, school_teacher):
        self.course_name = course.course_name
        self.teacher_name = school_teacher.name
        print('{} is {} and teached by {}'.format(self.class_id, self.course_name, self.teacher_name))


school_a = School('01', 'old_boy', 'beijing')
school_b = School('01', 'old_boy', 'shanghai')

if __name__ == '__main__':
    new_course = school_a.create_course('001', 'python', 1000, '5 months')
    new_teacher = school_a.create_teacher('t001', 'alex', 32, 'man')
    new_class = school_a.create_class('c001', 'python', '1期')
    new_class.cla_connect_course_teacher(new_course, new_teacher)
    new_teacher.teacher_connect_school(new_course, new_class)

你可能感兴趣的:(脚本)