• Python 学习笔记(十五)Python类拓展(一)继承


    继承

    继承(Inheritance):是面向对象软件技术当中的一个概念。如果一个类别A "继承自" 另一个类B,就把这个A称为“B的子类”,而把B称为“A的父类”,也可以称“B是A的超类”。

              重用代码

              属性和方法的继承

    单继承和super函数

    示例1:子类调用父类update_web(),执行父类update_web()

     1 #! /usr/bin/env python
     2 # coding:utf-8
     3 
     4 class Person(object): #新式类
     5 
     6     def __init__(self,web_site): #初始化方法
     7         self.web =web_site
     8 
     9     def update_web(self,site):
    10         self.web =site
    11         return self.web
    12 
    13 
    14 class Cc(Person): #继承了Person类,子类继承了一个父类,叫单继承
    15 
    16       def about_me(self,name,site): #继承关系,调用父类update_web方法
    17           my_web =self.update_web(site)
    18           return {"name":name,"web":my_web}
    19 
    20    
    21     
    22 
    23 if __name__ =="__main__":
    24     my =Cc("www.weibo.com")
    25     print my.about_me("cc","cc.blog.com")
    26     print my.web
    27 
    28 
    29 #output
    30 #{'web': 'cc.blog.com', 'name': 'cc'}
    31 #cc.blog.com

    示例2:子类重写父类update_web()方法,在子类中调用,执行子类的update_web()方法

     1 #! /usr/bin/env python
     2 # coding:utf-8
     3 
     4 class Person(object): #新式类
     5 
     6     def __init__(self,web_site): #初始化方法
     7         self.web =web_site
     8 
     9     def update_web(self,site):
    10         self.web =site
    11         return self.web
    12 
    13 
    14 class Cc(Person): #继承了Person类,子类继承了一个父类,叫单继承
    15 
    16     def update_web(self,site,lang="python"):#此处重写了父类的方法,或者是覆盖了父类的方法
    17          self.web =site
    18          self.lang =lang
    19          return self.web,self.lang
    20 
    21    
    22     def about_me(self,name,site): #继承关系,此处调用子类的方法
    23        my_web,my_lang=self.update_web(site)
    24        return {"name":name,"web":my_web,"lang":my_lang}
    25     
    26 
    27 if __name__ =="__main__":
    28     my =Cc("www.weibo.com")
    29     print my.about_me("cc","cc.blog.com")
    30     
    31 #output
    32 #{'lang': 'python', 'web': 'cc.blog.com', 'name': 'cc'}

    示例3:调用父类中被覆盖的方法,使用super()函数,或者父类.方法名

     1 #! /usr/bin/env python
     2 # coding:utf-8
     3 
     4 class Person(object): #新式类
     5 
     6     def __init__(self,web_site): #初始化方法
     7         self.web =web_site
     8 
     9     def update_web(self,site):
    10         self.web =site
    11         return self.web
    12 
    13 
    14 class Cc(Person): #继承了Person类,子类继承了一个父类,叫单继承
    15 
    16     def __init__(self,teacher,web_site):
    17         self.teacher =teacher
    18         #Person.__init__(self,web_site) #调用父类的init方法
    19         super(Cc,self).__init__(web_site) #调用父类中被覆盖的方法
    20         
    21     
    22 
    23     def update_web(self,site,lang="python"):#此处重写了父类的方法,或者是覆盖了父类的方法
    24          self.web =site
    25          self.lang =lang
    26          return self.web,self.lang
    27 
    28     def your_teacher(self):
    29         return self.teacher
    30 
    31    
    32     def about_me(self,name,site): #继承关系,此处调用子类的方法
    33        my_web,my_lang=self.update_web(site)
    34        return {"name":name,"web":my_web,"lang":my_lang}
    35     
    36 
    37 if __name__ =="__main__":
    38     my =Cc("cclaoshi","cnblog.com")
    39     print my.your_teacher()
    40     print my.teacher
    41     print my.web
    42 
    43 #output
    44 #cclaoshi
    45 #cclaoshi
    46 #cnblog.com

     多重继承

    示例:

     1 #! /usr/bin/env python
     2 # coding:utf-8
     3 
     4 class Person(object): #新式类
     5     def eye(self):
     6         print "two eyes"
     7 
     8     def breast(self,n):
     9         print "The breast is:",n
    10 
    11 
    12 class Girl(object):
    13     def __init__(self,age):
    14         self.age =age
    15 
    16     def color(self):
    17         print "The girl is white."
    18 
    19 
    20 class BeaGirl(Person,Girl):#多继承,写入两个类的名字,将父类的所有方法继承过来
    21     pass
    22 
    23 
    24 
    25 
    26 if __name__ =="__main__":
    27     kong = BeaGirl(28)
    28     kong.eye()
    29     kong.breast(90)
    30     kong.color()
    31     print kong.age
    32 
    33 
    34 #output
    35 #two eyes
    36 #The breast is: 90
    37 #The girl is white.
    38 #28

    示例:多重继承的执行顺序,广度优先

     1 #! /usr/bin/env python
     2 # coding:utf-8
     3 
     4 class K1(object): #新式类
     5     def foo(self):
     6         print "K1-foo"
     7 
     8 
     9 
    10 class K2(object):
    11     def foo(self):
    12         print "K2-foo"
    13 
    14     def bar(self):
    15         print "K2-bar"
    16 
    17 
    18 class J1(K1,K2):#多继承,写入两个类的名字,将父类的所有方法继承过来
    19     pass
    20 
    21 
    22 class J2(K1,K2):
    23     def bar(self):
    24         print "J2-bar"
    25 
    26 class C(J1,J2):
    27     pass
    28 
    29 
    30 
    31 
    32 if __name__ =="__main__":
    33     print C.__mro__
    34     m =C()
    35     m.foo()
    36     m.bar()
    37 
    38 #这种继承顺序称为广度优先
    39 #output
    40 #(<class '__main__.C'>, <class '__main__.J1'>, <class '__main__.J2'>,
    41 #  <class '__main__.K1'>, <class '__main__.K2'>, <type 'object'>)
    42 #K1-foo
    43 #J2-bar
  • 相关阅读:
    The AllegroGraph Tutorial
    Using Prolog withUsing Prolog with AllegroGraph 7.1.0 AllegroGraph 7.1.0
    Prolog 语言入门教程
    Learn Prolog Now!:Chapter 3 Recursion
    What are OWL Ontologies?
    论文阅读:SkillMaN—A skill-based robotic manipulation framework based on perception and reasoning
    论文阅读:Knowledge-based instruction of manipulation tasks for industrial robotics
    Learn Prolog
    KnowRob安装过程中的相关问题记录
    Qt音视频开发17-海康sdk解码
  • 原文地址:https://www.cnblogs.com/wangruihua-521/p/8569652.html
Copyright © 2020-2023  润新知