雪花在空中嬉戏着、飞舞着,它净化了世间的一切尘埃,送走了严冬的寂寞,它自由地来,潇洒地去,多少著名的诗词都赞美过它: "忽如一夜春风来,千树万树梨花开 ",多么俏丽呀! "瑞雪兆丰年 ",它还是丰收的预言家呢!
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实现类继承实例就介绍到这了。对待生命你不妨大胆冒险一点儿,因为好歹你要失去它。更多相关Python实现类继承实例内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!