{ "cells": [ { "cell_type": "markdown", "source": [ "# 理性认识「类与对象」" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "class Human:\n", " eyes = 2\n", " hands = 2\n", " legs = 2\n", " has_hair = True\n", "\n", " def walk(self):\n", " print(\"Walking\")\n", "\n", " def run(self):\n", " print(\"Running\")\n", "\n", " def think(self):\n", " print(\"Thinking\")\n", "\n", "\n", "def is_human(obj):\n", " if (\n", " (hasattr(obj, \"eyes\") and getattr(obj, \"eyes\") == 2)\n", " and (hasattr(obj, \"hands\") and getattr(obj, \"hands\") == 2)\n", " and (hasattr(obj, \"legs\") and getattr(obj, \"legs\") == 2)\n", " and (hasattr(obj, \"has_hair\") and getattr(obj, \"has_hair\") == True)\n", " and hasattr(obj, \"walk\")\n", " and hasattr(obj, \"run\")\n", " and hasattr(obj, \"think\")\n", " ):\n", " return True\n", " return False\n", "\n", "\n", "obj = Human()\n", "print(is_human(obj))" ] }, { "cell_type": "code", "execution_count": 30, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "obj.eyes: 2\n" ] } ], "source": [ "print(f\"obj.eyes: {obj.eyes}\")" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 31, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "obj.hands: 2\n" ] } ], "source": [ "print(f\"obj.hands: {obj.hands}\")" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 32, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "obj.legs: 2\n" ] } ], "source": [ "print(f\"obj.legs: {obj.legs}\")" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 33, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "obj.has_hair: True\n" ] } ], "source": [ "print(f\"obj.has_hair: {obj.has_hair}\")" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 34, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Walking\n" ] } ], "source": [ "obj.walk()" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 35, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running\n" ] } ], "source": [ "obj.run()" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 36, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Thinking\n" ] } ], "source": [ "obj.think()" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## 属性和方法" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "execution_count": 37, "outputs": [], "source": [ "class MatrixFactory: # 1\n", " name = \"Matrix\" # 2\n", "\n", " def echo(self): # 3\n", " print(\"Welcome to the Matrix\")\n", "\n", "\n", "matrix = MatrixFactory() # 4" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 38, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix\n" ] } ], "source": [ "print(matrix.name) # 5" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 39, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Welcome to the Matrix\n" ] } ], "source": [ "matrix.echo() # 6" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## 构造" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "execution_count": 40, "outputs": [], "source": [ "class Nation: # 1\n", " def __init__(self, name=None): # 2\n", " if not name:\n", " self.name = \"China\"\n", " else:\n", " self.name = name\n" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 41, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "China\n" ] } ], "source": [ "china = Nation() # 3\n", "print(china.name) # 4" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 42, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "USA\n" ] } ], "source": [ "usa = Nation(name=\"USA\") # 5\n", "print(usa.name) # 6" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 43, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "robot address: 140602200206592\n", "self address: 140602200206592\n" ] } ], "source": [ "\n", "class Robot: # 1\n", " def show_self(self): # 2\n", " print(f\"self address: {id(self)}\")\n", "\n", "\n", "robot = Robot() # 3\n", "print(f\"robot address: {id(robot)}\") # 4\n", "robot.show_self() # 5\n" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 44, "outputs": [], "source": [ "# error example\n", "\n", "# class Error: # 1\n", "# def raise_error(): # 2\n", "# print(\"error\")\n", "#\n", "#\n", "# err = Error() # 3\n", "# err.raise_error() # 4" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 45, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "error\n" ] } ], "source": [ "class Error:\n", " def raise_error(err): # 1\n", " print(\"error\")\n", "\n", "\n", "err = Error()\n", "err.raise_error()" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.6" } }, "nbformat": 4, "nbformat_minor": 0 }