# encoding=utf-8
import datetime
class PersonValueError(ValueError):
pass
class PersonTypeError(TypeError):
pass
class Person:
_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 date :", birthday)
self._name = name
self._sex = sex
self._birthday = birth
self._id = ident
Person._num += 1
def id(self,): return self._id
def name(self,): return self._name
def naem(self,): return self._sex
def birthday(self,): return self._birthday
def age(self,):
return datatime.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)))
p1 = Person("谢一姐", "男", (1991, 1, 21), "1201510111")
p2 = Person("谢二姐", "女", (1992, 2, 22), "1201510112")
p3 = Person("谢三姐", "男", (1993, 3, 23), "1201510113")
p4 = Person("谢四姐", "女", (1994, 4, 24), "1201510114")
for p in [p1, p2, p3, p4]:
print(p)
print(p.details())
print('total Person num ', Person.num())
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_date = datetime.date.today()
self._courses = {}
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 score(self,): return [(cname, self._courses[cname])
for cname in self._courses]
def details(self):
# 覆盖基类方法,但是又通过Person.details()方法使用使用基类方法
return ', '.join((Person.details(self),
'入学日期: ' + str(self._enroll_date),
'院系: ' + self._department,
'课程记录: ' + str(self.score())))
p1 = Student("学生一", "男", (1991, 1, 21), "机械学院")
p1.set_course('机械')
p1.set_score('机械', 33)
p2 = Student("学生二", "女", (1992, 2, 22), "计算机学院")
p2.set_course('计算机')
p2.set_score('计算机', 80)
p3 = Student("学生三", "男", (1993, 3, 23), "美术学院")
p2.set_course('美术')
p2.set_score('美术', 90)
p4 = Student("学生四", "女", (1994, 4, 24), "电子学院")
p4.set_course('电子')
p4.set_score('电子', 20)
for p in [p1, p2, p3, p4]:
print(p)
print(p.details())
print('total Student num ', Student.num())
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 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("校长", "男", (1971, 1, 21), (1991, 1, 21),)
p1.set_salary(10000)
p1.set_position('校长')
p1.set_department('校长办公室')
p2 = Staff("教师二", "女", (1980, 2, 22), (1991, 1, 21),)
p2.set_salary(10000)
p2.set_position('教师')
p2.set_department('机械')
p3 = Staff("教师三", "男", (1993, 3, 23),)
p3.set_salary(10000)
p3.set_position('教师')
p3.set_department('美术')
p4 = Staff("教师四", "女", (1994, 4, 24))
p4.set_salary(10000)
p4.set_position('教师')
p4.set_department('电子')
for p in [p1, p2, p3, p4]:
print(p)
print(p.details())
print('total Staff num ', Staff.num())
输出:
PS C:\Users\yanghe\Desktop> python .\test.py
1201510111 谢一姐 男 1991-01-21
编号: 1201510111, 姓名: 谢一姐, 性别: 男, 出生日期:1991-01-21
1201510112 谢二姐 女 1992-02-22
编号: 1201510112, 姓名: 谢二姐, 性别: 女, 出生日期:1992-02-22
1201510113 谢三姐 男 1993-03-23
编号: 1201510113, 姓名: 谢三姐, 性别: 男, 出生日期:1993-03-23
1201510114 谢四姐 女 1994-04-24
编号: 1201510114, 姓名: 谢四姐, 性别: 女, 出生日期:1994-04-24
total Person num 4
1201800001 学生一 男 1991-01-21
编号: 1201800001, 姓名: 学生一, 性别: 男, 出生日期:1991-01-21, 入学日期: 2018-10-20, 院系: 机械学院, 课程记录: [('机械', 33)]
1201800002 学生二 女 1992-02-22
编号: 1201800002, 姓名: 学生二, 性别: 女, 出生日期:1992-02-22, 入学日期: 2018-10-20, 院系: 计算机学院, 课程记录: [('计算机', 80), ('美术', 90)]
1201800003 学生三 男 1993-03-23
编号: 1201800003, 姓名: 学生三, 性别: 男, 出生日期:1993-03-23, 入学日期: 2018-10-20, 院系: 美术学院, 课程记录: []
1201800004 学生四 女 1994-04-24
编号: 1201800004, 姓名: 学生四, 性别: 女, 出生日期:1994-04-24, 入学日期: 2018-10-20, 院系: 电子学院, 课程记录: [('电子', 20)]
total Student num 8
0197100001 校长 男 1971-01-21
编号: 0197100001, 姓名: 校长, 性别: 男, 出生日期:1971-01-21, 入职日期: 1991-01-21, 院系: 校长办公室, 职位:校长, 工资:10000
0198000002 教师二 女 1980-02-22
编号: 0198000002, 姓名: 教师二, 性别: 女, 出生日期:1980-02-22, 入职日期: 1991-01-21, 院系: 机械, 职位:教师, 工资:10000
0199300003 教师三 男 1993-03-23
编号: 0199300003, 姓名: 教师三, 性别: 男, 出生日期:1993-03-23, 入职日期: 2018-10-20, 院系: 美术, 职位:教师, 工资:10000
0199400004 教师四 女 1994-04-24
编号: 0199400004, 姓名: 教师四, 性别: 女, 出生日期:1994-04-24, 入职日期: 2018-10-20, 院系: 电子, 职位:教师, 工资:10000
total Student num 12
PS C:\Users\yanghe\Desktop> python .\test.py
1201510111 谢一姐 男 1991-01-21
编号: 1201510111, 姓名: 谢一姐, 性别: 男, 出生日期:1991-01-21
1201510112 谢二姐 女 1992-02-22
编号: 1201510112, 姓名: 谢二姐, 性别: 女, 出生日期:1992-02-22
1201510113 谢三姐 男 1993-03-23
编号: 1201510113, 姓名: 谢三姐, 性别: 男, 出生日期:1993-03-23
1201510114 谢四姐 女 1994-04-24
编号: 1201510114, 姓名: 谢四姐, 性别: 女, 出生日期:1994-04-24
total Person num 4
1201800001 学生一 男 1991-01-21
编号: 1201800001, 姓名: 学生一, 性别: 男, 出生日期:1991-01-21, 入学日期: 2018-10-20, 院系: 机械学院, 课程记录: [('机械', 33)]
1201800002 学生二 女 1992-02-22
编号: 1201800002, 姓名: 学生二, 性别: 女, 出生日期:1992-02-22, 入学日期: 2018-10-20, 院系: 计算机学院, 课程记录: [('美术', 90), ('计算机', 80)]
1201800003 学生三 男 1993-03-23
编号: 1201800003, 姓名: 学生三, 性别: 男, 出生日期:1993-03-23, 入学日期: 2018-10-20, 院系: 美术学院, 课程记录: []
1201800004 学生四 女 1994-04-24
编号: 1201800004, 姓名: 学生四, 性别: 女, 出生日期:1994-04-24, 入学日期: 2018-10-20, 院系: 电子学院, 课程记录: [('电子', 20)]
total Student num 8
0197100001 校长 男 1971-01-21
编号: 0197100001, 姓名: 校长, 性别: 男, 出生日期:1971-01-21, 入职日期: 1991-01-21, 院系: 校长办公室, 职位:校长, 工资:10000
0198000002 教师二 女 1980-02-22
编号: 0198000002, 姓名: 教师二, 性别: 女, 出生日期:1980-02-22, 入职日期: 1991-01-21, 院系: 机械, 职位:教师, 工资:10000
0199300003 教师三 男 1993-03-23
编号: 0199300003, 姓名: 教师三, 性别: 男, 出生日期:1993-03-23, 入职日期: 2018-10-20, 院系: 美术, 职位:教师, 工资:10000
0199400004 教师四 女 1994-04-24
编号: 0199400004, 姓名: 教师四, 性别: 女, 出生日期:1994-04-24, 入职日期: 2018-10-20, 院系: 电子, 职位:教师, 工资:10000
total Staff num 12
网友评论