zoukankan      html  css  js  c++  java
  • 【python学习】之四、面向对象之续

    作者:jofranks 原创作品,转载请标明出处!版权所有,侵权必究!

    来源:http://blog.csdn.net/jofranks


    一、组合和派生

    我们创建类,我们要把他当成一个模块来使用它,并且能和其他数据类型及逻辑执行流混合使用!  我们现在又两种方法来在代码中利用类: 1、组合    2、派生


    组合:

    就是让不同的类混合并加入到其他类中,来增加功能和代码重用性!  

    学过其他面向对象语言的同学可能知道,一个类和其他类之间可以定义一种“有一个”(has-a)的关系!  比如:我定义一个A类,一个B类,然后将他们变成组合的关系,也就是说A类中有一个B类实例!

    这样创建以后,每一个类都能单独的去管理他们自己的名字空间和行为了!



    派生:

    面向对象编程的强大功能之一就是能够使一个已经定义好的类扩展它,或者对他进行修改。这样的行为不会影响系统中使用现存类的其他代码片段!

    这样在这里就要涉及到面向对象允许对子类或者子孙类进行继承,被继承的类是基类!并且这些派生类会扩展多带,形成一个类树图!           其中父类和所有的高层类都可叫做祖先!

    来看一下创建子类:

    class SubClassName(ParentClass[, ParentClass2, ...])
         class_suite

    ok,下面我们来看一个例子:

    >>> class Parent(object):
    	def parentMethod(self):
    		print 'calling parent method'
    
    		
    >>> class Child(Parent):
    	def childMethod(self):
    		print 'calling child method'
    
    		
    >>> p = Parent()
    >>> p.parentMethod()
    calling parent method
    >>> c = Child()
    >>> c.childMethod()
    calling child method
    >>> c.parentMethod()
    calling parent method



    二、继承

    他讲述的是基类的属性如何“遗传”给派生类的,一个子类可以继承他的积累的任何属性,不管是数据属性还是方法。  

    下面看一下例子:

    >>> class P(object):
    	'p class'
    	def __init__(self):
    		print 'create an instance of', self.__class__.__name__
    
    		
    >>> class C(P):
    	pass
    
    >>> p = P()
    create an instance of P
    >>> c = C()
    create an instance of C
    >>> p.__class__
    <class '__main__.P'>
    >>> c.__class__
    <class '__main__.C'>
    >>> p.__base__
    >>> P.__bases__
    (<type 'object'>,)
    >>> C.__bases__
    (<class '__main__.P'>,)


    ok,在这里要注意的是文档字符串对类,函数,方法还有模块来说都是唯一的,所以特殊属性__doc__是不会从基类中继承的:

    >>> p.__doc__
    'p class'
    >>> c.__doc__
    >>> p.__bases__

    方法覆盖:

    >>> class P(object):
    	def foo(self):
    		print 'P'
    
    		
    >>> class C(P):
    	def foo(self):
    		print 'C'
    
    		
    >>> p = P()
    >>> c = C()
    >>> p.foo()
    P
    >>> c.foo()
    C

    OK,根据代码可以看出来吧,当然,我们也可以调用基类的方法:

    >>> P.foo(c)
    P



    多重继承:

    在python中支持多重继承,当使用多重继承的时候,有两个不同的方面要基础:

    1、找到合适的属性;

    2、当你重写方法时,如何调用对应父类的方法,以“发挥他们的作用”;


    对于经典类,这里采用的是深度优先遍历的方法:

    >>> class P1:
    	def foo(self):
    		print 'p1'
    
    		
    >>> class P2:
    	def foo(self):
    		print 'p2'
    	def bar(self):
    		print 'p2bar'
    
    		
    >>> class C1(P1, P2):
    	pass
    
    >>> class C2(P1, P2):
    	def bar(self):
    		print 'c2 bar'
    
    		
    >>> class GC(C1, C2):
    	pass
    
    >>> gc = GC()
    >>> gc.foo()
    p1
    >>> gc.bar()
    p2bar

    认真看一下代码!

    而对于新式类,采用的是广度优先遍历:

    >>> class P1(object):
    	def foo(self):
    		print 'p1'
    
    		
    >>> class P2(object):
    	def foo(self):
    		print 'p2'
    	def bar(self):
    		print 'p2bar'
    
    		
    >>> class C1(P1, P2):
    	pass
    
    >>> class C2(P1, P2):
    	def bar(self):
    		print 'c2 bar'
    
    		
    >>> class GC(C1, C2):
    	pass
    
    >>> gc = GC()
    >>> gc.foo()
    p1
    >>> gc.bar()
    c2 bar

    看一下代码有什么不同应该明了了!

    在新式类里面有个属性来查看我们查找类的顺序:

    >>> GC.__mro__
    (<class '__main__.GC'>, <class '__main__.C1'>, <class '__main__.C2'>, <class '__main__.P1'>, <class '__main__.P2'>, <type 'object'>)


    OK!就写到这里吧!




    -------2012/8/3

    -------jofranks 于南昌




  • 相关阅读:
    《Rubu基础教程第五版》第一章笔记
    《Linux Command Line and Shell Scripting Bible》Part 14 处理用户输入
    Phpstorm-Xdebug配置
    Phpstorm-svn配置
    Phpstorm-sftp配置
    3前端案例-遮罩
    2前端案例-选项卡样式
    1前端案例-tag标签+随机位置
    Phpstorm-php在线手册配置
    mysql语句总结
  • 原文地址:https://www.cnblogs.com/java20130723/p/3211440.html
Copyright © 2011-2022 走看看