Python implementation class inheritance instance

Source: Internet
Author: User

Python is an interpreted, object-oriented, and dynamic data-type high-level programming language. This document describes an example of Python class inheritance.

The instance code is as follows:

#! /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() 

Running result:

[work@db-testing.baidu.com python]$ python inherit.py init SchoolMember: yangganginit Teacher: yangganginit SchoolMember: liminginit Student: limingname:yanggang; age:20salary: 1000name:liming; age:12marks: 86

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.