学习对象:@丁七岁 https://blog.csdn.net/qq_43612538/article/details/105914720 1.创建School类,声明3个主属性 2.创建学生类Student,继承自School类的3个属性, 添加额外两个属性:班级class、学号s_no。 打印学生的所有信息print_info方法。 3.创建教师类Teacher,继承自School类, 添加额外两个属性:部门department、工号c_no。 添加方法:打印教师的所有信息print_info方法。 4.定义学生类、教师类的对象,然后分别调用print_info方法 实现各自对象属性信息的输出。
class School(object):
def __init__(self, name, sex, age):
self.name = name
self.sex = sex
self.age = age
class Student(School):
def __init__(self, name, sex, age, class_no, s_no):
super(Student, self).__init__(name, sex, age)
self.class_no = class_no
self.s_no = s_no
def print_info(self):
print("{}\t{}\t{}\t{}\t{}".format(self.name, self.sex, self.age, self.class_no, self.s_no))
class Teacher(School):
def __init__(self, name, sex, age, department, c_no):
super(Teacher, self).__init__(name, sex, age)
self.department = department
self.c_no = c_no
def print_info(self):
print("{}\t{}\t{}\t{}\t{}".format(self.name, self.sex, self.age, self.department, self.c_no))
print("name\tsex\tage\tclass/dep\tno")
s = Student("刘德华", "男", "18", "高三一班", "15")
s.print_info()
t = Teacher("马德华", "男", "29", "后勤部", "018410")
t.print_info()
运行结果:
name sex age class/dep no
刘德华 男 18 高三一班 15
马德华 男 29 后勤部 018410