day 06 初始面向对象

  先回顾了re,讲了一点扩展,爬虫的。然后开始面向对象,终于到了令人期待的环节,面向对象。之前看小马学java的时候,就听闻了这个词。从人狗大战开始说起,逐渐引入面向对象。作业 的话,这次的正则练习加re模块,很关键,比之前透彻很多。就是爬虫那个还是不太懂,我发现就是这样。这次的socketserver也是,就直接甩给你,也不知道为啥。用起来一脸懵逼。然后选课系统,由于没讲反射,写的很low,pickcle不会用,说是不会pickcle其实是不懂面向对象。哎,每次直播,带着写作业,怎么说呢,不能说是不好。但是这种就顺着讲,效果我觉得甚微。就拿这次的作业来说吧,我是看了直播,照着写了。但是我根本不明白什么时候实例化,很被动地接收。也怪自己没有思考,或者说研究的不够

class Father(object): #父类
    def logout(self): #退出
        exit('已退出选课系统')
    def check_course(self):
        with open('课程信息.txt','r',encoding='utf-8') as f:
            course_info = f.readlines()
            for i in course_info:
                print(i,end='')
            print('')
class Course:
    def __init__(self,name,price,period):
        self.name = name
        self.price = price
        self.period = period

class Student(Father):
    def __init__(self,name):
        self.name = name
        self.course = []
    def check_to_choose(self):#查看可选课程
        print('可选课程信息如下:')
        self.check_course()
    def check_chosen_course(self):#查看已选课程
        print(self.course)
    def choose_course(self):#选课
        with open('课程信息.txt','r',encoding='utf-8') as f:
            course_info = f.readlines()
            for k,v in enumerate(course_info,1):
                print(k,v,end='')
            while 1:
                a = input('是否继续?(q或Q退出,其他任意键继续)\n')
                if a.upper() == 'Q':
                    break
                else:
                    num = int(input('请选择课程:(输入序号)'))
                    print(course_info)
                    self.course.append(course_info[num - 1])



class Admin(Father):
    def __init__(self,name):
        self.name = name
    def create_stu(self):#创建学生账号
        while 1:
            a = input('是否继续?(q或Q退出,其他任意键继续)\n')
            if a.upper() == 'Q':
                break
            else:
                print('---创建学生账户---')
                account = input('设置用户名:').strip()
                key = input('设置密码:').strip()
                f = open('用户信息.txt', 'a', encoding='utf-8')
                f.write('\n%s|%s|stu' % (account, key))  # 追加至register文件
                print('学生%s的信息创建成功' % account)
                f.close()
                f1 =open('学生信息.txt','a',encoding='utf-8')
                f1.write('%s\n'%account)
                f1.close()
    def create_course(self):#创建课程
        while 1:
            a = input('是否继续?(q或Q退出,其他任意键继续)\n')
            if a.upper() == 'Q':
                break
            else:
                name = input('输入要创建的课程名:').strip()
                price = input('输入课程价格:').strip()
                period = input('输入课程周期:').strip()
                course_obj = Course(name,price,period)
                with open('课程信息.txt','a',encoding='utf-8') as f:
                    f.write('%s  %s  %s \n'%(name,price,period))
                print('课程创建成功')

    def check_all_course(self):#查看全部课程
        print('全部课程信息如下:')
        self.check_course()
    def check_stu(self):#查看全部学生
        with open('学生信息.txt', 'r') as f:
            stu_info = f.readlines()
            for i in stu_info:
                print(i,end='')
    def check_situation(self):
        pass


def login():
    count = 3
    f = open('用户信息.txt',encoding='utf-8')
    content = f.readlines()
    while count > 0:
        print('----请登录系统-----')
        in_name = input('输入您的用户名:').strip()
        in_pwd = input('输入您的密码:').strip()
        for i in content:
            name,pwd,identity = i.strip().split('|')
            if in_name == name and in_pwd == pwd:
                print('%s:%s 登录成功'%(identity,name))
                return {'name':name,'identity':identity}
        else:
            count -= 1
            if count > 0:
                print('用户名或密码错误,还有%s次机会'%count)
            if count == 0:
                quit('错误次数过多,已退出')
            continue

def core():
    ret = login()
    if ret:
        print('正在跳转到主页')
        if ret['identity'] == 'admin':
            manager = Admin(ret['name'])
            while 1:
                l_admin = ['创建学生','创建课程','查看所有课程','查看所有学生','查看学生选课情况','退出']
                for k,v in enumerate(l_admin,1):
                    print(k,v)
                choice = input('输入数字执行对应功能:')
                if choice.isdigit():
                    choice = int(choice)
                    if choice == 1:
                        manager.create_stu()
                    if choice == 2:
                        manager.create_course()
                    if choice == 3:
                        manager.check_all_course()
                    if choice == 4:
                        manager.check_stu()
                    if choice == 5:
                        manager.check_situation()
                    if choice == 6:
                        manager.logout()
        elif ret['identity'] == 'stu':
            student = Student(ret['name'])
            while 1:
                l_stu = ['查看所有课程','选择课程','查看所选课程','退出']
                for k,v in enumerate(l_stu,1):
                    print(k,v)
                choice = input('输入数字执行对应功能:')
                if choice.isdigit():
                    choice = int(choice)
                    if choice == 1:
                        student.check_to_choose()
                    if choice == 2:
                        student.choose_course()
                    if choice == 3:
                        student.check_chosen_course()
                    if choice == 4:
                        student.logout()

core()

虽说现在明白,Student(ret['name'])是在实例化对象,可是为啥都是student,然后就自己去试了下实例化,是不同的对象。看开哥的答案,好像有一点更明确了,继续看吧,果然没有那么简单。

posted @ 2018-09-17 15:04  CP喜欢晒太阳  阅读(83)  评论(0)    收藏  举报