您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Python面向對象類繼承和組合的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
具體如下:
在python3中所有類默認繼承object,凡是繼承了object的類都成為新式類,以及該子類的子類Python3中所有的類都是新式類,沒有集成object類的子類成為經典類(在Python2中沒有集成object的類以及它的子類都是經典類
繼承式用來創建新的類的一種方式,好處是減少重復代碼
class People: def __init__(self,name,age): self.name=name self.age=age def walking(self): print('%s is walking ' %self.name) def talking(self): print('%s is talking '%self.name) class Teacher(People): pass class Student(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()
執行結果
egon 18
egon is walking
egon is talking
xiaobai 22
xiaobai is talking
xiaobai is walking
class People: def __init__(self,name,age,sex): self.name=name self.age=age self.sex=sex def walking(self): print('%s is walking ' %self.name) def talking(self): print('%s is talking '%self.name) class Teacher(People): def __init__(self,name,age,sex,level,salary): People.__init__(name,age,sex) self.level=level self.salary=salary def teaching(self): People.talking(self) print('%s is teaching'%self.name) class Student(People): def __init__(self,name,age,sex,group): People.__init__(name,age,sex) self.group=group def studying(self): People.talking(self) print('%s is studying'%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: def __init__(self,name,age,sex,year,month,day): self.name=name self.age=age self.sex=sex self.birth=Date(year,month,day) def teaching(self): print('%s is teaching'%self.name) class Student: def __init__(self,name,age,sex,year,mon,day): self.name=name self.age=age self.sex=sex self.birth=Date(year,mon,day) def studying(self): print('%s is studying'%self.name) xiaobai=Student('xiaobai',22,'male','1995','3','16') xiaobai.birth.tell_birth()
執行結果
出生于<1995>年 <3>月 <16>日
繼承和組合
class People: def __init__(self,name,age,sex,year,mon,day): self.name=name self.age=age self.sex=sex self.birth=Date(year,mon,day) def walking(self): print('%s is walking ' %self.name) def talking(self): print('%s is talking '%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,sex,level,salary,year,mon,day): People.__init__(self,name,age,sex,year,mon,day) self.level=level self.salary=salary def teaching(self): People.talking(self) print('%s is teaching'%self.name) class Student(People): def __init__(self,name,age,sex,year,mon,day,group): People.__init__(self,name,age,sex,year,mon,day) self.group=group def studying(self): People.talking(self) print('%s is studying'%self.name)
父類要限制
1、子類必須有父類的方法
2、子類實現的方法必須跟父類的方法的名字一樣
import abc class File(metaclass=abc.ABCMeta): @abc.abstractclassmethod def read(self): pass @abc.abstractclassmethod def write(self): pass
關于“Python面向對象類繼承和組合的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。