{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## python 面向对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this is my class.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "101"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def getabs(x,y):\n",
    "    return(abs(x),abs(y))\n",
    "\n",
    "class MyClass:\n",
    "    #定义类名\n",
    "    \"myclass help\"\n",
    "    #构造函数     \n",
    "    def __init__(self,x,y=0):\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "    def info(self):\n",
    "        print('this is my class.')\n",
    "    def mysum(self,x,y):\n",
    "        x,y = getabs(x,y)\n",
    "        return x + y\n",
    "nclass = MyClass(2,-9)\n",
    "nclass.info()\n",
    "nclass.mysum(2,-99)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 静态方法和类成员方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "you can call me when democlass is not null. <class '__main__.DemoClass2'>\n"
     ]
    }
   ],
   "source": [
    "class DemoClass2:\n",
    "    @staticmethod\n",
    "    def static_methon():\n",
    "        print('you can call me every where.')\n",
    "        \n",
    "    @classmethod\n",
    "    def classs_methond(cls):\n",
    "        print('you can call me when democlass is not null.',cls)\n",
    "\n",
    "dcs = DemoClass2()\n",
    "dcs.classs_methond()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 继承和多继承"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this ant can fly.\n",
      "当前位置 99 10\n",
      "爬行动物 99  's animal should eat something.\n",
      "当前位置 99 10\n"
     ]
    }
   ],
   "source": [
    "class Animal(object):\n",
    "    def __init__(self,name='baseanimal',age=1):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        \n",
    "    def aniinfo(self):\n",
    "        print('当前位置',self.name,self.age)\n",
    "        \n",
    "    def eat(self):\n",
    "        print(self.name,self.age,\" 's animal should eat something.\")\n",
    "        self.info()\n",
    "        \n",
    "class Ant(object):\n",
    "    def __init__(self,x=0,y=0,color='black'):\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "        self.color = color\n",
    "        \n",
    "    def info(self):\n",
    "        print('当前位置',self.x,self.y)\n",
    "        \n",
    "    def crawl(self,x,y):\n",
    "        print('ant is crawling.')\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "        self.info()\n",
    "        \n",
    "    def bit(self):\n",
    "        print('ant bit people.')\n",
    "        \n",
    "class FlyAnt(Animal,Ant,):\n",
    "    def __init__(self,name,age,x,y,color):\n",
    "        super(FlyAnt,self).__init__()\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "        self.color = color\n",
    "#     def eat(self):\n",
    "#         print('flyant eat',self.color)\n",
    "    #重写bit方法     \n",
    "    def bit(self):\n",
    "        print('flyant bit animals, not people')\n",
    "    #新增功能\n",
    "    def fly(self,x,y):\n",
    "        print('this ant can fly.')\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "        self.info()\n",
    "# TEST\n",
    "flyant = FlyAnt(name='爬行动物',age=99,x=9,y=100,color='blue')\n",
    "# flyant.crawl(3,5)\n",
    "flyant.fly(99,10)\n",
    "flyant.eat()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 异常处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "end\n"
     ]
    },
    {
     "ename": "ZeroDivisionError",
     "evalue": "division by zero",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-70-41e5fc22ad73>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     11\u001b[0m     \u001b[1;32mfinally\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     12\u001b[0m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'end'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 13\u001b[1;33m \u001b[0mtestTry\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-70-41e5fc22ad73>\u001b[0m in \u001b[0;36mtestTry\u001b[1;34m()\u001b[0m\n\u001b[0;32m      2\u001b[0m     \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'789'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m \u001b[1;33m/\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m     \u001b[1;32mexcept\u001b[0m \u001b[0mAttributeError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'attributeError.'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero"
     ]
    }
   ],
   "source": [
    "def testTry():\n",
    "    a = '789'\n",
    "    try:\n",
    "        print(3 / 0)\n",
    "    except AttributeError:\n",
    "        print('attributeError.')\n",
    "    except TabError:\n",
    "        print('tableError')\n",
    "    else:\n",
    "        print('other')\n",
    "    finally:\n",
    "        print('end')\n",
    "testTry()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "assert a error\n",
      "0\n",
      "assert a error\n",
      "1\n",
      "assert a error\n",
      "2\n",
      "assert a error\n",
      "3\n",
      "4\n",
      "end\n"
     ]
    }
   ],
   "source": [
    "def testRaise():\n",
    "    for i in range(5):\n",
    "        try :\n",
    "            #条件为假 则大于3则不抛出异常\n",
    "            assert i > 3\n",
    "        except AssertionError:\n",
    "            print('assert a error')\n",
    "#             raise NameError\n",
    "        print(i)\n",
    "    print('end')\n",
    "testRaise()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "myError",
     "evalue": "this is my error.",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mmyError\u001b[0m                                   Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-77-d33b95585e64>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__str__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalue\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mmyError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'this is my error.'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mmyError\u001b[0m: this is my error."
     ]
    }
   ],
   "source": [
    "class myError(Exception):\n",
    "    def __init__(self,value):\n",
    "        self.value = value\n",
    "    def __str__(self):\n",
    "        return self.value\n",
    "raise myError('this is my error.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pdb\n",
    "pdb.run(\"\"\"\n",
    "for i in range():\n",
    "    print(i)\n",
    "\"\"\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
