继承是面向对象的重要特征之一,继承是两个类或者多个类之间的父子关系,子进程继承了父进程的所有公有实例变量和方法。继承实现了代码的重用。重用已经存在的数据和行为,减少代码的重新编写,python在类名后用一对圆括号表示继承关系, 括号中的类表示父类,如果父类定义了__init__方法,则子类必须显示地调用父类的__init__方法,如果子类需要扩展父类的行为,可以添加__init__方法的参数。

单继承:

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Eric.yue class Fruit(object):
def __init__(self, color):
self.color = color
print "fruit's color: %s" % self.color def grow(self):
print "grow..." class Apple(Fruit):
def __init__(self,color):
Fruit.__init__(self,color) #继承父类的构造方法
print "apple's color: %s" % self.color class Banana(Fruit): # 继承了父类
def __init__(self, color): # 显示调用父类的__init__方法
Fruit.__init__(self, color)
print "banana's color:%s" % self.color def grow(self): # 覆盖了父类的grow方法
print "banana grow..." if __name__ == "__main__":
apple = Apple("red")
apple.grow()
print '--------'
banana = Banana("yellow")
banana.grow() '''
输出结果:
fruit's color: red
apple's color: red
grow...
--------
fruit's color: yellow
banana's color:yellow
banana grow...
'''

多继承:

 class Person1(object):
def __init__(self):
self.name = 'person1 name' def running(self):
print 'person1 is running' class Person2(object):
def __init__(self):
pass def swimming(self):
print 'person2 is swimming' class Mike(Person1,Person2):
def __init__(self):
Person1.__init__(self)
#self.name = 'Mike name' 将覆盖person1中的name m = Mike()
print m.name
m.running()
m.swimming() '''
输出结果
Mike name
person1 is running
person2 is swimming
'''

python的继承的更多相关文章

  1. sqlalchemy mark-deleted 和 python 多继承下的方法解析顺序 MRO

    sqlalchemy mark-deleted 和 python 多继承下的方法解析顺序 MRO 今天在弄一个 sqlalchemy 的数据库基类的时候,遇到了跟多继承相关的一个小问题,因此顺便看了一 ...

  2. python基础——继承和多态

    python基础——继承和多态 在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的class继承,新的class称为子类(Subclass),而被继承的class称为基类.父类或超类 ...

  3. [修]python普通继承方式和super继承方式

    [转]python普通继承方式和super继承方式 原文出自:http://www.360doc.com/content/13/0306/15/9934052_269664772.shtml 原文的错 ...

  4. Python进阶-继承中的MRO与super

    Python进阶-继承中的MRO与super 写在前面 如非特别说明,下文均基于Python3 摘要 本文讲述Python继承关系中如何通过super()调用"父类"方法,supe ...

  5. python基础——继承实现的原理

    python基础--继承实现的原理 1 继承顺序 class A(object): def test(self): print('from A') class B(A): def test(self) ...

  6. python基础——继承与派生、组合

    python基础--继承与派生 1 什么是继承: 继承是一种创建新的类的方式,在python中,新建的类可以继承自一个或者多个父类,原始类成为基类或超累,新建的类成为派生类或子类 1.1 继承分为:单 ...

  7. python 类继承演示范例的代码

    把做工程过程重要的代码片段备份一次,下面的资料是关于python 类继承演示范例的代码. # a simple example of a class inheritance # tested with ...

  8. [py]python的继承体系-源码目录结构

    python3安装目录 pip install virtualenv pip install virtualenvwrapper pip install virtualenvwrapper-win m ...

  9. [py]python的继承体系

    python的继承体系 python中一切皆对象 随着类的定义而开辟执行 class Foo(object): print 'Loading...' spam = 'eggs' print 'Done ...

  10. Python多继承解析顺序的C3线性算法流程解析

    Python多继承MRO 在Python2.1中,采用了经典类,使用深度优先算法解析. Python2.2中,引入了新式类,使用深度优先算法和广度优先算法. 在Python2.3以后的版本中,经典类和 ...

随机推荐

  1. css中怎么设置透明度的问题

    小伙伴们是不是在找怎么样去设置页面的透明度的方法呢...别找了,我这儿就有,而且肯定够用了. 我自己会用到的就有两种,可以和大家分享一下. 1.用opcity的方法去设置透明度.代码如下: .div ...

  2. sacc scss less

    CSS 预处理器技术已经非常的成熟,而且也涌现出了越来越多的 CSS 的预处理器框架.本文向你介绍使用最为普遍的三款 CSS 预处理器框架,分别是 Sass.Less CSS.Stylus. 首先我们 ...

  3. Jquery 表单验证

    <html>     <head>         <meta http-equiv="content-type" content="tex ...

  4. C++中的注解理解

    SAL: the Microsoft Source Code Annotation Language. SAL: the Microsoft Source Code Annotation Langua ...

  5. [XAF] 多级联列表显示

    XAF给的例子已经实现,详细可查看例子中的代码. 工作车间--工作中心--机器

  6. Texture2D.GetPixelBilinear(float u, float v)的使用,官方例子注释

    using UnityEngine; using System.Collections; public class TEST : MonoBehaviour { public Texture2D so ...

  7. 《理解 ES6》阅读整理:函数(Functions)(六)Purpose of Functions

    明确函数的双重作用(Clarifying the Dual Purpose of Functions) 在ES5及更早的ES版本中,函数调用时是否使用new会有不同的作用.当使用new时,函数内的th ...

  8. HTML5-块元素标签的使用

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. [leetcode 25]Reverse Nodes in k-Group

    1 题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified li ...

  10. Twitter全局唯一ID生成算法

    测试:private static void TestIdWorker() { HashSet<long> set = new HashSet<long>(); IdWorke ...