python基础之继承原理,多态与封装,,1.什么是继承? 继
python基础之继承原理,多态与封装,,1.什么是继承? 继
1.什么是继承? 继承是一种创建新的类的方式。
class A:
pass
class B:
pass
2.如何继承---->如何寻找继承关系
现实生活中找继承关系是自下而上,在程序中写是自上而下
继承是一种"是"的关系:
人类、猪类、狗类都是继承动物类,因为他们都是动物
3.为什么要用继承?
解决代码重用问题?解决的是:什么是什么的问题-->继承
4.派生:子类继承了父类的属性,然后衍生(或派生)出自己新的属性
如果子类衍生出的新的属性与父类的某个属性名字相同
那么再调用子类的这个属性,就以子类自己这里的为准了
5.在子类中重用父类的函数,父类名.父类的函数(参数)
6.组合对继承来说,也是用来重用代码,但是组合描述的是一种"有"的关系
老师有课程
学生有成绩
学生有课程
学校有老师
学校有学生
class Course:def __init__(self,name,price,period):self.name=nameself.price=priceself.period=periodclass Teacher:def __init__(name.course):self.name=nameself.course=courseclass Student:def __init__(name.course):self.name=nameself.course=coursepython=Course(‘python‘,15800,‘7m‘)t1=Teacher(‘egon‘,python)s1=Student(‘alex‘,python)print(s1.course.name)print(s1.course.period)组合示例
继承原理1(python3中的新式类):
#新式类的继承,在查找属性时遵循:广度优先class A(object): #A继承object类,在Python3中写不写都一样 def test(self): print(‘from A‘) passclass X(A): # def test(self): # print(‘from X‘) passclass B(X): # def test(self): # print(‘from B‘) passclass C(A): def test(self): print(‘from C‘) passclass D(B): # def test(self): # print(‘from D‘) passclass E(C): def test(self): print(‘from E‘) passclass F(D,E): # def test(self): # print(‘from F‘) #从F自己这里找 passf1=F()f1.test()print(F.mro())#MRO程序列表会生成一个列表#python到底是如何实现继承的,对于定义的每一个类,python会计算出一个方法解析顺序(MRO)列表,这个MRO列表就是一个简单的所有基类的线性顺序列表。继承原理示例1-1
#广度优先:F->D->B->E->C->A->object
#广度优先:从左边开始走,但是不走到头,再从右边开始走
class A(object): def test(self): print(‘from A‘) passclass B(A): # def test(self): # print(‘from B‘) passclass C(A): # def test(self): # print(‘from C‘) passclass D(B): # def test(self): # print(‘from D‘) passclass E(C): # def test(self): # print(‘from E‘) passclass F(D,E): # def test(self): # print(‘from F‘) passf1=F()f1.test() #先在f1找,没有,然后再F找print(F.__mro__) #只有新式才有这个属性可以查看线性列表,经典类没有这个属性继承原理示例1-2
继承原理2(python3中的新式类):
# Auther:bing#新式类的继承,在查找属性时遵循:广度优先class X(object): def test(self): print(‘from X‘) passclass Y(object): def test(self): print(‘from Y‘) passclass B(X): def test(self): print(‘from B‘) passclass C(Y): def test(self): print(‘from C‘) passclass D(B): def test(self): print(‘from D‘) passclass E(C): def test(self): print(‘from E‘) passclass F(D,E): def test(self): print(‘from F‘) passf1=F()f1.test()#深度优先#F-->D-->B-->X-->E-->C-->Y-->object,先从左边开始找,再找右边,相当于一个V形继承原理2
继承原理图2,深度优先
#F-->D-->B-->X-->E-->C-->Y-->object,先从左边开始找,再找右边,相当于一个V形
继承原理3(python3中的新式类):
class A(object): def test(self): print(‘from A‘) passclass B(A): def test(self): print(‘from B‘) passclass C(A): def test(self): print(‘from C‘) passclass D(A): def test(self): print(‘from D‘) passclass E(B): def test(self): print(‘from E‘) passclass F(C): def test(self): print(‘from F‘) passclass G(D): def test(self): print(‘from G‘) passclass H(E,F,G): def test(self): print(‘from H‘) passh1=H()h1.test()#广度优先继承原理3
#执行顺序是E-B-F-C-G-D-A-object,先从左边,再从中间在从右边找
#经典类的继承class A: def test(self): print(‘from A‘) passclass B(A): def test(self): print(‘from B‘) passclass C(A): def test(self): print(‘from C‘) passclass D(B): def test(self): print(‘from D‘) passclass E(C): def test(self): print(‘from E‘) passclass F(D,E): def test(self): print(‘from F‘) passf1=F()f1.test()# print(F.__mro__)#MRO程序列表会生成一个列表#python到底是如何实现继承的,对于定义的每一个类,python会计算出一个方法解析顺序(MRO)列表,这个MRO列表就是一个简单的所有基类的线性顺序列表。#F-D-B-A-E-C-objectpython2中经典类的继承
#F-D-B-A-E-C-object
super用法:
#super在Python3中的用法:class People: def __init__(self,name,sex,age): self.name=name self.sex=sex self.age=age def walk(self): print(‘%s is walking‘ %self.name)class Chinese(People): country=‘China‘ def __init__(self,name,sex,age,language=‘Chinese‘): # self.name=name # self.sex=sex # self.age=age # People.__init__(self,name,sex,age) super(Chinese,self).__init__(name,sex,age) #调用父类(People类)的__init__方法(绑定方法) self.language=language def walk(self,x): super().walk() print(‘子类的x‘,x)c=Chinese(‘egon‘,‘male‘,‘18‘)print(c.name,c.age,c.sex,c.language)c.walk(123)----------输出--------egon 18 male Chineseegon is walking子类的x 123#super在python2中的用法:#1:super(自己的类,self).父类的函数名称#2:super只能用于新式类super应用
python基础之继承原理,多态与封装
评论关闭