标签:code lib ict obj load eve 没有 等级 优先
第一题:
class Parent(object):
x = 1
class Child1(Parent):
pass
class Child2(Parent):
pass
print(Parent.x, Child1.x, Child2.x)#由于前面只有在父类定义一个x,因此全部输出的都是1 #1 1 1
Child1.x = 2
print(Parent.x, Child1.x, Child2.x)#子类会优先从自己这边查找,因此child1输出的2 #1 2 1
Parent.x = 3
print(Parent.x, Child1.x, Child2.x)#当子类未被定义的时候,会去父类查找,因此child2输出的是3# 3 2 3
第二题:
[图片]
第三题
新式类:继承了object类以及该类的子类就是新式类了,而且python3中所有的都是新式类
经典类:相对的没有继承object类以及该类的子类就是经典类,只有再python2中有经典类
import hashlib
import time
import pickle
class teacher :
def __init__(self,name,gender,age,level,salary):
self.name=name
self.gender=gender
self.age=age
self.level=level
self.salary=salary
def create_id(self):
tempral=time.strftime('%Y/%M/%D---%X')
info=self.name+self.gender+str(self.age)+str(self.level)+str(self.salary)
res = hashlib.md5((tempral + info).encode('utf-8'))
self.id=res.hexdigest()
return self.id
def tell_info(self):
print('''
编号: %s
姓名: %s
性别: %s
年龄: %s
等级: %s
薪资: %s
''' % (
self.id, self.name, self.gender, 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'))
tea=teacher('Gary','male',20,5,10)
tea.create_id()
tea.tell_info()
tea.save()
res=tea.get_obj_by_id()
print(res)
class student:
def __init__(self, name, gender, age):
self.name = name
self.age = age
self.gender = gender
def create_id(self):
tempral = time.strftime('%Y/%M/%D---%X')
info=self.name+self.gender+str(self.age)
res = hashlib.md5((tempral + info).encode('utf-8'))
self.id=res.hexdigest()
return self.id
def tell_info(self):
print('''
编号: %s
姓名: %s
性别: %s
年龄: %s
''' % (
self.id, self.name, self.gender, 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'))
tea=student('Gary','male',20)
tea.create_id()
tea.tell_info()
tea.save()
res=tea.get_obj_by_id()
print(res)
import hashlib
import time
import pickle
class teacher :
def __init__(self,name,gender,age,level,salary):
self.name=name
self.gender=gender
self.age=age
self.level=level
self.salary=salary
def create_id(self):
tempral=time.strftime('%Y/%M/%D---%X')
info=self.name+self.gender+str(self.age)+str(self.level)+str(self.salary)
res = hashlib.md5((tempral + info).encode('utf-8'))
self.id=res.hexdigest()
return self.id
def tell_info(self):
print('''
编号: %s
姓名: %s
性别: %s
年龄: %s
等级: %s
薪资: %s
''' % (
self.id, self.name, self.gender, 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'))
tea=teacher('Gary','male',20,5,10)
tea.create_id()
tea.tell_info()
tea.save()
res=tea.get_obj_by_id()
print(res)
class student:
def __init__(self, name, gender, age):
self.name = name
self.age = age
self.gender = gender
def create_id(self):
tempral = time.strftime('%Y/%M/%D---%X')
info=self.name+self.gender+str(self.age)
res = hashlib.md5((tempral + info).encode('utf-8'))
self.id=res.hexdigest()
return self.id
def tell_info(self):
print('''
编号: %s
姓名: %s
性别: %s
年龄: %s
''' % (
self.id, self.name, self.gender, 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'))
tea=student('Gary','male',20)
tea.create_id()
tea.tell_info()
tea.save()
res=tea.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('gary', 'male', 18, 1, 1000)
s1 = Students('gary', 'male', 18)
t1.create_id()
t1.tell_info()
t1.save()
t1.get_obj_by_id()
print(t1.get_obj_by_id())
标签:code lib ict obj load eve 没有 等级 优先
原文地址:https://www.cnblogs.com/jimGraymane/p/11650590.html