2018.10.23
1繼承介紹
‘‘‘
1. 什麽是繼承?
在程序中繼承是一種新建子類的方式,新創建的類稱之為子類\派生類,被繼承的類稱之為父類\基類\超類
繼承描述的是一種遺傳關系,子類可以重用父類的屬性
2. 為何用繼承?
減少類與類之間代碼冗余的問題
3. 如何繼承
先抽象再繼承
‘‘‘
# 在python中繼承的特點?單繼承&多繼承
# class Parent1(object):
# pass
#
# class Parent2:
# pass
#
# class Sub1(Parent1):
# pass
#
# class Sub2(Parent1,Parent2):
# pass
#
# # print(Sub1.__bases__)#繼承了誰會輸出
# # print(Sub2.__bases__)
#
# print(Parent1.__bases__)
# print(Parent2.__bases__)
# x=2
# class Foo:
# # x=1
# pass
# obj=Foo()
# # obj.x=3
# print(obj.x)
# python2與python3在繼承上的區別
# 新式類:但凡繼承object類的子類,以及該子類的子子類,...都稱之為新式類
# 經典類:沒有繼承object類的子類,以及該子類的子子類,...都稱之為經典類
# 只有在python2中才區分新式類與經典類
# 新式類與經典在屬性查找上的區別???
2 基於繼承解決類與類代碼冗余問題
# 在子類派生出的新功能中如何重用父類的功能:
# 方式一: 指名道姓地訪問某一個類中的函數,與繼承無關
class OldboyPeople:
school = ‘Oldboy‘
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
# print(OldboyPeople.__init__)
class OldboyStudent(OldboyPeople):
# def __init__(self, name, age, gender):
# self.name = name
# self.age = age
# self.gender = gender
def choose_course(self):
print(‘%s is choosing course‘ %self.name)
class OldboyTeacher(OldboyPeople):
# tea, ‘egon‘, 18, ‘male‘, 10, 3000
def __init__(self, name, age, gender,level,salary):
# self.name = name
# self.age = age
# self.gender = gender
OldboyPeople.__init__(self, name, age, gender)
self.level=level
self.salary=salary
def score(self,stu,num):
stu.num=num
print(‘老師%s給學生%s打分%s‘ %(self.name,stu.name,num))
# stu=OldboyStudent(‘kevin‘,38,‘male‘) #__init___(stu1,‘kevin‘,38,‘male‘)
# print(stu.__dict__)
tea=OldboyTeacher(‘egon‘,18,‘male‘,10,3000) #__init___(tea,‘egon‘,18,‘male‘,10,3000)
print(tea.__dict__)
# print(stu.school)
# print(tea.school)
3
# 在單繼承背景下,無論是新式類還是經典類屬性查找順序都一樣
# 先obj->類->父類->...
# class Foo:
# def f1(self):
# print(‘Foo.f1‘)
#
# def f2(self):
# print(‘Foo.f2‘)
# self.f1() #obj.f1()
#
# class Bar(Foo):
# def f1(self):
# print(‘Bar.f1‘)
#
# obj=Bar()
# obj.f2()
# 在多繼承背景下,如果一個子類繼承了多個分支,但是多個分支沒有匯聚到一個非object類,無論是新式類還是經典類屬性查找順序都一樣:
# 會按照從左到右的順序一個分支一個分支的查找下去
class E:
# xxx=‘E‘
pass
class F:
# xxx=‘F‘
pass
class B(E):
# xxx=‘B‘
pass
class C(F):
# xxx=‘C‘
pass
class D:
# xxx=‘D‘
pass
class A(B,C,D):
# xxx=‘A‘
pass
obj=A()
# obj.xxx=111
# print(obj.xxx)
print(A.mro())
# 在多繼承背景下,如果一個子類繼承了多個分支,但是多個分支最終匯聚到一個非object類(菱形繼承問題)
# 新式類:廣度優先查找:obj->A->B->E->C->F->D->G->object
# 經典類:深度優先查找:obj->A->B->E->G->C->F->D
# class G:
# xxx=‘G‘
#
# class E(G):
# xxx=‘E‘
# pass
#
# class F(G):
# xxx=‘F‘
# pass
#
# class B(E):
# xxx=‘B‘
# pass
#
# class C(F):
# xxx=‘C‘
# pass
#
# class D(G):
# xxx=‘D‘
# pass
#
# class A(B,C,D):
# xxx=‘A‘
# pass
#
#
# print(A.mro())
4 super方法
# 在子類派生出的新功能中如何重用父類的功能:
# 方式一: 指名道姓地訪問某一個類中的函數,與繼承無關
# 方式二: super(OldboyTeacher,self),在python3中super可以不傳參數,調用該函數會得到一個特殊的對象,該對象是專門用來訪問父類中屬性,
# 強調:super會嚴格參照類的mro列表依次查找屬性
# class OldboyPeople:
# school = ‘Oldboy‘
# def __init__(self, name, age, gender):
# self.name = name
# self.age = age
# self.gender = gender
#
# class OldboyTeacher(OldboyPeople):
# # tea, ‘egon‘, 18, ‘male‘, 10, 3000
# def __init__(self, name, age, gender,level,salary):
# OldboyPeople.__init__(self, name, age, gender)
# # super(OldboyTeacher,self).__init__(name, age, gender)
#
# self.level=level
# self.salary=salary
#
# def score(self,stu,num):
# stu.num=num
# print(‘老師%s給學生%s打分%s‘ %(self.name,stu.name,num))
#
# tea=OldboyTeacher(‘egon‘,18,‘male‘,10,3000) #__init___(tea,‘egon‘,18,‘male‘,10,3000)
# print(tea.__dict__)
# # print(stu.school)
#A沒有繼承B,
class A:
def test(self):
print(‘A.test‘)
super().test()
class B:
def test(self):
print(‘from B‘)
class C(A,B):
pass
c=C()
c.test()
print(C.mro())
2018.10.23