Python 繼承和組合 接口
阿新 • • 發佈:2017-08-15
evel init 接口 elf () hid col sel div
#解決代碼重用的問題,減少代碼冗余 #繼承是一種什麽‘是’什麽的關系 class People: def __init__(self, name, age): # print(‘People.__init__‘) self.name = name self.age = age def walk(self): print(‘%s is walking‘ %self.name) class Teacher(People): pass class Student(People): pass # t繼承=Teacher(‘egon‘,18) # print(t.name,t.age) # print(t.__dict__) # t.walk() # s=Student(‘alex‘,13) # print(s.name,s.age) # s.walk() #==================== class People: 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) class Teacher(People): school = ‘偶的博愛‘ #__init__(t,‘egon‘,18,‘male‘,10,3000) def __init__(self, name, age,sex,level,salary): People.__init__(self,name,age,sex) #People.__init__(t,‘egon‘,18,‘male‘) #t.name=‘egon‘ #t.age=18 #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‘) class Student(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‘,18,‘male‘,10,3000) #__init__(t,‘egon‘,18,‘male‘,10,3000) t.foo() # # class Teacher: # school = ‘偶的博愛‘ # 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) # # # class Student: # 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)
#繼承是用來創建新的類的一種方式,好處是可以減少重復代碼 #繼承是類與類之間的關系,是一種什麽是什麽的關系 #組合:是一種什麽有什麽的關系,也是為了減少重復代碼 class People: 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) class Date: def __init__(self,year,mon,day): self.year=year self.mon=mon self.day=day def tell_birth(self): print(‘出生於<%s>年 <%s>月 <%s>日‘ % (self.year,self.mon,self.day)) class Teacher(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) class Student(People): def __init__(self, name, age, year, mon, day,group): People.__init__(self,name,age,year,mon,day) self.group=group def study(self): print(‘%s is studying‘ %self.name) # t=Teacher(‘egon‘,18,1990,2,33) # print(t.name,t.age) # print(t.birth) # t.birth.tell_birth() # print(t.birth.year) # print(t.birth.mon) # print(t.birth.day) class Course: def __init__(self,name,price,period): self.name=name self.price=price self.period=period def tell_info(self): print(‘課程名<%s> 價錢<%s> 周期<%s>‘ %(self.name,self.price,self.period)) #===================================== # class Date: # def __init__(self,year,mon,day): # self.year=year # self.mon=mon # self.day=day # # def tell_birth(self): # print(‘出生於<%s>年 <%s>月 <%s>日‘ % (self.year,self.mon,self.day)) # # class Teacher: # 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,33) # t=Teacher(‘egon‘,18,t_birth)組合 接口
Python 繼承和組合 接口