Python实现类继承实例
程序员文章站
2022-06-29 19:10:52
python是一种解释型、面向对象、动态数据类型的高级程序设计语言,本文就举一例python类继承的实例。
实例代码如下:
#! /usr/bin/python...
python是一种解释型、面向对象、动态数据类型的高级程序设计语言,本文就举一例python类继承的实例。
实例代码如下:
#! /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.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
上一篇: 为什么说姜茶暖胃,姜茶有哪些作用呢
下一篇: Python Deque 模块使用详解