以学校、班级、课程、学生数据和功能的整合,来加深面向对象编程的理解与运用。
学校类的定义与使用
lass School:
school_name = '广师大'
def __init__(self, address, nickname):
self.address = address
self.nickname = nickname
self.classes = []
def related_class(self, class_name):
self.classes.append(class_name)
def tell_class(self):
for class_name in self.classes:
print('%s开设了%s' % (self.nickname, class_name))
School_While = School('白云', '白云校区')
School_East = School('天河', '东校区')
School_While.related_class('python全栈课程')
School_While.related_class('C语言')
School_While.related_class('C++')
School_East.related_class('crawler课程')
School_East.related_class('Java课程')
School_While.tell_class()
School_East.tell_class()
'''
白云校区开设了python全栈课程
白云校区开设了C语言
白云校区开设了C++
东校区开设了crawler课程
东校区开设了Java课程
'''
班级定义与使用
class Class:
def __init__(self, name):
self.name = name
self.course = None
def related_course(self, course_name):
self.course = course_name
class_computer1 = Class('18计算机1班')
class_computer2 = Class('19计算机2班')
class_computer1.related_course('C#')
class_computer2.related_course('Java')
print(class_computer1.course)
'''
C#
Java
'''
关联学校与班级
class School:
school_name = '广师大'
def __init__(self, address, nickname):
self.address = address
self.nickname = nickname
self.classes = []
def related_class(self, class_obj):
self.classes.append(class_obj)
def tell_class(self):
print(f'{self.nickname}:')
for class_obj in self.classes:
class_obj.tell_course()
School_While = School('白云', '白云校区')
School_East = School('天河', '东校区')
class Class:
def __init__(self, name):
self.name = name
self.course = None
def related_course(self, course_name):
self.course = course_name
def tell_course(self):
print('%s开设了%s' % (self.name, self.course))
class_computer1 = Class('18计算机1班')
class_computer2 = Class('19计算机2班')
class_computer1.related_course('C++')
class_computer2.related_course('Java课程')
School_While.related_class(class_computer1)
School_East.related_class(class_computer2)
School_While.tell_class()
School_East.tell_class()
学校与班级,课程相关联
class School:
school_name = '广师大'
def __init__(self, address, nickname):
self.address = address
self.nickname = nickname
self.classes = []
def related_class(self, class_obj):
self.classes.append(class_obj)
def tell_class(self):
print(f'{self.nickname}:')
for class_obj in self.classes:
class_obj.tell_course()
School_While = School('白云', '白云校区')
School_East = School('天河', '东校区')
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)
self.course.tell_info()
class_computer1 = Class('18计算机1班')
class_computer2 = Class('19计算机2班')
class_computer1.related_course('C++')
class_computer2.related_course('Java课程')
School_While.related_class(class_computer1)
School_East.related_class(class_computer2)
class Course:
def __init__(self, name, period, price):
self.name = name
self.period = period
self.price = price
def tell_info(self):
print('<课程名称:[%s] 周期:[%s] 价钱:[%s]>' % (self.name, self.period, self.price))
course_obj1 = Course('python课程', '6个月', 20000)
course_obj2 = Course('linux运维', '5个月', 10000)
class_computer1.related_course(course_obj1)
class_computer2.related_course(course_obj2)
School_While.tell_class()
'''
白云校区:
班级名称:18计算机1班
<课程名称:[python课程] 周期:[6个月] 价钱:[20000]>
'''
|