""" 创建学生类Student,继承自SchoolPerson类, 添加额外两个属性:班级class、学号sno。 添加方法:打印学生的所有信息print_info方法。 创建教师类Teacher,继承自SchoolPerson类, 添加额外两个属性:部门department、工号cno。 添加方法:打印教师的所有信息print_info方法。 定义学生类、教师类的对象,然后分别调用print_info方法 实现各自对象属性信息的输出。 """
class SchoolPerson(object):
def __init__(self, name, sex, age):
self.name = name
self.sex = sex
self.age = age
print(self.name + "执行了__init__()方法~~")
def set_info(self, name, sex, age):
self.name = name
self.sex = sex
self.age = age
def get_info(self):
return self.name + " " + self.sex + " " + self.age
def __del__(self):
print(self.name + "执行了__del__方法~~~")
class Student(SchoolPerson):
def __init__(self, name, sex, age, class0, sno):
super(Student, self).__init__(name, sex, age)
self.class0 = class0
self.sno = sno
def print_info(self):
print(self.name + " " + self.sex + " " + self.age + " " + self.class0 + " " + self.sno)
class Teacher(SchoolPerson):
def __init__(self, name, sex, age, department, cno):
super(Teacher, self).__init__(name, sex, age)
self.department = department
self.cno = cno
def print_info(self):
print(self.name + " " + self.sex + " " + self.age + " " + self.department + " " + self.cno)
s1 = Student("赵三", "男", "19", "1班", "123456789")
s1.print_info()
t1 = Teacher("李四", "男", "29", "教学部", "123456987")
t1.print_info()