\u7c7b\u5bf9\u8c61<\/strong><\/div>\n\u7c7b\u5bf9\u8c61\u652f\u6301\u4e24\u79cd\u64cd\u4f5c\uff1a\u5c5e\u6027\u5f15\u7528\u548c\u5b9e\u4f8b\u5316\u3002<\/p>\n
\u5c5e\u6027\u5f15\u7528\u4f7f\u7528\u548c Python \u4e2d\u6240\u6709\u7684\u5c5e\u6027\u5f15\u7528\u4e00\u6837\u7684\u6807\u51c6\u8bed\u6cd5\uff1aobj.name\u3002<\/p>\n
\u7c7b\u5bf9\u8c61\u521b\u5efa\u540e\uff0c\u7c7b\u547d\u540d\u7a7a\u95f4\u4e2d\u6240\u6709\u7684\u547d\u540d\u90fd\u662f\u6709\u6548\u5c5e\u6027\u540d\u3002\u6240\u4ee5\u5982\u679c\u7c7b\u5b9a\u4e49\u662f\u8fd9\u6837:<\/p>\n
\u5b9e\u4f8b(Python 3.0+)\r\n#!\/usr\/bin\/python3\r\n \r\nclass MyClass:\r\n \"\"\"\u4e00\u4e2a\u7b80\u5355\u7684\u7c7b\u5b9e\u4f8b\"\"\"\r\n i = 12345\r\n def f(self):\r\n return 'hello world'\r\n \r\n# \u5b9e\u4f8b\u5316\u7c7b\r\nx = MyClass()\r\n \r\n# \u8bbf\u95ee\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\r\nprint(\"MyClass \u7c7b\u7684\u5c5e\u6027 i \u4e3a\uff1a\", x.i)\r\nprint(\"MyClass \u7c7b\u7684\u65b9\u6cd5 f \u8f93\u51fa\u4e3a\uff1a\", x.f())<\/pre>\n\u4ee5\u4e0a\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u7c7b\u5b9e\u4f8b\u5e76\u5c06\u8be5\u5bf9\u8c61\u8d4b\u7ed9\u5c40\u90e8\u53d8\u91cf x\uff0cx \u4e3a\u7a7a\u7684\u5bf9\u8c61\u3002<\/p>\n
\u6267\u884c\u4ee5\u4e0a\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n
MyClass \u7c7b\u7684\u5c5e\u6027 i \u4e3a\uff1a 12345\r\nMyClass \u7c7b\u7684\u65b9\u6cd5 f \u8f93\u51fa\u4e3a\uff1a hello world<\/pre>\n\u7c7b\u6709\u4e00\u4e2a\u540d\u4e3a __init__() \u7684\u7279\u6b8a\u65b9\u6cd5\uff08\u6784\u9020\u65b9\u6cd5\uff09\uff0c\u8be5\u65b9\u6cd5\u5728\u7c7b\u5b9e\u4f8b\u5316\u65f6\u4f1a\u81ea\u52a8\u8c03\u7528\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n
def __init__(self):\r\n self.data = []<\/pre>\n\u7c7b\u5b9a\u4e49\u4e86 __init__() \u65b9\u6cd5\uff0c\u7c7b\u7684\u5b9e\u4f8b\u5316\u64cd\u4f5c\u4f1a\u81ea\u52a8\u8c03\u7528 __init__() \u65b9\u6cd5\u3002\u5982\u4e0b\u5b9e\u4f8b\u5316\u7c7b MyClass\uff0c\u5bf9\u5e94\u7684 __init__() \u65b9\u6cd5\u5c31\u4f1a\u88ab\u8c03\u7528:<\/p>\n
x = MyClass()<\/pre>\n\u5f53\u7136\uff0c __init__() \u65b9\u6cd5\u53ef\u4ee5\u6709\u53c2\u6570\uff0c\u53c2\u6570\u901a\u8fc7 __init__() \u4f20\u9012\u5230\u7c7b\u7684\u5b9e\u4f8b\u5316\u64cd\u4f5c\u4e0a\u3002\u4f8b\u5982:<\/p>\n
\u5b9e\u4f8b(Python 3.0+)\r\n#!\/usr\/bin\/python3\r\n \r\nclass Complex:\r\n def __init__(self, realpart, imagpart):\r\n self.r = realpart\r\n self.i = imagpart\r\nx = Complex(3.0, -4.5)\r\nprint(x.r, x.i) # \u8f93\u51fa\u7ed3\u679c\uff1a3.0 -4.5<\/pre>\nself\u4ee3\u8868\u7c7b\u7684\u5b9e\u4f8b\uff0c\u800c\u975e\u7c7b<\/strong><\/span><\/div>\n\u7c7b\u7684\u65b9\u6cd5\u4e0e\u666e\u901a\u7684\u51fd\u6570\u53ea\u6709\u4e00\u4e2a\u7279\u522b\u7684\u533a\u522b\u2014\u2014\u5b83\u4eec\u5fc5\u987b\u6709\u4e00\u4e2a\u989d\u5916\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u540d\u79f0, \u6309\u7167\u60ef\u4f8b\u5b83\u7684\u540d\u79f0\u662f self\u3002<\/p>\n
class Test:\r\n def prt(self):\r\n print(self)\r\n print(self.__class__)\r\n \r\nt = Test()\r\nt.prt()<\/pre>\n\u4ee5\u4e0a\u5b9e\u4f8b\u6267\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n
<__main__.Test instance at 0x100771878>\r\n__main__.Test<\/pre>\n\u4ece\u6267\u884c\u7ed3\u679c\u53ef\u4ee5\u5f88\u660e\u663e\u7684\u770b\u51fa\uff0cself \u4ee3\u8868\u7684\u662f\u7c7b\u7684\u5b9e\u4f8b\uff0c\u4ee3\u8868\u5f53\u524d\u5bf9\u8c61\u7684\u5730\u5740\uff0c\u800c self.class \u5219\u6307\u5411\u7c7b\u3002<\/p>\n
self \u4e0d\u662f python \u5173\u952e\u5b57\uff0c\u6211\u4eec\u628a\u4ed6\u6362\u6210 runoob \u4e5f\u662f\u53ef\u4ee5\u6b63\u5e38\u6267\u884c\u7684:<\/p>\n
class Test:\r\n def prt(runoob):\r\n print(runoob)\r\n print(runoob.__class__)\r\n \r\nt = Test()\r\nt.prt()<\/pre>\n\u4ee5\u4e0a\u5b9e\u4f8b\u6267\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n
<__main__.Test instance at 0x100771878>\r\n__main__.Test<\/pre>\n\u7c7b\u7684\u65b9\u6cd5<\/strong><\/div>\n\u5728\u7c7b\u7684\u5185\u90e8\uff0c\u4f7f\u7528 def \u5173\u952e\u5b57\u6765\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\uff0c\u4e0e\u4e00\u822c\u51fd\u6570\u5b9a\u4e49\u4e0d\u540c\uff0c\u7c7b\u65b9\u6cd5\u5fc5\u987b\u5305\u542b\u53c2\u6570 self, \u4e14\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0cself \u4ee3\u8868\u7684\u662f\u7c7b\u7684\u5b9e\u4f8b\u3002<\/p>\n
\u5b9e\u4f8b(Python 3.0+)\r\n#!\/usr\/bin\/python3\r\n \r\n#\u7c7b\u5b9a\u4e49\r\nclass people:\r\n #\u5b9a\u4e49\u57fa\u672c\u5c5e\u6027\r\n name = ''\r\n age = 0\r\n #\u5b9a\u4e49\u79c1\u6709\u5c5e\u6027,\u79c1\u6709\u5c5e\u6027\u5728\u7c7b\u5916\u90e8\u65e0\u6cd5\u76f4\u63a5\u8fdb\u884c\u8bbf\u95ee\r\n __weight = 0\r\n #\u5b9a\u4e49\u6784\u9020\u65b9\u6cd5\r\n def __init__(self,n,a,w):\r\n self.name = n\r\n self.age = a\r\n self.__weight = w\r\n def speak(self):\r\n print(\"%s \u8bf4: \u6211 %d \u5c81\u3002\" %(self.name,self.age))\r\n \r\n# \u5b9e\u4f8b\u5316\u7c7b\r\np = people('runoob',10,30)\r\np.speak()<\/pre>\n\u6267\u884c\u4ee5\u4e0a\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n
runoob \u8bf4: \u6211 10 \u5c81\u3002<\/pre>\n\u7ee7\u627f<\/strong><\/div>\nPython \u540c\u6837\u652f\u6301\u7c7b\u7684\u7ee7\u627f\uff0c\u5982\u679c\u4e00\u79cd\u8bed\u8a00\u4e0d\u652f\u6301\u7ee7\u627f\uff0c\u7c7b\u5c31\u6ca1\u6709\u4ec0\u4e48\u610f\u4e49\u3002\u6d3e\u751f\u7c7b\u7684\u5b9a\u4e49\u5982\u4e0b\u6240\u793a:<\/p>\n
\r\nclass<\/span> <\/span>DerivedClassName<\/span>(<\/span>BaseClassName1<\/span>)<\/span>:\r\n <<\/span>statement<\/span>-<\/span>1<\/span>>\r\n .\r\n .\r\n .\r\n <<\/span>statement<\/span>-<\/span>N<\/span>><\/span>\r\n<\/div><\/pre>\n\u9700\u8981\u6ce8\u610f\u5706\u62ec\u53f7\u4e2d\u57fa\u7c7b\u7684\u987a\u5e8f\uff0c\u82e5\u662f\u57fa\u7c7b\u4e2d\u6709\u76f8\u540c\u7684\u65b9\u6cd5\u540d\uff0c\u800c\u5728\u5b50\u7c7b\u4f7f\u7528\u65f6\u672a\u6307\u5b9a\uff0cpython\u4ece\u5de6\u81f3\u53f3\u641c\u7d22 \u5373\u65b9\u6cd5\u5728\u5b50\u7c7b\u4e2d\u672a\u627e\u5230\u65f6\uff0c\u4ece\u5de6\u5230\u53f3\u67e5\u627e\u57fa\u7c7b\u4e2d\u662f\u5426\u5305\u542b\u65b9\u6cd5\u3002<\/p>\n
BaseClassName\uff08\u793a\u4f8b\u4e2d\u7684\u57fa\u7c7b\u540d\uff09\u5fc5\u987b\u4e0e\u6d3e\u751f\u7c7b\u5b9a\u4e49\u5728\u4e00\u4e2a\u4f5c\u7528\u57df\u5185\u3002\u9664\u4e86\u7c7b\uff0c\u8fd8\u53ef\u4ee5\u7528\u8868\u8fbe\u5f0f\uff0c\u57fa\u7c7b\u5b9a\u4e49\u5728\u53e6\u4e00\u4e2a\u6a21\u5757\u4e2d\u65f6\u8fd9\u4e00\u70b9\u975e\u5e38\u6709\u7528:<\/p>\n
class DerivedClassName(modname.BaseClassName):<\/pre>\n\u5b9e\u4f8b(Python 3.0+)\r\n#!\/usr\/bin\/python3\r\n \r\n#\u7c7b\u5b9a\u4e49\r\nclass people:\r\n #\u5b9a\u4e49\u57fa\u672c\u5c5e\u6027\r\n name = ''\r\n age = 0\r\n #\u5b9a\u4e49\u79c1\u6709\u5c5e\u6027,\u79c1\u6709\u5c5e\u6027\u5728\u7c7b\u5916\u90e8\u65e0\u6cd5\u76f4\u63a5\u8fdb\u884c\u8bbf\u95ee\r\n __weight = 0\r\n #\u5b9a\u4e49\u6784\u9020\u65b9\u6cd5\r\n def __init__(self,n,a,w):\r\n self.name = n\r\n self.age = a\r\n self.__weight = w\r\n def speak(self):\r\n print(\"%s \u8bf4: \u6211 %d \u5c81\u3002\" %(self.name,self.age))\r\n \r\n#\u5355\u7ee7\u627f\u793a\u4f8b\r\nclass student(people):\r\n grade = ''\r\n def __init__(self,n,a,w,g):\r\n #\u8c03\u7528\u7236\u7c7b\u7684\u6784\u51fd\r\n people.__init__(self,n,a,w)\r\n self.grade = g\r\n #\u8986\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\r\n def speak(self):\r\n print(\"%s \u8bf4: \u6211 %d \u5c81\u4e86\uff0c\u6211\u5728\u8bfb %d \u5e74\u7ea7\"%(self.name,self.age,self.grade))\r\n \r\n \r\n \r\ns = student('ken',10,60,3)\r\ns.speak()<\/pre>\n\u6267\u884c\u4ee5\u4e0a\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n
ken \u8bf4: \u6211 10 \u5c81\u4e86\uff0c\u6211\u5728\u8bfb 3 \u5e74\u7ea7<\/pre>\n\u591a\u7ee7\u627f<\/strong><\/div>\nPython\u540c\u6837\u6709\u9650\u7684\u652f\u6301\u591a\u7ee7\u627f\u5f62\u5f0f\u3002\u591a\u7ee7\u627f\u7684\u7c7b\u5b9a\u4e49\u5f62\u5982\u4e0b\u4f8b:<\/p>\n
\r\nclass<\/span> <\/span>DerivedClassName<\/span>(<\/span>Base1<\/span>, <\/span>Base2<\/span>, <\/span>Base3<\/span>)<\/span>:\r\n <<\/span>statement<\/span>-<\/span>1<\/span>>\r\n .\r\n .\r\n .\r\n <<\/span>statement<\/span>-<\/span>N<\/span>><\/span>\r\n<\/div><\/pre>\n\u9700\u8981\u6ce8\u610f\u5706\u62ec\u53f7\u4e2d\u7236\u7c7b\u7684\u987a\u5e8f\uff0c\u82e5\u662f\u7236\u7c7b\u4e2d\u6709\u76f8\u540c\u7684\u65b9\u6cd5\u540d\uff0c\u800c\u5728\u5b50\u7c7b\u4f7f\u7528\u65f6\u672a\u6307\u5b9a\uff0cpython\u4ece\u5de6\u81f3\u53f3\u641c\u7d22 \u5373\u65b9\u6cd5\u5728\u5b50\u7c7b\u4e2d\u672a\u627e\u5230\u65f6\uff0c\u4ece\u5de6\u5230\u53f3\u67e5\u627e\u7236\u7c7b\u4e2d\u662f\u5426\u5305\u542b\u65b9\u6cd5\u3002<\/p>\n
\u5b9e\u4f8b(Python 3.0+)\r\n#!\/usr\/bin\/python3\r\n \r\n#\u7c7b\u5b9a\u4e49\r\nclass people:\r\n #\u5b9a\u4e49\u57fa\u672c\u5c5e\u6027\r\n name = ''\r\n age = 0\r\n #\u5b9a\u4e49\u79c1\u6709\u5c5e\u6027,\u79c1\u6709\u5c5e\u6027\u5728\u7c7b\u5916\u90e8\u65e0\u6cd5\u76f4\u63a5\u8fdb\u884c\u8bbf\u95ee\r\n __weight = 0\r\n #\u5b9a\u4e49\u6784\u9020\u65b9\u6cd5\r\n def __init__(self,n,a,w):\r\n self.name = n\r\n self.age = a\r\n self.__weight = w\r\n def speak(self):\r\n print(\"%s \u8bf4: \u6211 %d \u5c81\u3002\" %(self.name,self.age))\r\n \r\n#\u5355\u7ee7\u627f\u793a\u4f8b\r\nclass student(people):\r\n grade = ''\r\n def __init__(self,n,a,w,g):\r\n #\u8c03\u7528\u7236\u7c7b\u7684\u6784\u51fd\r\n people.__init__(self,n,a,w)\r\n self.grade = g\r\n #\u8986\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\r\n def speak(self):\r\n print(\"%s \u8bf4: \u6211 %d \u5c81\u4e86\uff0c\u6211\u5728\u8bfb %d \u5e74\u7ea7\"%(self.name,self.age,self.grade))\r\n \r\n#\u53e6\u4e00\u4e2a\u7c7b\uff0c\u591a\u91cd\u7ee7\u627f\u4e4b\u524d\u7684\u51c6\u5907\r\nclass speaker():\r\n topic = ''\r\n name = ''\r\n def __init__(self,n,t):\r\n self.name = n\r\n self.topic = t\r\n def speak(self):\r\n print(\"\u6211\u53eb %s\uff0c\u6211\u662f\u4e00\u4e2a\u6f14\u8bf4\u5bb6\uff0c\u6211\u6f14\u8bb2\u7684\u4e3b\u9898\u662f %s\"%(self.name,self.topic))\r\n \r\n#\u591a\u91cd\u7ee7\u627f\r\nclass sample(speaker,student):\r\n a =''\r\n def __init__(self,n,a,w,g,t):\r\n student.__init__(self,n,a,w,g)\r\n speaker.__init__(self,n,t)\r\n \r\ntest = sample(\"Tim\",25,80,4,\"Python\")\r\ntest.speak() #\u65b9\u6cd5\u540d\u540c\uff0c\u9ed8\u8ba4\u8c03\u7528\u7684\u662f\u5728\u62ec\u53f7\u4e2d\u6392\u524d\u5730\u7236\u7c7b\u7684\u65b9\u6cd5<\/pre>\n\u6267\u884c\u4ee5\u4e0a\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n
\u6211\u53eb Tim\uff0c\u6211\u662f\u4e00\u4e2a\u6f14\u8bf4\u5bb6\uff0c\u6211\u6f14\u8bb2\u7684\u4e3b\u9898\u662f Python<\/pre>\n\u65b9\u6cd5\u91cd\u5199<\/strong><\/div>\n\u5982\u679c\u4f60\u7684\u7236\u7c7b\u65b9\u6cd5\u7684\u529f\u80fd\u4e0d\u80fd\u6ee1\u8db3\u4f60\u7684\u9700\u6c42\uff0c\u4f60\u53ef\u4ee5\u5728\u5b50\u7c7b\u91cd\u5199\u4f60\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n
\u5b9e\u4f8b(Python 3.0+)\r\n#!\/usr\/bin\/python3\r\n \r\nclass Parent: # \u5b9a\u4e49\u7236\u7c7b\r\n def myMethod(self):\r\n print ('\u8c03\u7528\u7236\u7c7b\u65b9\u6cd5')\r\n \r\nclass Child(Parent): # \u5b9a\u4e49\u5b50\u7c7b\r\n def myMethod(self):\r\n print ('\u8c03\u7528\u5b50\u7c7b\u65b9\u6cd5')\r\n \r\nc = Child() # \u5b50\u7c7b\u5b9e\u4f8b\r\nc.myMethod() # \u5b50\u7c7b\u8c03\u7528\u91cd\u5199\u65b9\u6cd5\r\nsuper(Child,c).myMethod() #\u7528\u5b50\u7c7b\u5bf9\u8c61\u8c03\u7528\u7236\u7c7b\u5df2\u88ab\u8986\u76d6\u7684\u65b9\u6cd5\r\nsuper() \u51fd\u6570\u662f\u7528\u4e8e\u8c03\u7528\u7236\u7c7b(\u8d85\u7c7b)\u7684\u4e00\u4e2a\u65b9\u6cd5\u3002<\/pre>\n\u6267\u884c\u4ee5\u4e0a\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n
\u8c03\u7528\u5b50\u7c7b\u65b9\u6cd5\r\n\u8c03\u7528\u7236\u7c7b\u65b9\u6cd5<\/pre>\n\u7c7b\u5c5e\u6027\u4e0e\u65b9\u6cd5<\/strong><\/div>\n\u7c7b\u7684\u79c1\u6709\u5c5e\u6027<\/strong><\/span><\/div>\n__private_attrs\uff1a\u4e24\u4e2a\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u58f0\u660e\u8be5\u5c5e\u6027\u4e3a\u79c1\u6709\uff0c\u4e0d\u80fd\u5728\u7c7b\u7684\u5916\u90e8\u88ab\u4f7f\u7528\u6216\u76f4\u63a5\u8bbf\u95ee\u3002\u5728\u7c7b\u5185\u90e8\u7684\u65b9\u6cd5\u4e2d\u4f7f\u7528\u65f6 self.__private_attrs\u3002<\/p>\n
\u7c7b\u7684\u65b9\u6cd5<\/strong><\/span><\/div>\n\u5728\u7c7b\u7684\u5185\u90e8\uff0c\u4f7f\u7528 def \u5173\u952e\u5b57\u6765\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\uff0c\u4e0e\u4e00\u822c\u51fd\u6570\u5b9a\u4e49\u4e0d\u540c\uff0c\u7c7b\u65b9\u6cd5\u5fc5\u987b\u5305\u542b\u53c2\u6570 self\uff0c\u4e14\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0cself \u4ee3\u8868\u7684\u662f\u7c7b\u7684\u5b9e\u4f8b\u3002<\/p>\n
self \u7684\u540d\u5b57\u5e76\u4e0d\u662f\u89c4\u5b9a\u6b7b\u7684\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 this\uff0c\u4f46\u662f\u6700\u597d\u8fd8\u662f\u6309\u7167\u7ea6\u5b9a\u662f\u7528 self\u3002<\/p>\n
\u7c7b\u7684\u79c1\u6709\u65b9\u6cd5<\/strong><\/span><\/div>\n__private_method\uff1a\u4e24\u4e2a\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u58f0\u660e\u8be5\u65b9\u6cd5\u4e3a\u79c1\u6709\u65b9\u6cd5\uff0c\u53ea\u80fd\u5728\u7c7b\u7684\u5185\u90e8\u8c03\u7528 \uff0c\u4e0d\u80fd\u5728\u7c7b\u7684\u5916\u90e8\u8c03\u7528\u3002self.__private_methods\u3002<\/p>\n
\u5b9e\u4f8b<\/strong><\/span><\/div>\n\u7c7b\u7684\u79c1\u6709\u5c5e\u6027\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n
\u5b9e\u4f8b(Python 3.0+)\r\n#!\/usr\/bin\/python3\r\n \r\nclass JustCounter:\r\n __secretCount = 0 # \u79c1\u6709\u53d8\u91cf\r\n publicCount = 0 # \u516c\u5f00\u53d8\u91cf\r\n \r\n def count(self):\r\n self.__secretCount += 1\r\n self.publicCount += 1\r\n print (self.__secretCount)\r\n \r\ncounter = JustCounter()\r\ncounter.count()\r\ncounter.count()\r\nprint (counter.publicCount)\r\nprint (counter.__secretCount) # \u62a5\u9519\uff0c\u5b9e\u4f8b\u4e0d\u80fd\u8bbf\u95ee\u79c1\u6709\u53d8\u91cf<\/pre>\n\u6267\u884c\u4ee5\u4e0a\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n
1\r\n2\r\n2\r\nTraceback (most recent call last):\r\n File \"test.py\", line 16, in \r\n print (counter.__secretCount) # \u62a5\u9519\uff0c\u5b9e\u4f8b\u4e0d\u80fd\u8bbf\u95ee\u79c1\u6709\u53d8\u91cf\r\nAttributeError: 'JustCounter' object has no attribute '__secretCount'<\/module><\/pre>\n\u7c7b\u7684\u79c1\u6709\u65b9\u6cd5\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n
\u5b9e\u4f8b(Python 3.0+)\r\n#!\/usr\/bin\/python3\r\n \r\nclass Site:\r\n def __init__(self, name, url):\r\n self.name = name # public\r\n self.__url = url # private\r\n \r\n def who(self):\r\n print('name : ', self.name)\r\n print('url : ', self.__url)\r\n \r\n def __foo(self): # \u79c1\u6709\u65b9\u6cd5\r\n print('\u8fd9\u662f\u79c1\u6709\u65b9\u6cd5')\r\n \r\n def foo(self): # \u516c\u5171\u65b9\u6cd5\r\n print('\u8fd9\u662f\u516c\u5171\u65b9\u6cd5')\r\n self.__foo()\r\n \r\nx = Site('\u83dc\u9e1f\u6559\u7a0b', 'www.runoob.com')\r\nx.who() # \u6b63\u5e38\u8f93\u51fa\r\nx.foo() # \u6b63\u5e38\u8f93\u51fa\r\nx.__foo() # \u62a5\u9519<\/pre>\n\u4ee5\u4e0a\u5b9e\u4f8b\u6267\u884c\u7ed3\u679c\uff1a
\n<\/p>\n
\u7c7b\u7684\u4e13\u6709\u65b9\u6cd5\uff1a<\/strong><\/span><\/div>\n__init__ : \u6784\u9020\u51fd\u6570\uff0c\u5728\u751f\u6210\u5bf9\u8c61\u65f6\u8c03\u7528
\n__del__ : \u6790\u6784\u51fd\u6570\uff0c\u91ca\u653e\u5bf9\u8c61\u65f6\u4f7f\u7528
\n__repr__ : \u6253\u5370\uff0c\u8f6c\u6362
\n__setitem__ : \u6309\u7167\u7d22\u5f15\u8d4b\u503c
\n__getitem__: \u6309\u7167\u7d22\u5f15\u83b7\u53d6\u503c
\n__len__: \u83b7\u5f97\u957f\u5ea6
\n__cmp__: \u6bd4\u8f83\u8fd0\u7b97
\n__call__: \u51fd\u6570\u8c03\u7528
\n__add__: \u52a0\u8fd0\u7b97
\n__sub__: \u51cf\u8fd0\u7b97
\n__mul__: \u4e58\u8fd0\u7b97
\n__truediv__: \u9664\u8fd0\u7b97
\n__mod__: \u6c42\u4f59\u8fd0\u7b97
\n__pow__: \u4e58\u65b9<\/p>\n
\u8fd0\u7b97\u7b26\u91cd\u8f7d<\/strong><\/span><\/div>\nPython\u540c\u6837\u652f\u6301\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u7c7b\u7684\u4e13\u6709\u65b9\u6cd5\u8fdb\u884c\u91cd\u8f7d\uff0c\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n
\u5b9e\u4f8b(Python 3.0+)\r\n#!\/usr\/bin\/python3\r\n \r\nclass Vector:\r\n def __init__(self, a, b):\r\n self.a = a\r\n self.b = b\r\n \r\n def __str__(self):\r\n return 'Vector (%d, %d)' % (self.a, self.b)\r\n \r\n def __add__(self,other):\r\n return Vector(self.a + other.a, self.b + other.b)\r\n \r\nv1 = Vector(2,10)\r\nv2 = Vector(5,-2)\r\nprint (v1 + v2)<\/pre>\n\u4ee5\u4e0a\u4ee3\u7801\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u6240\u793a:<\/p>\n
Vector(7,8)<\/pre>\n","protected":false},"excerpt":{"rendered":"\u7c7b(Class): \u7528\u6765\u63cf\u8ff0\u5177\u6709\u76f8\u540c\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u5bf9\u8c61\u7684\u96c6\u5408\u3002\u5b83\u5b9a\u4e49\u4e86\u8be5\u96c6\u5408\u4e2d\u6bcf\u4e2a\u5bf9\u8c61\u6240\u5171\u6709\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u5bf9\u8c61 […]<\/p>\n","protected":false},"author":321,"featured_media":167444,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-167442","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=167442"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167442\/revisions"}],"predecessor-version":[{"id":167896,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167442\/revisions\/167896"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/167444"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=167442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=167442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=167442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}