Python面向对象类继承和组合实例分析
本文实例讲述了Python面向对象类继承和组合。分享给大家供大家参考,具体如下:
在python3中所有类默认继承object,凡是继承了object的类都成为新式类,以及该子类的子类Python3中所有的类都是新式类,没有集成object类的子类成为经典类(在Python2中没有集成object的类以及它的子类都是经典类
继承式用来创建新的类的一种方式,好处是减少重复代码
classPeople: def__init__(self,name,age): self.name=name self.age=age defwalking(self): print('%siswalking'%self.name) deftalking(self): print('%sistalking'%self.name) classTeacher(People): pass classStudent(People): pass t1=Teacher('egon',18) print(t1.name,t1.age) t1.walking() t1.talking() s1=Student('xiaobai',22) print(s1.name,s1.age) s1.talking() s1.walking()
执行结果
egon18
egoniswalking
egonistalking
xiaobai22
xiaobaiistalking
xiaobaiiswalking
classPeople: def__init__(self,name,age,sex): self.name=name self.age=age self.sex=sex defwalking(self): print('%siswalking'%self.name) deftalking(self): print('%sistalking'%self.name) classTeacher(People): def__init__(self,name,age,sex,level,salary): People.__init__(name,age,sex) self.level=level self.salary=salary defteaching(self): People.talking(self) print('%sisteaching'%self.name) classStudent(People): def__init__(self,name,age,sex,group): People.__init__(name,age,sex) self.group=group defstudying(self): People.talking(self) print('%sisstudying'%self.name)
组合
classDate: def__init__(self,year,mon,day): self.year=year self.mon=mon self.day=day deftell_birth(self): print('出生于<%s>年<%s>月<%s>日'%(self.year,self.mon,self.day)) classTeacher: def__init__(self,name,age,sex,year,month,day): self.name=name self.age=age self.sex=sex self.birth=Date(year,month,day) defteaching(self): print('%sisteaching'%self.name) classStudent: def__init__(self,name,age,sex,year,mon,day): self.name=name self.age=age self.sex=sex self.birth=Date(year,mon,day) defstudying(self): print('%sisstudying'%self.name) xiaobai=Student('xiaobai',22,'male','1995','3','16') xiaobai.birth.tell_birth()
执行结果
出生于<1995>年<3>月<16>日
继承和组合
classPeople: def__init__(self,name,age,sex,year,mon,day): self.name=name self.age=age self.sex=sex self.birth=Date(year,mon,day) defwalking(self): print('%siswalking'%self.name) deftalking(self): print('%sistalking'%self.name) classDate: def__init__(self,year,mon,day): self.year=year self.mon=mon self.day=day deftell_birth(self): print('出生于<%s>年<%s>月<%s>日'%(self.year,self.mon,self.day)) classTeacher(People): def__init__(self,name,age,sex,level,salary,year,mon,day): People.__init__(self,name,age,sex,year,mon,day) self.level=level self.salary=salary defteaching(self): People.talking(self) print('%sisteaching'%self.name) classStudent(People): def__init__(self,name,age,sex,year,mon,day,group): People.__init__(self,name,age,sex,year,mon,day) self.group=group defstudying(self): People.talking(self) print('%sisstudying'%self.name)
父类要限制
1、子类必须有父类的方法
2、子类实现的方法必须跟父类的方法的名字一样
importabc classFile(metaclass=abc.ABCMeta): @abc.abstractclassmethod defread(self): pass @abc.abstractclassmethod defwrite(self): pass
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。