python的继承
继承是面向对象的重要特征之一,继承是两个类或者多个类之间的父子关系,子进程继承了父进程的所有公有实例变量和方法。继承实现了代码的重用。重用已经存在的数据和行为,减少代码的重新编写,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的继承的更多相关文章
- sqlalchemy mark-deleted 和 python 多继承下的方法解析顺序 MRO
sqlalchemy mark-deleted 和 python 多继承下的方法解析顺序 MRO 今天在弄一个 sqlalchemy 的数据库基类的时候,遇到了跟多继承相关的一个小问题,因此顺便看了一 ...
- python基础——继承和多态
python基础——继承和多态 在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的class继承,新的class称为子类(Subclass),而被继承的class称为基类.父类或超类 ...
- [修]python普通继承方式和super继承方式
[转]python普通继承方式和super继承方式 原文出自:http://www.360doc.com/content/13/0306/15/9934052_269664772.shtml 原文的错 ...
- Python进阶-继承中的MRO与super
Python进阶-继承中的MRO与super 写在前面 如非特别说明,下文均基于Python3 摘要 本文讲述Python继承关系中如何通过super()调用"父类"方法,supe ...
- python基础——继承实现的原理
python基础--继承实现的原理 1 继承顺序 class A(object): def test(self): print('from A') class B(A): def test(self) ...
- python基础——继承与派生、组合
python基础--继承与派生 1 什么是继承: 继承是一种创建新的类的方式,在python中,新建的类可以继承自一个或者多个父类,原始类成为基类或超累,新建的类成为派生类或子类 1.1 继承分为:单 ...
- python 类继承演示范例的代码
把做工程过程重要的代码片段备份一次,下面的资料是关于python 类继承演示范例的代码. # a simple example of a class inheritance # tested with ...
- [py]python的继承体系-源码目录结构
python3安装目录 pip install virtualenv pip install virtualenvwrapper pip install virtualenvwrapper-win m ...
- [py]python的继承体系
python的继承体系 python中一切皆对象 随着类的定义而开辟执行 class Foo(object): print 'Loading...' spam = 'eggs' print 'Done ...
- Python多继承解析顺序的C3线性算法流程解析
Python多继承MRO 在Python2.1中,采用了经典类,使用深度优先算法解析. Python2.2中,引入了新式类,使用深度优先算法和广度优先算法. 在Python2.3以后的版本中,经典类和 ...
随机推荐
- Linux下Bash入门学习笔记
学习好shell编程是很有用的,可以使用shell脚本轻巧地完成有趣的工作. 本文地址:http://www.cnblogs.com/yhLinux/p/4047516.html 1. Bash实例, ...
- Arcgis API For IOS扩展AGSDynamicLayer新旧版API对比
AGSDynamicLayer(ForSubclassEyesOnly) Category Reference Description This category organizes the meth ...
- awk 学习笔记
awk的语法有两种形式 awk [options] 'script' var=value file(s) awk [options] -f scriptfile var=value file(s) 选 ...
- 数据库SQL语句中根据当前日期计算其他日期小结
问题描述:我们在写存储过程和函数的时候经常会碰到利用当前日期计算出上周开始日期.结束日期,或者计算上个月的开始日期结束日期等问题.最近写了几个存储过程和函数,其中都涉及到了日期计算问题,在这里简单做一 ...
- 在Ubuntu下配置舒服的Python开发环境
Ubuntu 提供了一个良好的 Python 开发环境,但如果想使我们的开发效率最大化,还需要进行很多定制化的安装和配置.下面的是我们团队开发人员推荐的一个安装和配置步骤,基于 Ubuntu 12.0 ...
- Android技术点
一.四大组件 1.1 Activity 1.2 Broadcast 1.3 Content Provider 1.4 Service 1. Binder 2. Messager 3. AIDL ...
- Effecvive Java读书笔记(一):创建和销毁对象
I.考虑静态工厂方法替代构造器 优势:1.有清晰的方法名称,方便调用:多参数构造器易出现调用错误 2.不必每次调用都创建新对象 3.可以返回原返回类型的任何子类型 4.创建参数化类型实例的时候,代码简 ...
- LintCode 463 Sort Integer
这个是O(n2)的排序的总结 /* bubble sort */public static void sortIntegers(int[] A) { // Write your code here i ...
- Visual Studio2013(Update4)无法生成依赖项关系图解决方案
今天为了看别人写的一套框架,就琢磨着有没视图的方式去看别人每个类和方法之间的调用和继承关系.这个时候发现了一个VS2013自带的功能: 带着兴奋的心情小手一抖一点... 我等呀等,等呀等.... 尼玛 ...
- display:block 不起作用
jquery中$("#Main").css("display","none"); $("#Day").css('disp ...