python基础知识求助

0
#学员类
class Student(Baseclass):

    def __init__(self,student_name,student_sex,student_school,student_classes):
        Baseclass.__init__(self)
        self.student_name = student_name
        self.student_sex = student_sex
        self.student_school = student_school
        self.student_classes = student_classes
    def student_registered(self):
        student_dict = {}
        print("欢迎进入学生注册系统")
        student_name = input("注册姓名:")
        student_sex = input("性别:")
        student_school = input("学校:")
        student_class = input("班级:")
        st1 = Student(student_name,student_sex,student_school,student_class)
        student_dict["姓名"] = st1.student_name
        student_dict["性别"] = st1.student_sex
        student_dict["学校"] = st1.student_school
        student_dict["班级"] = st1.student_classes
        Baseclass.save(self, "student", student_dict)
#学员视图类,继承Student类
class Student_view(Student):
    def __init__(self,student_name,student_sex,student_school,student_classes):
        Student.__init__(self,student_name,student_sex,student_school,student_classes)

    def login(self):
        menu = u'''
        ------- 欢迎进入学生管理视图 ---------
        \033[32;1m 1.  注册
        2.  交学费
        3.  查看上课记录
        4.  查看作业成绩
        5.  返回
        \033[0m'''
        menu_dic = {
            '1': Student.student_registered, #Student.student_registered(self)
            '2': Student.student_pay_fees,
            '3': Student.student_view_record,
            '4': Student.student_view_grade,
            '5': "logout",
        }
        if True:
            exit_flag = False
            while not exit_flag:
                print(menu)
                option = input("请选择:").strip()
                if option in menu_dic:
                    if int(option) == 5:
                        exit_flag = True
                    else:
                        menu_dic[option](self)
                else:
                    print("\033[31;1m输入错误,重新输入\033[0m")

为啥通过:Student.student_registered(self)调用Student类里的student_registered方法时,不用给Student类里的__init__变量传递参数。

正常流程应该是:

student_1 = Student('韩小林','男','石油大学','软件一班')

student_1 .student_registered()

应该这样写呀。(我目前只学了这么点儿)

如果觉得这对你有用,请随意赞赏,给与作者支持
回答 (2)
2 个回答

0
#学员类
class Student(Baseclass):

    def __init__(self,student_name,student_sex,student_school,student_classes):
        Baseclass.__init__(self)
        self.student_name = student_name
        self.student_sex = student_sex
        self.student_school = student_school
        self.student_classes = student_classes
    def student_registered(self):
        student_dict = {}
        print("欢迎进入学生注册系统")
        student_name = input("注册姓名:")
        student_sex = input("性别:")
        student_school = input("学校:")
        student_class = input("班级:")
        st1 = Student(student_name,student_sex,student_school,student_class)
        student_dict["姓名"] = st1.student_name
        student_dict["性别"] = st1.student_sex
        student_dict["学校"] = st1.student_school
        student_dict["班级"] = st1.student_classes
        Baseclass.save(self, "student", student_dict)
    def student_pay_fees(self):
        pass
    def student_view_grade(self):
        student_school = input("校名:")
        student_class = input("班级:")
        student_times = input("课次:")
        student_name = input("姓名:")
        class_grade_list = Baseclass.open(self,"class_grade")
        for i in class_grade_list:
            for j in i:
                if j["学校"] == student_school and j["班级"] == student_class and j["课次"] == student_times \
                    and j["姓名"] == student_name:
                    for key in j:
                        print(key,j[key])
                    print("\n")
    def student_view_record(self):
        student_school = input("校名:")
        student_class = input("班级:")
        student_times = input("课次:")
        student_name = input("姓名:")
        class_record_list = Baseclass.open(self,"class_record")
        for i in class_record_list:
            for j in i:
                if j["学校"] == student_school and j["班级"] == student_class and j["课次"] == student_times \
                    and j["姓名"] == student_name:
                    for key in j:
                        print(key,j[key])
                    print("\n")
#学员视图类,继承Student类
class Student_view(Student):
    def __init__(self,student_name,student_sex,student_school,student_classes):
        Student.__init__(self,student_name,student_sex,student_school,student_classes)

    def login(self):
        menu = u'''
        ------- 欢迎进入学生管理视图 ---------
        \033[32;1m 1.  注册
        2.  交学费
        3.  查看上课记录
        4.  查看作业成绩
        5.  返回
        \033[0m'''
        menu_dic = {
            '1': Student.student_registered,
            '2': Student.student_pay_fees,
            '3': Student.student_view_record,
            '4': Student.student_view_grade,
            '5': "logout",
        }
        if True:
            exit_flag = False
            while not exit_flag:
                print(menu)
                option = input("请选择:").strip()
                if option in menu_dic:
                    if int(option) == 5:
                        exit_flag = True
                    else:
                        menu_dic[option](self)
                else:
                    print("\033[31;1m输入错误,重新输入\033[0m")

回复@谦谦君子 说:__init__时构造方法,不用显示调用这个我知道,但是在调用Student()这个类时,是不是得先给__init__里的构造方法传值,按照你说的,在下面通过st1 = Student(student_name,student_sex,student_school,student_class)给__init__把值传了,但是选3的时候,进行上课记录查询,为什么又没有给__init__传值了呢

0

首先 __init__ 方法是构造方法,不需要显式调用。

__init__() 方法是一种特殊的方法,被称为类的构造函数或初始化方法,当创建了这个类的实例时就会调用该方法。

按照你的代码逻辑,执行 login(self) 方法的时候,如果输入参数 1,代表注册。menu_dic[option](self) 表达式替换为 Student.student_registered(self) 方法。

在看这个注册方法,

student_name = input("注册姓名:")
student_sex = input("性别:")
student_school = input("学校:")
student_class = input("班级:")
st1 = Student(student_name,student_sex,student_school,student_class)

input() 方法接收输入并赋给变量,然后执行 st1 = Student(student_name,student_sex,student_school,student_class),这里就是 new 了类 Student, 除去必须的 self 参数,正好四个实参,对应 __init__() 构造的四个形参,就会隐式调用 __init__() 构造。

这时候 st1 实例就已经有数据了,你可以用它来获取到传入的数据了。

student_dict["姓名"] = st1.student_name
student_dict["性别"] = st1.student_sex
student_dict["学校"] = st1.student_school
student_dict["班级"] = st1.student_classes

这部分就是从 st1 学生实例中获取四个数据构造了一个字典,然后执行最后的保存操作。