{ "cells": [ { "cell_type": "markdown", "id": "6c276828", "metadata": {}, "source": [ "# 公有、私有、特殊以及静态的方法和属性\n", "\n", "一些以 `_` 开头的都是对象的特殊方法和属性。通常来说:\n", "\n", "- 以字母开头都是公有的方法和属性,可以直接调用;\n", "- 在Python中,“私有”的方法和属性以`_`开头,不过,并不存在真正私有的方法和属性,所谓“私有”只是不会被代码自动完成所记录,无法在按<TAB>键时自动补全。\n", "- 以`__`开头并以`__`结尾的是一些系统内置的属性和方法。\n", "- 以`__`开头不以`__`结尾的是一些更加特殊的方法,调用方式最为复杂。\n", "- 装饰器@staticmethod可以生成一个静态方法,静态方法可以用类本身去调用,在定义时,静态方法不需要加self参数。\n", "\n", "例如:" ] }, { "cell_type": "code", "execution_count": 1, "id": "2d3c089b", "metadata": {}, "outputs": [], "source": [ "class MyClass(object):\n", " @staticmethod\n", " def static():\n", " print(\"I'm a static method!\")\n", "\n", " pub = \"public\"\n", "\n", " def __init__(self):\n", " self._spe = \"special\"\n", " print(\"I'm a special method!\")\n", "\n", " def _private(self):\n", " print(\"I'm a private method!\")\n", "\n", " def public(self):\n", " print(\"I'm a public method!\")\n", "\n", " def __really_special(self):\n", " print(\"I'm a really special method!\")\n" ] }, { "cell_type": "markdown", "id": "d171116d", "metadata": {}, "source": [ "构造对象时调用了一个特殊的方法`.__init__()`:" ] }, { "cell_type": "code", "execution_count": 2, "id": "4b33c44b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm a special method!\n" ] } ], "source": [ "m = MyClass()" ] }, { "cell_type": "markdown", "id": "35a1d0fe", "metadata": {}, "source": [ "查看公有属性和特殊属性:" ] }, { "cell_type": "code", "execution_count": 3, "id": "27337836", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'public'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m.pub" ] }, { "cell_type": "code", "execution_count": 4, "id": "68a9de7b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'special'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m._spe" ] }, { "cell_type": "markdown", "id": "99e5e999", "metadata": {}, "source": [ "公有方法:" ] }, { "cell_type": "code", "execution_count": 5, "id": "a4014969", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm a public method!\n" ] } ], "source": [ "m.public()" ] }, { "cell_type": "markdown", "id": "01b0d226", "metadata": {}, "source": [ "私有方法:" ] }, { "cell_type": "code", "execution_count": 6, "id": "895a7f7c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm a private method!\n" ] } ], "source": [ "m._private()" ] }, { "cell_type": "markdown", "id": "cc74e7da", "metadata": {}, "source": [ "以`__`开头不以`__`结尾的方法调用方式有所不同,需要加上类名:" ] }, { "cell_type": "code", "execution_count": 7, "id": "6a0f9e3f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm a really special method!\n" ] } ], "source": [ "m._MyClass__really_special()" ] }, { "cell_type": "markdown", "id": "be41bdc1", "metadata": {}, "source": [ "静态方法用类去调用:" ] }, { "cell_type": "code", "execution_count": 8, "id": "6cf4be62", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm a static method!\n" ] } ], "source": [ "MyClass.static()" ] }, { "cell_type": "code", "execution_count": null, "id": "fdc05823", "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 }