1 1 1
1 2 1
3 2 3
- 从父类继承,自己没有的就可以到父类中找
- 私有的都是不可以访问的,但是本质上依然是改了名称放在这个属性所在类的_dict-中
- 继承时,公有的,子类和实例都可以随意访问,私有成员被隐藏了,子类和实例不可直接访问
- 当私有变量所在的类内的方法中可以访问这个私有变量
- 属性查找属性:实例的dict--》类的dict--》父类dict
G-D-A-B-F-C-B-D-A
先从G开始运行,第一个是G ,G中继承的父类是D和B,其中D中继承的父类是A,所以G-D-A-B,
然后从F运行,第一个是F,F中继承的父类是C,B,D,广度优先,最后是A
新式类
- 继承了object的类以及该类的子类,都是新式类
- Python3中所有的类都是新式类
经典类
- 没有继承object的类以及该类的子类,都是经典类
- 只有Python2中才有经典类
深度优先遍历不断深入图中并在栈中保存了所有分叉的顶点;广度优先遍历则像扇面一般扫描图,用一个队列保存访问过的最前端的顶点。深度遍历的方式是寻找离起点更远的顶点,只在碰到死胡同时才访问进出的顶点,广度遍历则会首先覆盖起点附近的顶点,只在临近的顶点都被访问完后,才去访问更远的顶点。
import pickle
import hashlib
import time
class Teacher:
def __init__(self, name, sex, age, level, salary):
self.name = name
self.age = age
self.sex = sex
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((' Info of %s ' % (self.name)).center(50, '-'))
print('''
编号: %s
姓名: %s
性别: %s
年龄: %s
等级: %s
薪资: %s
''' % (
self.id, self.name, self.sex, self.age, self.level, self.salary))
def save(self):
with open(self.id, 'wb') as f:
pickle.dump(self, f)
def get_obj_by_id(self):
return pickle.load(open(self.id, 'rb'))
t1 = Teacher('haonan', 'male', 18, 1, 1)
t1.create_id()
t1.tell_info()
t1.save()
res = t1.get_obj_by_id()
print(res)
class Student:
def __init__(self, name, sex, age):
self.name = name
self.age = age
self.sex = sex
def create_id(self):
now = time.strftime('%Y-%m-%d---%X')
info = self.name + self.sex + str(self.age)
res = hashlib.md5((now + info).encode('utf-8'))
self.id = res.hexdigest()
return self.id
def tell_info(self):
print((' Info of %s ' % (self.name)).center(50, '-'))
print('''
编号: %s
姓名: %s
性别: %s
年龄: %s
''' % (
self.id, self.name, self.sex, self.age))
def save(self):
with open(self.id, 'wb') as f:
pickle.dump(self, f)
def get_obj_by_id(self):
return pickle.load(open(self.id, 'rb'))
t2 = Student('wanghaonan', 'male', 18)
t2.create_id()
t2.tell_info()
t2.save()
res = t2.get_obj_by_id()
print(res)
import hashlib
import time
import pickle
class People:
def __init__(self, name, gender, age):
self.name = name
self.gender = gender
self.age = age
def create_id(self):
m = hashlib.md5()
t = str(time.time())
s = str(self.__dict__)
m.update(t.encode('utf-8'))
m.update(s.encode('utf-8'))
t1.num = m.hexdigest()
def tell_info(self):
print(self.__dict__)
def save(self):
with open(t1.num, 'wb') as f:
pickle.dump(self.__dict__, f)
def get_obj_by_id(self):
return pickle.load(open(self.num, 'rb'))
class Teacher(People):
def __init__(self, name, gender, age, level, salary):
People.__init__(self, name, gender, age)
self.level = level
self.salary = salary
class Students(People):
def __init__(self, name, gender, age):
People.__init__(self, name, gender, age)
t1 = Teacher('haonan', 'male', 18, 1, 1000)
s1 = Students('wanghaonan', 'male', 18)
t1.create_id()
t1.tell_info()
t1.save()
t1.get_obj_by_id()
print(t1.get_obj_by_id())