没有合适的资源?快使用搜索试试~ 我知道了~
首页【Python】使用super()函数进行类的继承,将父类的方法和属性继承在子类的里。
学习对象:@丁七岁 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方法 实现各
资源详情
资源评论
资源推荐

【【Python】使用】使用super()函数进行类的继承,将父类的方法和函数进行类的继承,将父类的方法和
属性继承在子类的里。属性继承在子类的里。
学习对象:@丁七岁 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("{} {} {} {} {}".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("{} {} {} {} {}".format(self.name, self.sex, self.age, self.department, self.c_no))
print("name sex age class/dep no")
s = Student("刘德华", "男", "18", "高三一班", "15")
s.print_info()
t = Teacher("马德华", "男", "29", "后勤部", "018410")
t.print_info()
运行结果:
name sex age class/dep no
刘德华 男 18 高三一班 15
马德华 男 29 后勤部 018410
王怕怕升职记
原创文章 137获赞 14访问量 2万+
关注
私信
展开阅读全文


















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0