day-20 作业
day 20 作业
-
下面这段代码的输出结果将是什么?请解释。
class Parent(object): x = 1 class Child1(Parent): pass class Child2(Parent): pass print(Parent.x, Child1.x, Child2.x) Child1.x = 2 print(Parent.x, Child1.x, Child2.x) Parent.x = 3 print(Parent.x, Child1.x, Child2.x)
运行结果:
1 1 1 1 2 1 3 2 3
-
多重继承的执行顺序,请解答以下输出结果是什么?并解释。
class A(object): def __init__(self): print('A') super(A, self).__init__() class B(object): def __init__(self): print('B') super(B, self).__init__() class C(A): def __init__(self): print('C') super(C, self).__init__() class D(A): def __init__(self): print('D') super(D, self).__init__() class E(B, C): def __init__(self): print('E') super(E, self).__init__() class F(C, B, D): def __init__(self): print('F') super(F, self).__init__() class G(D, B): def __init__(self): print('G') super(G, self).__init__() if __name__ == '__main__': g = G() f = F()
运行结果:
G D A B F C B D A Process finished with exit code 0
-
什么是新式类,什么是经典类,二者有什么区别?什么是深度优先,什么是广度优先?
- 新式类:继承 object 的类以及该类的子类,python3 中所有的类都是新式类
- 经典类:没有继承 object 的类以及该类的子类,只有 python2 中才有经典类
区别:有没有继承 object
- 深度优先:按照继承顺序向上查找,一直找到最顶层的类
- 广度优先:按照继承顺序向上查找,最后查找 object
-
用面向对象的形式编写一个老师类, 老师有特征:编号、姓名、性别、年龄、等级、工资,老师类中有功能。
import hashlib import datetime import pickle class Teacher: def __init__(self, name, sex, age, level, salary, teacher_id=None): self.id = id self.name = name self.sex = sex self.age = age self.level = level self.salary = salary def func(self): pass
-
生成老师唯一编号的功能,可以用hashlib对当前时间加上老师的所有信息进行校验得到一个hash值来作为老师的编号
def creat_id(self): msg = f'{datetime.datetime.now()}+{self.name}+{self.sex}+{self.age}' self.teacher_id = hashlib.md5(msg.encode('utf-8')).hexdigest() return self.teacher_id
-
获取老师所有信息
def tell_info(self): for k, v in self.__dict__.items(): print(k.ljust(10), v)
-
将老师对象序列化保存到文件里,文件名即老师的编号,提示功能如下
def save(self): with open(f'{self.id}', 'wb') as fw: data = self.__dict__ pickle.dump(data, fw)
-
从文件夹中取出存储老师对象的文件,然后反序列化出老师对象,提示功能如下
def get_obj_by_id(self,id): with open(f'{self.id}', 'rb')as fr: data = pickle.load(fr) for k, v in data.items(): print(k.ljust(10), v)
-
按照定义老师的方式,再定义一个学生类
class Student: def __init__(self, name, sex, age, course=None, student_id=None): self.name = name self.student_id = student_id self.sex = sex self.age = age self.course = course def creat_id(self): msg = f'{datetime.datetime.now()}+{self.name}+{self.sex}+{self.age}' self.student_id = hashlib.md5(msg.encode('utf-8')).hexdigest() return self.student_id def choose_course(self, *args): self.course = args return self.course def tell_info(self): for k, v in self.__dict__.items(): print(k.ljust(15), v) def save(self): with open(f'{self.student_id}.pkl', 'wb') as fw: data = self.__dict__ pickle.dump(data, fw) def get_obj_by_id(self): with open(f'{self.student_id}.pkl', 'rb')as fr: data = pickle.load(fr) for k, v in data.items(): print(k.ljust(15), v)
分别实例化一个老师、一个学生对象:
# 实例化老师对象 teacher1 = Teacher('tom', 'male', 26, 5, 3000) teacher1_id = teacher1.creat_id() teacher1.tell_info() teacher1.save() teacher1.get_obj_by_id() # 实例化学生对象 student1 = Student('jack', 'male', 18) student1_id = student1.creat_id() student1_course = student1.choose_course('python','linux') student1.tell_info() student1.save() student1.get_obj_by_id()
-
抽象老师类与学生类得到父类,用继承的方式减少代码冗余
import hashlib import pickle import datetime class People(object): def __init__(self, name, sex, age, ): self.name = name self.sex = sex self.age = age def tell_info(self): for k, v in self.__dict__.items(): print(k.ljust(15), v) class Teacher(People): def __init__(self, name, sex, age, level, salary, teacher_id=None): super().__init__(name, sex, age) self.level = level self.salary = salary self.teacher_id = teacher_id def creat_id(self): msg = f'{datetime.datetime.now()}+{self.name}+{self.sex}+{self.age}' self.teacher_id = hashlib.md5(msg.encode('utf-8')).hexdigest() return self.teacher_id def save(self): with open(f'{self.teacher_id}.pkl', 'wb') as fw: data = self.__dict__ pickle.dump(data, fw) def get_obj_by_id(self): with open(f'{self.teacher_id}.pkl', 'rb')as fr: data = pickle.load(fr) for k, v in data.items(): print(k.ljust(15), v) class Student(People): def __init__(self, name, sex, age, student_id=None, course=None): super().__init__(name, sex, age) self.student_id = student_id self.course = course def creat_id(self): msg = f'{datetime.datetime.now()}+{self.name}+{self.sex}+{self.age}' self.student_id = hashlib.md5(msg.encode('utf-8')).hexdigest() return self.student_id def save(self): with open(f'{self.student_id}.pkl', 'wb') as fw: data = self.__dict__ pickle.dump(data, fw) def get_obj_by_id(self): with open(f'{self.student_id}.pkl', 'rb')as fr: data = pickle.load(fr) for k, v in data.items(): print(k.ljust(15), v) def choose_course(self, *args): self.course = args return self.course teacher2 = Teacher('alex', 'male', 30, 5, 40000) teacher2_id = teacher2.creat_id() teacher2.tell_info() teacher2.save() teacher2.get_obj_by_id() print('*' * 30) student2 = Student('Jane', 'femal', 18) student2_id = student2.creat_id() student2_course = student2.choose_course('linux', 'python') student2.tell_info() student2.save() student2.get_obj_by_id()
-