本文主要是介绍课程管理类的设计,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#父类基类
class Course:lesson={ } #课程列表{'python基础':,'正正','python web':,'宁夫'}__totalSale = 0 #总销量__totalIncome = 0 #总收入lesson_comment = { } #{'python基础':['讲得好','英文太多听不懂'],'python web':['实用性','老师水平高']}education_year =2022def __init__(self,price):Course.__totalSale=Course.__totalSale+1 #特别注意,这里通过class名称直接访问,self.__totalSale成为实例的私有变量Course.__totalIncome=Course.__totalIncome+price #做收入的累加@classmethod #返回内部值(作为管理人员查看内部私有属性)def gettotal(cls):return cls.__totalIncome,cls.__totalSale@staticmethoddef aboutWanmen():print('万门是一个多么好的大学!')#声明单课类 lessons 类 继承 Course
class lessons(Course):"""title:课程名称content:课程内容lessontype:课程类型teacher:老师姓名 objisOnline:上架/下架 非参数 成员变量/实例变量"""def __init__(self,title,content,lesson_type,teacher,price): #teacher和lesson带入对象super().__init__(price) #这种是调用父类原始的构造函数self.title=title #课程名称self.content=content #课程内容信息self.lesson_type=lesson_type #类型免费/收费self.teacher=teacher.name #老师self.isOnline=Falsedef add_lesson(self): #定义课程上线self.lesson[self.title]=self.teacherself.isOnline=Truedef remove_lesson(self): #定义课程下线del self.lesson[self.title]self.isOnline=False#声明 课程类型类
class Lesson_type:def __init__(self,type):self.type=typeif self.type=='免费':self.benefit='可以观看20%的课程'else:self.benefit='可以观看100%的课程'class Teacher:#老师类def __init__(self,name,age):self.name=nameself.age=ageclass comment:def __init__(self,comment,lesson):self.comment=commentself.lesson=lesson.titledef add_comment(self):if Course.lesson_comment.get(self.lesson) == None: #dict.get('key')#字典.get(key)取的是其中的值,若为空Course.lesson_comment[self.lesson]=[self.comment] #dict[key]=value#则将评论直接赋给key所对应的值上else:add_comment = Course.lesson_comment[self.lesson] #dict[key]获取的值add_comment.append(self.comment) #给当前的值append数据Course.lesson_comment[self.lesson] = add_comment#1课程类型实例
free=Lesson_type('免费') #实例
charge=Lesson_type('收费') #实例#2老师实例
ZZ=Teacher('正正',28) #实例
NF=Teacher('宁夫',26) #实例#3具体课程实例
python_basic1 = lessons('python基础1','趣味讲述',free,ZZ,199) #初始化 带入以上实例objec对象
python_basic2 = lessons('python基础2','趣味讲述',free,ZZ,199) #初始化 带入以上实例objec对象
python_basic3 = lessons('python基础3','趣味讲述',free,ZZ,199) #初始化 带入以上实例objec对象
python_web = lessons('python web','网站开发',charge,NF,548) #初始化 带入以上实例objec对象#课程准备好 开始添加课程
python_basic1.add_lesson()
python_basic2.add_lesson()
python_basic3.add_lesson()
python_web.add_lesson()print(Course.lesson)
#可以下架或移除课程
python_web.remove_lesson()
#查看属性
print(python_web.isOnline)
print(Course.lesson)#先有课程后评论,创建评论实例
comment1=comment('非常好的课程',python_basic1)
comment1.add_comment()comment2=comment('通俗的课程',python_basic2)
comment2.add_comment()comment3=comment('代码写的非常好',python_basic3)
comment3.add_comment()comment4=comment('学了就能独立做网站了',python_web)
comment4.add_comment()
print(Course.lesson_comment)#查询类
class BasicInfo(Course): #对外可查询接口,数据也是相对公开的,注意这里不能再去初始化父类构造的函数了def __init__(self): #这种情况看似平常,实则重写了父类构造的函数,换句话说,不让父类的init执行pass #如果父类执行这里会导致父类的计数器增长def get_lesson(self):print('课程列表',Course.lesson)def get_comment(self):print('课程评价',Course.lesson_comment)def wanmen(self): #加一个知识点 super()直接调用父类方法super().aboutWanmen()get_info=BasicInfo()
get_info.wanmen()
get_info.get_comment()
get_info.get_comment()
print(Course.gettotal())
这篇关于课程管理类的设计的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!