课程对象的定义与使用
class School:
school_name = 'oldboy'
def __init__(self, nickname, addr):
self.nickname = nickname
self.addr = addr
self.classes = []
def ralated_class(self, class_obj):
self.classes.append(class_obj)
def tell_class(self):
print(self.nickname.center(50, '='))
for class_obj in self.classes:
class_obj.tell_course()
class Class:
def __init__(self, name):
self.name = name
self.course = None
def related_course(self, course_obj):
self.course = course_obj
def tell_course(self):
print('%s ' % self.name, end='')
self.course.tell_info()
class Student:
_name = '名称'
def __init__(self, name):
self.name = name
print(f'学生姓名是:{name}')
def Choose_class(self):
pass
class Course:
def __init__(self, name, period, price):
self.name = name
self.period = period
self.price = price
def related_students(self, student_obj):
pass
def tell_info(self):
print('<课程名:%s 周期:%s 价格:%s>' % (self.name, self.period, self.price))
class Teacher:
_name = '名称'
def __init__(self):
pass
创建校区
school_obj1 = School('老男孩上海校区', '上海')
school_obj2 = School('老男孩帝都校区', '北京')
创建班级
class_obj1 = Class('脱产14期')
class_obj2 = Class('脱产15期')
class_obj3 = Class('脱产29期')
创建课程
course_obj1 = Course('python全栈开发', '6mons', 20000)
course_obj2 = Course('linux运维', '5mons', 18000)
course_obj3 = Course('python全栈开发', '6mons', 20000)
为班级开设课程
class_obj1.related_course(course_obj1)
class_obj2.related_course(course_obj2)
class_obj3.related_course(course_obj3)
为学校开设班级
school_obj1.ralated_class(class_obj1)
school_obj1.ralated_class(class_obj2)
school_obj2.ralated_class(class_obj3)
school_obj1.tell_class()
school_obj2.tell_class()
print(school_obj1.__dict__)
print(class_obj1 .__dict__)
print(course_obj1.__dict__)
=====================老男孩上海校区======================
脱产14期 <课程名:python全栈开发 周期:6mons 价格:20000>
脱产15期 <课程名:linux运维 周期:5mons 价格:18000>
=====================老男孩帝都校区======================
脱产29期 <课程名:python全栈开发 周期:6mons 价格:20000>
{'nickname': '老男孩上海校区', 'addr': '上海', 'classes': [<__main__.Class object at 0x0000010FC816A970>, <__main__.Class object at 0x0000010FC816AA30>]}
{'name': '脱产14期', 'course': <__main__.Course object at 0x0000010FC816AAF0>}
{'name': 'python全栈开发', 'period': '6mons', 'price': 20000}