Python implementation class inheritance instance _python

Source: Internet
Author: User
Tags inheritance

Python is a high-level programming language for interpretive, object-oriented, Dynamic Data types, and this article gives an example of a 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 ', M, 1000) 
s = Student (' Liming ', M,) members 
= [T,s] Print to member in 
Membe Rs: 
  

Run Result:

 [work@db-testing.baidu.com python]$ python inherit.py init schoolmember:yanggang init Te Acher:yanggang init schoolmember:liming init student:liming Name:yanggang; Age:20 salary:1000 name:liming; Age:12 marks:86 
Related Article

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.