{ "cells": [ { "cell_type": "markdown", "id": "8a5d82b9", "metadata": {}, "source": [ "# 面向对象简介\n", "\n", "对象一般是指一个类的实例,具有相关的成员变量和成员函数,在Python中成员变量叫做属性,成员函数叫做方法。\n" ] }, { "cell_type": "markdown", "id": "7c2c9e49", "metadata": {}, "source": [ "## 什么是对象\n", "\n", "Python中几乎所有的东西都是对象。基本类型是对象,如整数:" ] }, { "cell_type": "code", "execution_count": 1, "id": "21ffb0a2", "metadata": {}, "outputs": [], "source": [ "a = 10" ] }, { "cell_type": "markdown", "id": "d67f6220", "metadata": {}, "source": [ "其属性与方法:" ] }, { "cell_type": "code", "execution_count": 2, "id": "f8d025b8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.real" ] }, { "cell_type": "code", "execution_count": 3, "id": "7e227da7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.conjugate()" ] }, { "cell_type": "markdown", "id": "e666906e", "metadata": {}, "source": [ "对象在内存中有一个地址与之对应,这个地址可以用id()函数查看:" ] }, { "cell_type": "code", "execution_count": 4, "id": "73e18793", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4514492720" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(len)" ] }, { "cell_type": "markdown", "id": "80760e35", "metadata": {}, "source": [ "同一个对象的内存地址一致:" ] }, { "cell_type": "code", "execution_count": 5, "id": "6715c20b", "metadata": {}, "outputs": [], "source": [ "b = a" ] }, { "cell_type": "code", "execution_count": 6, "id": "b2b0f179", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b) == id(a)" ] }, { "cell_type": "markdown", "id": "ea101f3e", "metadata": {}, "source": [ "id()函数本身也是对象:" ] }, { "cell_type": "code", "execution_count": 7, "id": "2d4c1062", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4514488128" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(id)" ] }, { "cell_type": "markdown", "id": "8c9a076b", "metadata": {}, "source": [ "在Python中,只有一些保留的关键字和符号不是对象。" ] }, { "cell_type": "markdown", "id": "31b5ecc1", "metadata": {}, "source": [ "## 自定义类型\n", "\n", "基本形式为:\n", "```python\n", "class ClassName(ParentClass):\n", " \"\"\"class docstring\"\"\"\n", " def some_method(self, ...):\n", " return ...\n", "```\n", "具体规律为:\n", "- class关键字在最开始。\n", "- ClassName是自定义类型的名称, Python对类型名称没有规定,流行的做法是用大骆驼拼写法(Upper Camel Case)的记法,即每个单词的首字母大写,中间不留空格。\n", "- “()”中的ParentClass用来表示继承关系,可省略,默认为object。\n", "- “:”不能缺少,之后的内容要进行缩进。\n", "- 三引号包含的部分是类型的文档即docstring,可以省略。\n", "- 类型的方法定义与函数定义类似,第一个参数表示是对象本身,通常用self表示。\n", "\n", "例如:" ] }, { "cell_type": "code", "execution_count": 8, "id": "2e678cc3", "metadata": {}, "outputs": [], "source": [ "class Leaf(object):\n", " \"\"\"A leaf falling in the woods.\"\"\"\n", " pass" ] }, { "cell_type": "markdown", "id": "8b1b43d9", "metadata": {}, "source": [ "其中,pass是一个特殊的关键词,用来占位,表示什么都不做。" ] }, { "cell_type": "markdown", "id": "2ae4ea06", "metadata": {}, "source": [ "新类型Leaf继承了Python中最基本的类型,即object。事实上所有的对象都是object:" ] }, { "cell_type": "code", "execution_count": 9, "id": "1e0a28eb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isinstance(10, object)" ] }, { "cell_type": "code", "execution_count": 10, "id": "5a775665", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isinstance([], object)" ] }, { "cell_type": "markdown", "id": "31ccd183", "metadata": {}, "source": [ "三引号的部分是对Leaf类的说明文档,可以调用Leaf的`.__doc__`属性查看:" ] }, { "cell_type": "code", "execution_count": 11, "id": "11ddc382", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'A leaf falling in the woods.'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Leaf.__doc__" ] }, { "cell_type": "markdown", "id": "a32d330c", "metadata": {}, "source": [ "Leaf对象的产生:" ] }, { "cell_type": "code", "execution_count": 12, "id": "afb454d1", "metadata": {}, "outputs": [], "source": [ "leaf = Leaf()" ] }, { "cell_type": "markdown", "id": "35b34c33", "metadata": {}, "source": [ "在Python里,变量是区分大小写的,因此,leaf和Leaf是不同的变量。\n", "\n", "再次调用生成的对象是不同的:" ] }, { "cell_type": "code", "execution_count": 13, "id": "01c581c4", "metadata": {}, "outputs": [], "source": [ "leaf2 = Leaf()" ] }, { "cell_type": "code", "execution_count": 14, "id": "fedbbba1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "leaf is leaf2" ] }, { "cell_type": "code", "execution_count": null, "id": "ae2a7981", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.10" } }, "nbformat": 4, "nbformat_minor": 5 }