代码:
#! /usr/bin/python # Filename: inherit.py # Author: yanggang class SchoolMember: def __init__(self,name,age): self.name = name self.age = age print 'init SchoolMember: ', self.name def tell(self): print 'name:%s; age:%s' % (self.name, self.age) class Teacher(SchoolMember): def __init__(self,name,age,salary): SchoolMember.__init__(self,name,age) self.salary = salary print 'init Teacher: ', self.name def tell(self): SchoolMember.tell(self) print 'salary: ', self.salary class Student(SchoolMember): def __init__(self,name,age,marks): SchoolMember.__init__(self,name,age) self.marks = marks print 'init Student: ', self.name def tell(self): SchoolMember.tell(self) print 'marks: ', self.marks t = Teacher('yanggang', 20, 1000) s = Student('liming', 12, 86) members = [t,s] print for member in members: member.tell()
结果:
[work@db-testing-com06-vm3.db01.baidu.com python]$ python inherit.py
init SchoolMember: yanggang
init Teacher: yanggang
init SchoolMember: liming
init Student: liming
name:yanggang; age:20
salary: 1000
name:liming; age:12
marks: 86
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索init
, name
, print
, age
, Self
__init__
python 类继承、python 类的继承、python 继承多个类、python 类继承 初始化、python 继承父类属性,以便于您获取更多的相关知识。
时间: 2024-11-05 12:22:57