{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.\t面向对象三大特性，各有什么用处，说说你的理解。"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "继承：解决代码重用问题。 隐藏实现细节，使代码模块化。\n",
    "多态：为了类在继承和派生的时候，保证使用家谱中任一类的实例的某一属性时可以正确调用。增加程序的灵活性（以不变应万变，不论对象千变万化，同一种方式调用。）增加了程序的可扩展性。\n",
    "封装：明确区分内外，控制外部对隐藏属性的操作行为，隔离复杂度。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2 类的属性和对象的属性有什么区别?"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "的属性分为函数属性与数据属性：数据属性是对所有对象共享的，函数属性是与对象绑定的。\n",
    "对象的属性是实例化，可能来自于类定义，也可能依据实例化后定义的。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "类属性其实是用字典存储"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'key': 'name', 'aa': 'name'}\n",
      "name\n",
      "{'key': 'val', 'aa': 'name'}\n",
      "{'aa': 'name'}\n"
     ]
    }
   ],
   "source": [
    "class A():\n",
    "    def __init__(self,msg):\n",
    "        self.key = msg\n",
    "        self.aa = msg\n",
    "    def __getitem__(self, item):\n",
    "        return self.__dict__.get(item)\n",
    "    def __setitem__(self, key, value):\n",
    "        self.__dict__[key]=value\n",
    "    def __delitem__(self, key):\n",
    "        del self.__dict__[key]\n",
    " \n",
    "a = A(\"name\")\n",
    "print(a.__dict__)\n",
    "a[\"key\"]\n",
    "print(a.key)\n",
    "a[\"key\"] = \"val\"\n",
    "print(a.__dict__)     # print(a.key)\n",
    "del a[\"key\"]\n",
    "print(a.__dict__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面这段代码的输出结果将是什么？请解释。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 1 1\n",
      "1 2 1\n",
      "3 2 3\n"
     ]
    }
   ],
   "source": [
    "class Parent(object):\n",
    "    x = 1\n",
    "class Child1(Parent):\n",
    "    pass\n",
    "class Child2(Parent):\n",
    "    pass\n",
    "print(Parent.x, Child1.x, Child2.x)\n",
    "Child1.x = 2\n",
    "print(Parent.x, Child1.x, Child2.x)\n",
    "Parent.x = 3\n",
    "print(Parent.x, Child1.x, Child2.x)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "1 1 1 继承自父类的类属性x，所以都一样，指向同一块内存地址\n",
    "1 2 1 更改Child1，Child1的x指向了新的内存地址\n",
    "3 2 3 更改Parent，Parent的x指向了新的内存地址"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "多重继承的执行顺序，请解答以下输出结果是什么？并解释。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "G\n",
      "D\n",
      "A\n",
      "B\n",
      "F\n",
      "C\n",
      "B\n",
      "D\n",
      "A\n"
     ]
    }
   ],
   "source": [
    "class A(object):\n",
    "    def __init__(self):\n",
    "        print('A')\n",
    "        super(A, self).__init__()\n",
    "class B(object):\n",
    "    def __init__(self):\n",
    "        print('B')\n",
    "        super(B, self).__init__()\n",
    "class C(A):\n",
    "    def __init__(self):\n",
    "        print('C')\n",
    "        super(C, self).__init__()\n",
    "class D(A):\n",
    "    def __init__(self):\n",
    "        print('D')\n",
    "        super(D, self).__init__()\n",
    "class E(B, C):\n",
    "    def __init__(self):\n",
    "        print('E')\n",
    "        super(E, self).__init__()\n",
    "class F(C, B, D):\n",
    "    def __init__(self):\n",
    "        print('F')\n",
    "        super(F, self).__init__()\n",
    "class G(D, B):\n",
    "    def __init__(self):\n",
    "        print('G')\n",
    "        super(G, self).__init__()\n",
    "if __name__ == '__main__':\n",
    "    g = G()\n",
    "    f = F()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请编写一段符合多态特性的代码."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lalalalala......\n",
      "woof\n",
      "mew\n"
     ]
    }
   ],
   "source": [
    "class People(object):\n",
    "    def talk(self):\n",
    "        print(\"Lalalalala......\")\n",
    "class Dog(object):\n",
    "    def talk(self):\n",
    "        print(\"woof\")\n",
    "class Cat(object):\n",
    "    def talk(self):\n",
    "        print(\"mew\")\n",
    "def func(animal):\n",
    "    animal.talk()\n",
    "p = People()\n",
    "d = Dog()\n",
    "c = Cat()\n",
    "func(p)\n",
    "func(d)\n",
    "func(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "编写程序, A继承了B, 俩个类都实现了handle方法, 在A中的handle方法中调用B的handle方法　"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "B\n"
     ]
    }
   ],
   "source": [
    "class B():\n",
    "    def handle(self):\n",
    "        print(\"B\")\n",
    "class A(B):\n",
    "    def handle(self):\n",
    "        # print(\"A\")\n",
    "        super(A, self).handle()\n",
    "a = A()\n",
    "a.handle()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入文件名:\n",
      "aaa\n",
      "输入字符串:\n",
      "bbb\n",
      "bbb"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'raw_input' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-8-1ea6e257706e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      6\u001b[0m     \u001b[0mfp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mch\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m     \u001b[0mstdout\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mch\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m     \u001b[0mch\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mraw_input\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m''\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      9\u001b[0m \u001b[0mfp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'raw_input' is not defined"
     ]
    }
   ],
   "source": [
    "from sys import stdout\n",
    "filename = input('输入文件名:\\n')\n",
    "fp = open(filename,\"w\")\n",
    "ch = input('输入字符串:\\n')\n",
    "while ch != '#':\n",
    "    fp.write(ch)\n",
    "    stdout.write(ch)\n",
    "    ch = raw_input('')\n",
    "fp.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
