Instances of Python inheritance

Source: Internet
Author: User

class Schoolmember (object): #定义学校
member=0# Default member is 0
amount=0# Default tuition fee is 0 yuan
def __init__ (self,name,age,sex): #构造函数, defining the properties of the parent class
Self.name=name
Self.age=age
Self.sex=sex
self.enroll () #调用注册的函数

def enroll (self):
' Register '
print ("Just is enrolled a new school member[%s]"% self.name)
schoolmember.member+=1# Each member of the register, the number of members plus 1
def tell (self): #用来获取成员的属性
Print ("-----info:%s----"%self.name)
For k,v in Self.__dict__.items (): #用字典的形式来获取成员的属性
print ("\ T", k,v)
print ("------End-------")
class Teacher (schoolmember): #定义一个老师的类, and inherit this school
def __init__ (self,name,age,sex,salary,course): #继承父类并且重构
schoolmember.__init__ (self,name,age,sex) #继承父类
self.salary=salary
Self.course=course
def Teaching (self):
print ("The teacher is [%s] and he course is [%s]"% (self.name,self.course))

class Student (schoolmember): #定义一个学生的类来继承父类
def __init__ (self,name,age,sex,course,tuition):
schoolmember.__init__ (self,name,age,sex)
Self.course=course
self.tuition=tuition
def pay_tuition (self,amount): #学费
print ("Student%s has justed paied%s"% (Self.name,amount))
Self.amount+=amount


t1=teacher ("Xiangshucai", Si, "F", 4500, "physical")
s1=student ("Xiangxiao", "F", "Python", 6500)
s2=student ("Liuhaimei", 23°c, "F", "Python15", 11000)
t1.teaching ()
T1.tell ()
S1.tell ()
The results are as follows:

Instances of Python inheritance

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.