{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 定义 class" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 基本形式" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`class` 定义如下:\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` 中的方法要进行缩进" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Forest(object):\n", " \"\"\" Forest can grow trees which eventually die.\"\"\"\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "其中 `object` 是最基本的类型。\n", "\n", "查看帮助:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Forest()\n", "\n", "Forest can grow trees which eventually die.\n", "\n", "\n", "Methods:\n", "\n" ] } ], "source": [ "import numpy as np\n", "np.info(Forest)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "forest = Forest()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "<__main__.Forest at 0x3cda358>" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "forest" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 添加方法和属性" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以直接添加属性(有更好的替代方式):" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "forest.trees = np.zeros((150, 150), dtype=bool)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[False, False, False, ..., False, False, False],\n", " [False, False, False, ..., False, False, False],\n", " [False, False, False, ..., False, False, False],\n", " ..., \n", " [False, False, False, ..., False, False, False],\n", " [False, False, False, ..., False, False, False],\n", " [False, False, False, ..., False, False, False]], dtype=bool)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "forest.trees" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "forest2 = Forest()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`forest2` 没有这个属性:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "ename": "AttributeError", "evalue": "'Forest' object has no attribute 'trees'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mforest2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtrees\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m: 'Forest' object has no attribute 'trees'" ] } ], "source": [ "forest2.trees" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "添加方法时,默认第一个参数是对象本身,一般为 `self`,可能用到也可能用不到,然后才是其他的参数:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Forest(object):\n", " \"\"\" Forest can grow trees which eventually die.\"\"\"\n", " def grow(self):\n", " print \"the tree is growing!\"\n", " \n", " def number(self, num=1):\n", " if num == 1:\n", " print 'there is 1 tree.'\n", " else:\n", " print 'there are', num, 'trees.'" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the tree is growing!\n", "there are 12 trees.\n" ] } ], "source": [ "forest = Forest()\n", "\n", "forest.grow()\n", "forest.number(12)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" } }, "nbformat": 4, "nbformat_minor": 0 }