标签:teacher ima 代码 学生 没有 strong ccf 优先 init
python2与python3在继承上的区别:
新式类:但凡继承object类的子类,以及该子类的子子类,...都称之为新式类
经典类:没有继承object类的子类,以及该子类的子子类,...都称之为经典类
只有在python2中才区分新式类与经典类,但是开发中基本都是新式类,在python2中也会继承object类
1. 什么是继承? 在程序中继承是一种新建子类的方式,新创建的类称之为子类\派生类,被继承的类称之为父类\基类\超类 继承描述的是一种遗传关系,子类可以重用父类的属性 2. 为何用继承? 减少类与类之间代码冗余的问题 3. 如何继承 先抽象再继承
class Parent1(object): pass class Parent2: pass class Sub1(Parent1): pass class Sub2(Parent1,Parent2): pass print(Sub1.__bases__)#列出其基类,(<class ‘__main__.Parent1‘>,) print(Sub2.__bases__)#(<class ‘__main__.Parent1‘>, <class ‘__main__.Parent2‘>) print(Parent1.__bases__)#(<class ‘object‘>,) print(Parent2.__bases__)#(<class ‘object‘>,)
x=2 class Foo: x=1 pass obj=Foo() obj.x=3 print(obj.x)#3 x=2 class Foo: x=1 pass obj=Foo() #obj.x=3 print(obj.x)#1 x=2 class Foo: #x=1 pass obj=Foo() #obj.x=3 print(obj.x)#报错, ‘Foo‘ object has no attribute ‘x‘
1 class OldboyPeople: 2 school = ‘Oldboy‘ 3 def __init__(self, name, age, gender): 4 self.name = name 5 self.age = age 6 self.gender = gender 7 8 # print(OldboyPeople.__init__) 9 10 class OldboyStudent(OldboyPeople): 11 # def __init__(self, name, age, gender): 12 # self.name = name 13 # self.age = age 14 # self.gender = gender 15 #继承了父类中的__init__函数,减少了代码冗余 16 def choose_course(self):#选课行为 17 print(‘%s is choosing course‘ %self.name) 18 19 class OldboyTeacher(OldboyPeople): 20 # tea, ‘egon‘, 18, ‘male‘, 10, 3000 21 def __init__(self, name, age, gender,level,salary):#老师类有特有的属性 22 # self.name = name 23 # self.age = age 24 # self.gender = gender 25 OldboyPeople.__init__(self, name, age, gender)#通过类名访问函数,严格按照函数传值(值为上层__init__函数传入的参数),减少了代码冗余 27 self.level=level 28 self.salary=salary 29 30 def score(self,stu,num):#打分行为 31 stu.num=num 32 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__)#{‘name‘: ‘egon‘, ‘age‘: 18, ‘gender‘: ‘male‘, ‘level‘: 10, ‘salary‘: 3000}
强调: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) super().__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__)#{‘name‘: ‘egon‘, ‘age‘: 18, ‘gender‘: ‘male‘, ‘level‘: 10, ‘salary‘: 3000}
class Foo: def f1(self): print(‘Foo.f1‘) def f2(self): print(‘Foo.f2‘) self.f1() #obj.f1(),不会就近查找,会先从obj对象所在的类查找的 class Bar(Foo): def f1(self): print(‘Bar.f1‘) obj=Bar() obj.f2() #Foo.f2 #Bar.f1
如果一个子类继承多个分支,但多个分支没有汇聚到一个非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() print(obj.xxx)#E print(A.mro())#查找属性顺序:A->B->E->C->F->D->object
如果一个子类继承多个分支,但是多个分支最终汇聚到一个非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())#查找顺序:obj->A->B->E->C->F->D->G->object
标签:teacher ima 代码 学生 没有 strong ccf 优先 init
原文地址:https://www.cnblogs.com/xuechengeng/p/9838263.html