类的继承和多态
Posted Ryansuperwa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了类的继承和多态相关的知识,希望对你有一定的参考价值。
继承
1、什么是继承?
继承是一种新建类的方式
新建的类称之为类或者派生类,
父类又可以称之为基类或者超类
子类会‘遗传’父类的属性
继承是类与类之间的关系,寻找这种关系需要先抽象再继承
2、为什么要用继承
减少代码冗余
3、怎么用继承
class parentclass1:
pass
class parentclass2:
pass
class subclass1(parentclass1):
pass
class subclass2(parentclass1,parentclass2):#这就是继承上面
pass
print(subclass2.__bases__)#这个bases就是查看有几个‘爹’的
那我们能从“爹”那里能继承什么呢?
class parent1:
school=‘oldboy’
def __init__(self, name,age,sex):
self.name=name
self.age=age
self.sex=sex
class parent2:
school=\'oldboy\'
def __init__(self,name,age,sex):
self.name=name
self.age=age
self.sex=sex
class son1(parent1):
def f1(self):
print(\'%s son1\'%self.name)
class son2(parent1,parent2):
def f2(self):
print(\'%s son2\'%self.name)
猜一猜 f2中的f1,调用的到底是哪里的?是自己的还是儿子的
调用的是儿子辈的。不是根据就近原则找自己的而是找儿子的
class foo(): #爹 def f1(self): print(\'fool.f1\') def f2(self): print(\'foo.f2\') self.f1() class bar(foo): #儿子 def f1(self): print(\'bar.f1\')
派生:子类定义自己新的属性,如果与父辈同名,以子类自己为准
class OldboyPeople: # school = \'oldboy\' # # def __init__(self, name, age, sex): # self.name = name # self.age = age # self.sex = sex # # def f1(self): # print(\'爹的f1\')
class OldboyTeacher(OldboyPeople): # def change_score(self): # print(\'teacher %s is changing score\' %self.name) # # def f1(self): # print(\'儿子的f1\') # # tea1 = OldboyTeacher(\'egon\', 18, \'male\') # tea1.f1(class OldboyPeople:
school = \'oldboy\' class OldboyPeople: def __init__(self, name, age, sex): self.name = name self.age = age self.sex = sex def f1(self): print(\'爹的f1\') class OldboyTeacher(OldboyPeople): def __init__(self,name,age,sex,level,salary):#应用自己的,不用父辈了
self.name=name self.age=age self.sex=sex self.level=level self.salary=salary def change_score(self): print(\'teacher %s is changing score\' %self.name) def f1(self): print(\'儿子的f1\') tea1 = OldboyTeacher(\'egon\', 18, \'male\',9,3.1) print(tea1.name,tea1.age,tea1.sex,tea1.level,tea1.salary)
子类派生出的新方法中重用父辈的功能:
方法一:指名道姓地调用(其实与继承没有什么关系的)
OldboyPeople.__init__(self,name, age, sex) # class OldboyPeople: # school = \'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 OldboyTeacher(OldboyPeople): # # tea1,\'egon\', 18, \'male\', 9, 3.1 # def __init__(self, name, age, sex, level, salary): # # self.name = name # # self.age = age # # self.sex = sex # OldboyPeople.__init__(self,name, age, sex)#指名道姓的应用 # # self.level = level # self.salary = salary # # def tell_info(self): # OldboyPeople.tell_info(self) # print(""" # 等级:%s # 薪资:%s # """ %(self.level,self.salary)) # # tea1 = OldboyTeacher(\'egon\', 18, \'male\', 9, 3.1) # # print(tea1.name, tea1.age, tea1.sex, tea1.level, tea1.salary) # # # tea1.tell_info()
方法二:super()调用(严格依赖于继承)
super()的返回值是一个特殊的对象,该对象专门用来调用父类中的属性
#了解再python2中,需要super(自己的类名,self)
class OldboyPeople: school = \'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 OldboyTeacher(OldboyPeople): # tea1,\'egon\', 18, \'male\', 9, 3.1 def __init__(self, name, age, sex, level, salary): # OldboyPeople.__init__(self,name, age, sex) super(OldboyTeacher,self).__init__(name,age,sex) self.level = level self.salary = salary def tell_info(self): # OldboyPeople.tell_info(self) super().tell_info() print(""" 等级:%s 薪资:%s """ %(self.level,self.salary)) tea1 = OldboyTeacher(\'egon\', 18, \'male\', 9, 3.1) # print(tea1.name, tea1.age, tea1.sex, tea1.level, tea1.salary) tea1.tell_info()
二、经典类与新式类
1、新式类:
继承object的类,以及该类的子类,都是新式类,在python3中,如果一个类没有指定继承父辈,默认就继承object
所以说python3中所有的类都是新式类
2、经典类:
没有继承object的类,以及该类的子类,都是经典类
三、在棱形继承背景下,super严格按照查找属性
1、经典类:深度优先
先f--》d--》b--》a 找到了就不找了。一条道路走到黑
2、新式类:广度优先
先f--》d--》b--》e--》c--》a 按照菱形找
class A: # def test(self): # print(\'from A\') pass class B(A): # def test(self): # print(\'from B\') pass class C(A): # def test(self): # print(\'from C\') pass class D(B): # def test(self): # print(\'from D\') pass class E(C): # def test(self): # print(\'from E\') pass class F(D,E): # def test(self): # print(\'from F\') pass # f1=F() # f1.test() # F->D->B->E->C-A->object print(F.mro())
以上是关于类的继承和多态的主要内容,如果未能解决你的问题,请参考以下文章