您好,登錄后才能下訂單哦!
#面向對象三大特性
#1.繼承
#2.封裝
#3.多態
'''
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.經典類,:指沒有繼承object類的類,以及該類的子類。
#2.新式類,:指繼承object類的類,以及該類的子類。
#在python3中統一都為新式類:
#例
#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()
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。