作业
class Parent(object):
x = 1
class Child1(Parent):
pass
class Child2(Parent):
pass
print(Parent.x, Child1.x, Child2.x)
# 1 , 1 , 1
#子类继承父类,如果子类有则直接使用不管父类是什么,如果子类没有则去找父类中的内容
Child1.x = 2
print(Parent.x, Child1.x, Child2.x)
# 1,2,1
Parent.x = 3
#3,2,3
print(Parent.x, Child1.x, Child2.x)
2、多重继承的执行顺序,请解答以下输出结果是什么?并解释。
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
原因为在执行有相同基类的时候有其他子类不会去执行基类,需要等到执行完最后一个再执行基类
什么是新式类
新式类就是在继承object的类都称为新式类
什么是经典类
经典类就是凡是没有继承object类的都是经典类
二者的区别
区别就是一个经典类是python2的没有继承object
python3默认继承object类所以都是新式类
深度优先
深度优先就是直接继承到object类然后结束
广度优先
广度优先就是在到达有object父类时有其他子类有相同的父类object时不会执行到object,然后接着下一条类顺序进行
编写一个类的程序
用面向对象的形式编写一个老师类, 老师有特征:编号、姓名、性别、年龄、等级、工资,老师类中有功能。
1.生成老师唯一编号的功能,可以用hashlib对当前时间加上老师的所有信息进行校验得到一个hash值来作为老师的编号
def create_id(self):
pass
2.获取老师所有信息
def tell_info(self):
pass
3.将老师对象序列化保存到文件里,文件名即老师的编号,提示功能如下
def save(self):
with open('老师的编号','wb') as f:
pickle.dump(self,f)
4.从文件夹中取出存储老师对象的文件,然后反序列化出老师对象,提示功能如下
def get_obj_by_id(self,id):
return pickle.load(open(id,'rb'))
5、按照定义老师的方式,再定义一个学生类
6、抽象老师类与学生类得到父类,用继承的方式减少代码冗余
class School:
def __init__(self,name,number,sex,age,level,salary):
self.name = name
self.number = number
self.sex = sex
self.age = age
self.level = level
self.salary = salary
#定义老师类
class Teacher(School):
def only_number(self):
import hashlib
import datetime
teach_info = self.name + self.number + self.sex + self.age + self.level + self.salary
m = hashlib.md5()
time1 = str(datetime.datetime.now())
sum_info = time1 + teach_info
m.update(sum_info.encode('utf8'))
# print (m.hexdigest())
self.id = m.hexdigest()
return m.hexdigest()
def tell_info(self):
return self.name,self.number,self.sex,self.age,self.level,self.salary
def save(self):
import pickle
with open(f'{self.id}','wb') as fw:
pickle.dump(self,fw)
def get_obj_by_id(self):
import pickle
return pickle.load(open(self.id,'rb'))
a = Teacher('xiaoming','20','female','19','2','10000')
# print(a)
a.only_number()
a.save()
a.get_obj_by_id()
class Student(School):
def create_id(self):
import hashlib
import datetime
randtime = str(datetime.datetime.now())
sum_info = self.name + self.number + self.sex + self.age + self.level + self.salary + randtime
m = hashlib.md5()
m.update(sum_info.encode('utf8'))
self.id = m.hexdigest()
def tell_info(self):
return self.name,self.number,self.salary,self.level,self.sex,self.age
def save(self):
import pickle
with open(f'{self.id}','wb') as fw:
pickle.dump(self,fw)
def get_obj_by_id(self):
import pickle
return pickle.load(open(self.id,'rb'))
bb = Student('xiaoming','21','female','19','2','10000')
# print(a)
bb.create_id()
bb.save()
bb.get_obj_by_id()