#面向對象三大特性
#1.繼承
#2.封裝
#3.多態(tài)
'''
1.繼承的定義:是一種新建類的方式,新建的類稱為子類,子類會遺傳父類的屬性,可以減少代碼冗余。
在python中,子類(派生類)可以繼承一個或者多個父類(基類,超類)
'''
#繼承的寫法
#class Parent1:
# pass
#class Parent2:
# pass
#class Sub1(Parent1):
# pass
#class Sub2(Parent1,Parent2):
# pass
#print(Parent1.__bases__) #查看子類繼承了那些父類
#print(Sub1.__bases__) #查看子類繼承了那些父類
#print(Sub2.__bases__) #查看子類繼承了那些父類
#在python2中類分為2種:
#1.經(jīng)典類,:指沒有繼承object類的類,以及該類的子類。
#2.新式類,:指繼承object類的類,以及該類的子類。
#在python3中統(tǒng)一都為新式類:
#例
#class olstu:
# sch='oldboy'
# def __init__(self,name,age,sex):
# self.name=name
# self.age=age
# self.sex=sex
# def learn(self):
# print('%s is learing' %self.name)
#class olteach:
# sch='oldboy'
# def __init__(self,name,age,sex):
# self.name=name
# self.age=age
# self.sex=sex
# def tech(self):
# print('%s is teachring' % self.name)
#使用繼承(效果同上)
class olper:
sch = 'oldboy'
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def tell__info(self):
print('名字:<%s> 年齡:<%s> 性別:<%s>' %(self.name,self.age,self.sex))
class olstu(olper):
def learn(self):
print('%s is learing' % self.name)
def tell__info(self):
print('wo is stu',end='')
print('名字:<%s> 年齡:<%s> 性別:<%s>' %(self.name,self.age,self.sex))
class olteach(olper):
def tech(self):
print('%s is teachring' % self.name)
#對象屬相的查找順序,先找在自己的名稱空間找,找不到再去類的名稱空間找,找不到再去父類的名稱空間找。在哪兒找到,將不會去下個地方查找。
stu1=olstu('sut',18,'ser')
tea1=olteach('tea',18,'sse')
#print(stu1.__dict__)
#print(stu1.sch)
stu1.tell__info()
tea1.tell__info()
#屬性查找
class foo:
def f1(self):
print('from foo.f1')
def f2(self): #self=obj
print('from foo.f2')
self.f1() #obj.f1()
class bar(foo):
def f1(self):
print('from bar.f1')
obj=bar()
obj.f2()
#子類重用父類的方法
#class olper:
# sch = 'oldboy'
# def __init__(self, name, age, sex):
# self.name = name
# self.age = age
# self.sex = sex
# def tell__info(self):
# print('名字:<%s> 年齡:<%s> 性別:<%s>' %(self.name,self.age,self.sex))
#class olstu(olper):
# def learn(self):
# print('%s is learing' % self.name)
# def tell__info(self):
# print('wo is stu',end='')
# olper.tell__info(self)
#stu1=olstu('sut',18,'ser')
#stu1.tell__info()
#
class olper:
sch = 'oldboy'
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def tell__info(self):
print('名字:<%s> 年齡:<%s> 性別:<%s>' %(self.name,self.age,self.sex))
class olstu(olper):
def __init__(self,name,age,sex,work):
#self.name=name
#self.age=age
#self.sex=sex
olper.__init__(self,name,age,sex)
self.work=work
def learn(self):
print('%s is learing' % self.name)
def tell__info(self):
print('wo is stu:',end='')
olper.tell__info(self)
stu1=olstu('sut',18,'ser','ss')
stu1.tell__info()
#子類重用父類的方法之super(),super安照繼承原理進行查找。
class olper:
sch = 'oldboy'
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def tell__info(self):
print('名字:<%s> 年齡:<%s> 性別:<%s>' %(self.name,self.age,self.sex))
class olstu(olper):
def __init__(self,name,age,sex,work):
#self.name=name
#self.age=age
#self.sex=sex
super().__init__(name,age,sex)
self.work=work
def tell__info(self):
print('i is stu' ,end='')
# olper.tell__info(self)
super().tell__info()
stu1=olstu('hu',15,'sdf','python')
stu1.tell__info()
#super方法二
class Foo:
def f2(self):
print('=====')
def f1(self):
print('foo.f1')
super().f2()
class Bar:
def f2(self):
print('Bar.f2')
class Sub(Foo,Bar):
pass
s=Sub()
print(Sub.mro())
s.f1()
#組合
#class olper:
# sch = 'oldboy'
# def __init__(self, name, age, sex):
# self.name = name
# self.age = age
# self.sex = sex
# def tell__info(self):
# print('名字:<%s> 年齡:<%s> 性別:<%s>' %(self.name,self.age,self.sex))
#class olstu(olper):
# def __init__(self,name,age,sex,work,st_id,year,day):
# #self.name=name
# #self.age=age
# #self.sex=sex
# olper.__init__(self,name,age,sex)
# self.work=work
# self.st_id=st_id
# self.year=year
# self.day = day
# def learn(self):
# print('%s is learing' % self.name)
# def tell__info(self):
# print('wo is stu:',end='')
# olper.tell__info(self)
# def tell_birth(self):
# print('年日:《%s-%s》' %(self.year,self.day))
#class olteach(olper):
# def __init__(self,name,age,sex,level,salary,year,day):
# olper.__init__(self,name,age,sex)
# self.level=level
# self.salary=salary
# self.year=year
# self.day=day
# def tech(self):
# print('%s is teachring' % self.name)
# def tell_birth(self):
# print('年日:《%s-%s》' %(self.year,self.day))
# def tell__info(self):
# print('wo is teach:',end='')
# olper.tell__info(self)
#stu1=olstu('sut',18,'ser','ss',1,1995,9)
#teac1=olteach('tea',18,'tea',8,4000,1985,2)
#stu1.tell_birth()
#teac1.tell_birth()
#改寫如下
class olper:
sch = 'oldboy'
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def tell__info(self):
print('名字:<%s> 年齡:<%s> 性別:<%s>' %(self.name,self.age,self.sex))
class olstu(olper):
def __init__(self,name,age,sex,work,st_id):
#self.name=name
#self.age=age
#self.sex=sex
olper.__init__(self,name,age,sex)
self.work=work
self.st_id=st_id
def learn(self):
print('%s is learing' % self.name)
def tell__info(self):
print('wo is stu:',end='')
olper.tell__info(self)
class olteach(olper):
def __init__(self,name,age,sex,level,salary):
olper.__init__(self,name,age,sex)
self.level=level
self.salary=salary
def tech(self):
print('%s is teachring' % self.name)
def tell__info(self):
print('wo is teach:',end='')
olper.tell__info(self)
class Date:
def __init__(self,year,day):
self.year=year
self.day=day
def tell_birth(self):
print('年日:《%s-%s》' % (self.year, self.day))
stu1=olstu('sut',18,'ser','ss',1)
date_obj1=Date(1995,9)
stu1.birth=date_obj1
teac1=olteach('tea',18,'tea',8,4000)
date_obj2=Date(1985,9)
teac1.birth=date_obj2
#print(stu1.birth)
#print(teac1.birth)
stu1.birth.tell_birth()
teac1.birth.tell_birth()
#組合的使用
#class olper:
# sch = 'oldboy'
# def __init__(self, name, age, sex, date_obj):
# self.name = name
# self.age = age
# self.sex = sex
# self.brith=date_obj
# def tell__info(self):
# print('名字:<%s> 年齡:<%s> 性別:<%s>' %(self.name,self.age,self.sex))
#class olstu(olper):
# def __init__(self,name,age,sex,work,st_id,date_obj):
#self.name=name
#self.age=age
#self.sex=sex
# olper.__init__(self,name,age,sex,date_obj)
# self.work=work
# self.st_id=st_id
# def learn(self):
# print('%s is learing' % self.name)
# def tell__info(self):
# print('wo is stu:',end='')
# olper.tell__info(self)
#class olteach(olper):
# def __init__(self,name,age,sex,level,salary,date_obj):
# olper.__init__(self,name,age,sex,date_obj)
# self.level=level
# self.salary=salary
# def tech(self):
# print('%s is teachring' % self.name)
# def tell__info(self):
# print('wo is teach:',end='')
# olper.tell__info(self)
#class oldxiao(olper):
# def __init__(self,name,age,sex,key,date_obj):
# olper.__init__(self,name,age,sex,date_obj)
# self.key=key
# def tell_info(self):
# print('xiaoshou',end='')
# olper.tell__info(self)
#class Date:
# def __init__(self,year,day):
# self.year=year
# self.day=day
# def tell_birth(self):
# print('年日:《%s-%s》' % (self.year, self.day))
#date_obj=Date(1995,5)
#salte1=oldxiao('可可',45,'salce',73,date_obj)
#salte1.brith=date_obj
#salte1.tell__info()
#salte1.brith.tell_birth()
class olper:
sch = 'oldboy'
def __init__(self, name, age, sex, date_obj):
self.name = name
self.age = age
self.sex = sex
self.brith=date_obj
def tell__info(self):
print('名字:<%s> 年齡:<%s> 性別:<%s>' %(self.name,self.age,self.sex))
class olstu(olper):
def __init__(self,name,age,sex,work,st_id,date_obj):
#self.name=name
#self.age=age
#self.sex=sex
olper.__init__(self,name,age,sex,date_obj)
self.work=work
self.st_id=st_id
def learn(self):
print('%s is learing' % self.name)
def tell__info(self):
print('wo is stu:',end='')
olper.tell__info(self)
class olteach(olper):
def __init__(self,name,age,sex,level,salary,date_obj):
olper.__init__(self,name,age,sex,date_obj)
self.level=level
self.salary=salary
self.caoure=[]
def tech(self):
print('%s is teachring' % self.name)
def tell__info(self):
print('wo is teach:',end='')
olper.tell__info(self)
class oldxiao(olper):
def __init__(self,name,age,sex,key,date_obj):
olper.__init__(self,name,age,sex,date_obj)
self.key=key
def tell_info(self):
print('xiaoshou',end='')
olper.tell__info(self)
class Date:
def __init__(self,year,day):
self.year=year
self.day=day
def tell_birth(self):
print('年日:《%s-%s》' % (self.year, self.day))
class caoure:
def __init__(self,name,precei,ptryird):
self.name=name
self.precei=precei
self.ptryird=ptryird
def tell_info(self):
print('詳細信息:《%s,%s,%s》' %(self.name,self.precei,self.ptryird))
pytth=caoure('pythonsji',4566,'sf')
linux=caoure('linuxi',4566,'iii')
date_obj=Date(1998,9)
tech2=olteach('laohs',34,'ss',34,1000,date_obj)
tech2.caoure.append(pytth)
tech2.caoure.append(linux)
#print(tech2.caoure)
for i in tech2.caoure:
i.tell_info()
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
成都創(chuàng)新互聯(lián)是一家專業(yè)提供金秀企業(yè)網(wǎng)站建設,專注與網(wǎng)站設計制作、成都做網(wǎng)站、H5高端網(wǎng)站建設、小程序制作等業(yè)務。10年已為金秀眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進行中。
新聞名稱:面向對象三大特性-創(chuàng)新互聯(lián)
標題路徑:http://m.rwnh.cn/article22/doiocc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿網(wǎng)站建設、微信公眾號、用戶體驗、標簽優(yōu)化、網(wǎng)站改版、建站公司
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容