圆月山庄资源网 Design By www.vgjia.com

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,类继承

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com