import datetime
# TypeError不同类型之间的无效操作
class PersonTypeError(TypeError):
pass
class PersonValueError(TypeError):
pass
# 公共人员类
class Person(object):
_num = 0
def __init__(self, name, sex, birthday, ident):
if not (isinstance(name, str) and
sex in ('男', '女')):
raise PersonValueError(name, sex)
try:
birth = datetime.date(*birthday)
except:
raise PersonValueError('Wrong data:', birthday)
self._name = name
self._sex = sex
self._birthday = birthday
self._id = ident
Person._num += 1
def id(self): return self._id # 取得该人员记录中的人员编号
def name(self): return self._name # 取得该人员记录中的姓名
def sex(self): return self._sex # 取得该人员记录中的性别
def birthday(self): return self._birthday # 取得该人员记录中的出生年月日
def age(self): return (datetime.date.today().year -
self._birthday.year) # 取得该人员的年龄
def set_name(self,name): # 修改人员名字
if not isinstance(name,str):
raise PersonValueError('set_name:',name)
self.name = name
def __lt__(self,another): # 基于人员编号比较两个记录
if not isinstance(another,Person):
raise PersonTypeError(another)
return self._id < another._id
@classmethod
def num(cls): return Person._num
def __str__(self):
return ' '.join((self._id, self._name,
self._sex, str(self._birthday)))
def details(self): # 给出人员记录里保存的数据详情
return ', '.join(('编号: ' + self._id,
'姓名: ' + self._name,
'性别: ' + self._sex,
'出生日期: ' + str(self._birthday)))
# 学生类
class Student(Person):
_id_num = 0
@classmethod
def _id_gen(cls): # 实现学号生成规则
cls._id_num += 1
year = datetime.date.today().year
return '1{:04}{:05}'.format(year, cls._id_num)
def __init__(self, name, sex, birthday, department):
Person.__init__(self, name, sex, birthday,
Student._id_gen())
self._department = department
self._enroll_data = datetime.date.today()
self._courses = {} # 一个空字典
def department(self): return self._department # 取得学生所属院系
def en_year(self): return self._enroll_data # 取得学生的入学年度
def set_course(self, course_name): # 设置选课
self._courses[course_name] = None
def set_score(self, course_name, score): # 设置课程成绩
if course_name not in self._courses: # 这里假设必须先选课,最后才能设定课程成绩
raise PersonValueError('No this course selected:', course_name)
self._courses[course_name] = score
def scores(self): return [(cname, self._courses[cname])
for cname in self._courses] # 取得学生成绩单
def details(self):
return ", ".join((Person.details(self),
'入学日期:' + str(self._enroll_data),
'院系: ' + self._department,
'课程记录: ' + str(self.scores())))
# 教职工类
class Staff(Person):
_id_num = 0
@classmethod
def _id_gen(cls, birthday):
cls._id_num += 1
birth_year = datetime.date(*birthday).year
return '0{:04}{:05}'.format(birth_year, cls._id_num)
def __init__(self, name, sex, birthday, entry_date=None):
super().__init__(name, sex, birthday,
Staff._id_gen(birthday))
if entry_date:
try:
self._entry_date = datetime.date(*entry_date)
except:
raise PersonValueError('Wrong date: ', entry_date)
else:
self._entry_date = datetime.date.today()
self._salary = 1720 # 默认设为最低工资,可修改
self._department = '未定' # 需要另行设定
self._position = '未定' # 需要另行设定
def salary(self): return self._salary # 取得教职工的工资额
def department(self): return self._department # 取得教职工的职位
def position(self): return self._position # 取得教职工的所属院系
def set_salary(self, amount): # 设置教职工的工资额
if not type(amount) is int:
raise TypeError
self._salary = amount
def set_position(self, position): # 设置教职工的职位
self._position = position
def set_department(self, department): # 设置教职工的所属院系
self._department = department
def details(self):
return ", ".join((super().details(),
'入职日期:' + str(self._entry_date),
'院系:' + self._department,
'职位:' + self._position,
'工资:' + str(self._salary)))
p1 = Staff('路飞', '男', (1998, 3, 7))
p2 = Staff('女帝', '女', (1998, 4, 21))
p3 = Student('香吉士', '男', (1998, 8, 17),'云计算系')
plist2 = [p1, p2, p3]
for p in plist2:
print(p)
print('\nAfter sorting:')
p1.set_department('数学')
p1.set_position('副教授')
p1.set_salary(8400)
p3.set_course('python基础')
p3.set_score('python基础', 90)
p3.set_course('数据结构与算法')
p3.set_score('数据结构与算法', 99)
plist2.sort()
for p in plist2:
print(p.details())
print('People created:', Person.num(), '\n')
网友评论