欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

Python实现类继承实例

程序员文章站 2022-05-06 17:41:46
...
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实现类继承实例

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频


网友评论

文明上网理性发言,请遵守 新闻评论服务协议

我要评论
  • Python实现类继承实例
  • 专题推荐

    相关标签: Python 类继承