{"id":139884,"date":"2019-03-25T10:26:29","date_gmt":"2019-03-25T02:26:29","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=139884"},"modified":"2019-03-18T10:27:31","modified_gmt":"2019-03-18T02:27:31","slug":"class-static-method","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/class-static-method.html","title":{"rendered":"python\u4e2d\u7c7b\u65b9\u6cd5\u548c\u9759\u6001\u65b9\u6cd5\u533a\u522b"},"content":{"rendered":"
\u9762\u76f8\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1\u4e2d\uff0c\u7c7b\u65b9\u6cd5\u548c\u9759\u6001\u65b9\u6cd5\u662f\u7ecf\u5e38\u7528\u5230\u7684\u4e24\u4e2a\u672f\u8bed\u3002
\n\u903b\u8f91\u4e0a\u8bb2\uff1a\u7c7b\u65b9\u6cd5\u662f\u53ea\u80fd\u7531\u7c7b\u540d\u8c03\u7528\uff1b\u9759\u6001\u65b9\u6cd5\u53ef\u4ee5\u7531\u7c7b\u540d\u6216\u5bf9\u8c61\u540d\u8fdb\u884c\u8c03\u7528\u3002
\n\u5728C++\u4e2d\uff0c\u9759\u6001\u65b9\u6cd5\u4e0e\u7c7b\u65b9\u6cd5\u903b\u8f91\u4e0a\u662f\u7b49\u4ef7\u7684\uff0c\u53ea\u6709\u4e00\u4e2a\u6982\u5ff5\uff0c\u4e0d\u4f1a\u6df7\u6dc6\u3002
\n\u800c\u5728Python\u4e2d\uff0c\u65b9\u6cd5\u5206\u4e3a\u4e09\u7c7b\u5b9e\u4f8b\u65b9\u6cd5\u3001\u7c7b\u65b9\u6cd5\u3001\u9759\u6001\u65b9\u6cd5\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n
class Test(object):\r\ndef InstanceFun(self):\r\n print(\"InstanceFun\");\r\n print(self);\r\n\r\n\r\n@classmethod\r\ndef ClassFun(cls):\r\n print(\"ClassFun\");\r\n print(cls);\r\n\r\n\r\n@staticmethod\r\ndef StaticFun():\r\n print(\"StaticFun\");\r\n t = Test();\r\n t.InstanceFun();\u3000\u3000\u3000\u3000\u3000\u3000\uff03 \u8f93\u51faInstanceFun\uff0c\u6253\u5370\u5bf9\u8c61\u5185\u5b58\u5730\u5740\u201c<__main__.Test object at 0x0293DCF0>\u201d\r\n Test.ClassFun(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # \u8f93\u51faClassFun\uff0c\u6253\u5370\u7c7b\u4f4d\u7f6e\u00a0<class '__main__.Test'>\r\n Test.StaticFun(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0# \u8f93\u51faStaticFun\r\n t.StaticFun(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # \u8f93\u51faStaticFun\r\n t.ClassFun(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0# \u8f93\u51faClassFun\uff0c\u6253\u5370\u7c7b\u4f4d\u7f6e\u00a0<class '__main__.Test'>\r\n Test.InstanceFun(); \u00a0 \u00a0 \u00a0 \u00a0# \u9519\u8bef\uff0cTypeError: unbound method instanceFun() must be called with Test instance as first argument\r\n Test.InstanceFun(t); \u00a0 \u00a0 \u00a0 #\u00a0\u8f93\u51faInstanceFun\uff0c\u6253\u5370\u5bf9\u8c61\u5185\u5b58\u5730\u5740\u201c<__main__.Test object at 0x0293DCF0>\u201d\r\n t.ClassFun(Test); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0# \u9519\u8bef \u00a0 classFun() takes exactly 1 argument (2 given)\r\n<\/pre>\n\u53ef\u4ee5\u770b\u5230\uff0c\u5728PYTHON\u4e2d\uff0c\u4e24\u79cd\u65b9\u6cd5\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u53c2\u6570\u3002\u5b9e\u4f8b\u65b9\u6cd5\u9690\u542b\u7684\u53c2\u6570\u4e3a\u7c7b\u5b9e\u4f8bself\uff0c\u800c\u7c7b\u65b9\u6cd5\u9690\u542b\u7684\u53c2\u6570\u4e3a\u7c7b\u672c\u8eabcls\u3002
\n\u9759\u6001\u65b9\u6cd5\u65e0\u9690\u542b\u53c2\u6570\uff0c\u4e3b\u8981\u4e3a\u4e86\u7c7b\u5b9e\u4f8b\u4e5f\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u9759\u6001\u65b9\u6cd5\u3002<\/p>\n\u6240\u4ee5\u903b\u8f91\u4e0a\u7c7b\u65b9\u6cd5\u5e94\u5f53\u53ea\u88ab\u7c7b\u8c03\u7528\uff0c\u5b9e\u4f8b\u65b9\u6cd5\u5b9e\u4f8b\u8c03\u7528\uff0c\u9759\u6001\u65b9\u6cd5\u4e24\u8005\u90fd\u80fd\u8c03\u7528\u3002\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u53c2\u6570\u4f20\u9012\u4e0a\u7684\u533a\u522b\uff0c\u5b9e\u4f8b\u65b9\u6cd5\u6084\u6084\u4f20\u9012\u7684\u662fself\u5f15\u7528\u4f5c\u4e3a\u53c2\u6570\uff0c\u800c\u7c7b\u65b9\u6cd5\u6084\u6084\u4f20\u9012\u7684\u662fcls\u5f15\u7528\u4f5c\u4e3a\u53c2\u6570\u3002
\nPython\u5b9e\u73b0\u4e86\u4e00\u5b9a\u7684\u7075\u6d3b\u6027\u4f7f\u5f97\u7c7b\u65b9\u6cd5\u548c\u9759\u6001\u65b9\u6cd5\uff0c\u90fd\u80fd\u591f\u88ab\u5b9e\u4f8b\u548c\u7c7b\u4e8c\u8005\u8c03\u7528<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"\u9762\u76f8\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1\u4e2d\uff0c\u7c7b\u65b9\u6cd5\u548c\u9759\u6001\u65b9\u6cd5\u662f\u7ecf\u5e38\u7528\u5230\u7684\u4e24\u4e2a\u672f\u8bed\u3002 \u903b\u8f91\u4e0a\u8bb2\uff1a\u7c7b\u65b9\u6cd5\u662f\u53ea\u80fd\u7531\u7c7b\u540d\u8c03\u7528\uff1b\u9759\u6001\u65b9\u6cd5\u53ef\u4ee5\u7531\u7c7b […]<\/p>\n","protected":false},"author":643,"featured_media":139886,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-139884","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\/139884","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\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=139884"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/139884\/revisions"}],"predecessor-version":[{"id":139988,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/139884\/revisions\/139988"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/139886"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=139884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=139884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=139884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}