课程管理类的设计

在这里插入图片描述
在这里插入图片描述

#父类基类
class Course:

    lesson={ } #课程列表{'python基础':,'正正','python web':,'宁夫'}
    __totalSale = 0             #总销量
    __totalIncome = 0           #总收入
    lesson_comment = { }  #{'python基础':['讲得好','英文太多听不懂'],'python web':['实用性','老师水平高']}
    education_year =2022

    def __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

    @staticmethod
    def aboutWanmen():
        print('万门是一个多么好的大学!')

#声明单课类 lessons 类 继承 Course
class lessons(Course):
    """
    title:课程名称
    content:课程内容
    lessontype:课程类型
    teacher:老师姓名  obj
    isOnline:上架/下架  非参数  成员变量/实例变量
    """
    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=False

    def add_lesson(self):                                             #定义课程上线
        self.lesson[self.title]=self.teacher
        self.isOnline=True

    def remove_lesson(self):                                          #定义课程下线
        del self.lesson[self.title]
        self.isOnline=False

#声明 课程类型类
class Lesson_type:
    def __init__(self,type):
        self.type=type
        if self.type=='免费':
            self.benefit='可以观看20%的课程'
        else:
            self.benefit='可以观看100%的课程'

class Teacher:#老师类
    def __init__(self,name,age):
        self.name=name
        self.age=age

class comment:
    def __init__(self,comment,lesson):
        self.comment=comment
        self.lesson=lesson.title

    def 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())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值