码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象之继承与派生

时间:2018-10-23 21:09:26      阅读:176      评论:0      收藏:0      [点我收藏+]

标签: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(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)
      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}

属性查找:

在单继承背景下,无论是新式类还是经典类属性查找顺序都一样:先obj->类->父类->...

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!