Python Inheritance and Composition interfaces

Source: Internet
Author: User

#解决代码重用的问题, reducing Code Redundancy # Inheritance is a relationship of what ' is 'classpeople:def __init__ (self, Name, age): # print ('people.__init__') Self.name=name Self.age=Age def walk (self): print ('%s is walking'%self.name)classTeacher (people): PassclassStudent (people): pass# t=teacher ('Egon', -) # Print (t.name,t.age) # print (t.__dict__) # T.walk () # s=student ('Alex', -) # Print (s.name,s.age) # S.walk () #====================classpeople:def __init__ (self, Name, age,sex): Self.name=name Self.age=Age Self.sex=Sex def Walk (self): print ('%s is walking'%self.name) def foo (self): print ('From father%s'%self.name)classTeacher (People): School='I love you.'#__init__ (T,'Egon', -,'male',Ten, the) def __init__ (self, Name, age,sex,level,salary): people.__init__ (Self,name,age,sex) #People. __init__ (T,'Egon', -,'male') #t. Name='Egon'#t. Age= -#t. Sex='male'Self.level=Level Self.salary=Salary def teach (self): print ('%s is teaching'%self.name) def foo (self): People.foo (self) print ('From teacher')classStudent (People): Def __init__ (self, Name, Age,sex,group): people.__init__ (self, name, age, sex) self . Group=Group def Study (self): print ('%s is studying'%self.name) T=teacher ('Egon', -,'male',Ten, the) #__init__ (T,'Egon', -,'male',Ten, the) T.foo () # #classteacher:# School='I love you.'# def __init__ (self, Name, age): # Self.name=name# self.age=age# def teach (self): # print ('%s is teaching'%self.name) # def walk (self): # print ('%s is walking'%self.name) # # #classstudent:# def __init__ (self, Name, age): # Self.name=name# self.age=age## def study (self): # print ('%s is studying'%self.name) # def walk (self): # print ('%s is walking'%self.name)
Inheritance
#继承是用来创建新的类的一种方式, the advantage is that you can reduce the number of duplicate code # Inheritance is the relationship between classes and classes, and what is a relationship # combination: What is a relationship and also to reduce duplication of codeclasspeople:def __init__ (self, name, age, year, Mon, day): Self.name=name Self.age=Age Self.birth=Date (year, Mon, day) def Walk (self): print ('%s is walking'%self.name)classdate:def __init__ (self,year,mon,day): Self.year=Year Self.mon=Mon self.day=Day def Tell_birth (self): print ('born in <%s> year <%s> month <%s> Day'%(self.year,self.mon,self.day))classTeacher (People): Def __init__ (self, name, age, year, Mon, day,level,salary): people.__init__ (Self,name,age, Year,mon,day) Self.level=Level Self.salary=Salary def teach (self): print ('%s is teaching'%self.name)classStudent (People): Def __init__ (self, name, age, year, Mon, Day,group): people.__init__ (Self,name,age,year,mo N,day) Self.group=Group def Study (self): print ('%s is studying'%self.name) # t=teacher ('Egon', -,1990,2, -) # Print (t.name,t.age) # print (T.birth) # T.birth.tell_birth () # Print (t.birth.year) # print (T.birth.mon) # Print ( T.birth.day)classcourse:def __init__ (self,name,price,period): Self.name=name Self.price=Price Self.period=period def tell_info (self): print ('Course name <%s> price <%s> period <%s>'%(Self.name,self.price,self.period)) #=====================================# classdate:# def __init__ (self,year,mon,day): # self.year=year# Self.mon=mon# Self.day=day## def Tell_birth (self): # print ('born in <%s> year <%s> month <%s> Day'%(Self.year,self.mon,self.day)) # #classteacher:# def __init__ (Self,name,age,birth): # self.name=name# self.age=age# Self.birth=birth## def teach (self): # print ('%s is teaching'%self.name) # # T_birth=date (1990,2, -) # t=teacher ('Egon', -, T_birth)
CombinationInterface

Python Inheritance and Composition interfaces

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.