學習對象:@丁七歲 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