1
- 1,1,1
因为Child1和Child2的父类都是Parent,所以继承了Parent的x - 1,2,1
Child2不改变,Child1增加了自己的x,因为子类的优先级比父类高,所以Child1中x以自己的为准 3,2,3
Parent中x的值变为了3,Child1中的x值依旧为2,Child2无值以父类中的为准2
G,D,A,B,F,C,B,D,A
因为python3是新式类,所以以广度优先,因为A,B都是继承object,所以谁在最后面,谁就后打印3
继承object的类都是新式类
没有object的类就是经典类
深度优先:是先一个方向找,找到他的最终父类为止,在找下一个.
广度优先:是最后找object
4
import pickle
import hashlib
import time
class Class:
def __init__(self, name, sex, age):
self.name = name
self.age = age
self.sex = sex
class Teacher(Class):
def __init__(self, name, sex, age, level, salary):
super().__init__(name, sex, age)
self.level = level
self.salary = salary
def create_id(self):
now = time.strftime('%Y-%m-%d---%X')
info = self.name + self.sex + str(self.age) + str(self.level) + str(self.salary)
res = hashlib.md5((now + info).encode('utf-8'))
self.id = res.hexdigest()
return self.id
def tell_info(self):
print((f' Info of {self.name}').center(50, '-'))
print(f'''编号: {self.id}
姓名: {self.name}
性别: {self.sex}
年龄: {self.age}
等级: {self.level}
薪资: {self.salary}''')
def save(self):
with open(self.id, 'wb') as fw:
pickle.dump(self, fw)
def get_obj_by_id(self):
return pickle.load(open(self.id, 'rb'))
t1 = Teacher('渣渣辉', '蓝', 28, 999, 10000)
t1.create_id()
t1.tell_info()
t1.save()