{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 继承" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "一个类定义的基本形式如下:\n", "```python\n", "class ClassName(ParentClass):\n", " \"\"\"class docstring\"\"\"\n", " def method(self):\n", " return\n", "```\n", "\n", "- `class` 关键词在最前面\n", "- `ClassName` 通常采用 `CamelCase` 记法\n", "- 括号中的 `ParentClass` 用来表示继承关系\n", "- 冒号不能缺少\n", "- `\"\"\"\"\"\"` 中的内容表示 `docstring`,可以省略\n", "- 方法定义与函数定义十分类似,不过多了一个 `self` 参数表示这个对象本身\n", "- `class` 中的方法要进行缩进\n", "\n", "在里面有一个 `ParentClass` 项,用来进行继承,被继承的类是父类,定义的这个类是子类。\n", "对于子类来说,继承意味着它可以使用所有父类的方法和属性,同时还可以定义自己特殊的方法和属性。\n", "\n", "假设我们有这样一个父类:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Leaf(object):\n", " def __init__(self, color=\"green\"):\n", " self.color = color\n", " def fall(self):\n", " print \"Splat!\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "测试:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "green\n" ] } ], "source": [ "leaf = Leaf()\n", "\n", "print leaf.color" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Splat!\n" ] } ], "source": [ "leaf.fall()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "现在定义一个子类,继承自 `Leaf`:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class MapleLeaf(Leaf):\n", " def change_color(self):\n", " if self.color == \"green\":\n", " self.color = \"red\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "继承父类的所有方法:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "green\n" ] } ], "source": [ "mleaf = MapleLeaf()\n", "\n", "print mleaf.color" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Splat!\n" ] } ], "source": [ "mleaf.fall()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "但是有自己独有的方法,父类中没有:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "red\n" ] } ], "source": [ "mleaf.change_color()\n", "\n", "print mleaf.color" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "如果想对父类的方法进行修改,只需要在子类中重定义这个类即可:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class MapleLeaf(Leaf):\n", " def change_color(self):\n", " if self.color == \"green\":\n", " self.color = \"red\"\n", " def fall(self):\n", " self.change_color()\n", " print \"Plunk!\"" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "green\n", "Plunk!\n", "red\n" ] } ], "source": [ "mleaf = MapleLeaf()\n", "\n", "print mleaf.color\n", "mleaf.fall()\n", "print mleaf.color" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }