码迷,mamicode.com
首页 > 其他好文 > 详细

反射的应用

时间:2018-08-01 12:00:30      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:line   inf   spl   word   反射   bsp   pre   enum   get   

class Teacher:
    OPERATE_DIC = [
        (创建课程, create_course),
        (创造学生, create_student),
        (创建课程, create_course),
        (查看学生信息, check_student_info),
    ]

    def __init__(self, name):
        self.name = name

    def create_course(self):
        print(创建课程)

    def create_student(self):
        print(创建学生)

    def check_student_info(self):
        print(查看学生信息)

    def check_all_student(self):
        print(查看所有学生)


class Student:
    OPERATE_DIC = [
        (查看所有课程, check_course),
        (选择课程, choose_course),
        (查看已选择的课程, choosed_course)
    ]

    def __init__(self, name):
        self.name = name

    def check_course(self):
        print(查看课程)

    def choose_course(self):
        print(选择课程)

    def choosed_course(self):
        print(查看已选课程)


def longin():
    username = input(输入账号:)
    password = input(输入密码:)

    with open(userinfo.txt, r, encoding=utf-8) as f:
        for line in f:
            user, pwd, ident = line.split()
            if user == username and pwd == password:
                print(登录成功!)
                return username, ident


import sys


def main():
    usr, id = longin()
    print(user,id:, usr, id)
    file = sys.modules[__main__]
    cls = getattr(file, id)
    print(cls)
    obj = cls(usr)
    operate_dic = cls.OPERATE_DIC
    while 1:
        for num, i in enumerate(operate_dic, 1):
            print(num, i[0])

        choice = int(input(num>>>))
        choice_item = operate_dic[choice - 1]
        getattr(obj, choice_item[1])()


main()

 

反射的应用

标签:line   inf   spl   word   反射   bsp   pre   enum   get   

原文地址:https://www.cnblogs.com/xiao-xue-di/p/9399253.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!