{"id":243951,"date":"2022-06-15T09:31:27","date_gmt":"2022-06-15T01:31:27","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=243951"},"modified":"2022-05-29T17:33:51","modified_gmt":"2022-05-29T09:33:51","slug":"python-self-weighr","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-self-weighr.html","title":{"rendered":"\u8be6\u7ec6\u4ecb\u7ecdPython\u7c7b\u7684\u7ee7\u627f\u4e0e\u591a\u6001"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5927\u5bb6\u597d\uff0c\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u7684\u662fPython\u7c7b\u7684\u7ee7\u627f\u4e0e\u591a\u6001\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u611f\u5174\u8da3\u7684\u540c\u5b66\u8d76\u5feb\u6765\u770b\u4e00\u770b\u5427\uff0c\u5bf9\u4f60\u6709\u5e2e\u52a9\u7684\u8bdd\u8bb0\u5f97\u6536\u85cf\u4e00\u4e0b\uff0c\u65b9\u4fbf\u4e0b\u6b21\u6d4f\u89c8<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u6982\u5ff5<\/strong><\/div>\n

\u7c7b(Class): \u7528\u6765\u63cf\u8ff0\u5177\u6709\u76f8\u540c\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u5bf9\u8c61\u7684\u96c6\u5408\u3002<\/p>\n

\u7c7b\u53d8\u91cf\uff1a\u7c7b\u53d8\u91cf\u5728\u6574\u4e2a\u5b9e\u4f8b\u5316\u7684\u5bf9\u8c61\u4e2d\u662f\u516c\u7528\u7684\u3002\u7c7b\u53d8\u91cf\u5b9a\u4e49\u5728\u7c7b\u4e2d\u4e14\u5728\u51fd\u6570\u4f53\u4e4b\u5916\u3002\u7c7b\u53d8\u91cf\u901a\u5e38\u4e0d\u4f5c\u4e3a\u5b9e\u4f8b\u53d8\u91cf\u4f7f\u7528\u3002<\/p>\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<\/p>\n

self:self \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

\u7c7b\u8c03\u7528 Car.weight<\/p>\n

\u5b9e\u4f8b\u5316 car01=Car(5)<\/p>\n

\u5b9e\u4f8b\u5bf9\u8c61\u8c03\u7528 car01.weght<\/p>\n

\u7c7b\u7684\u521b\u5efa<\/strong><\/div>\n
#\u521b\u5efa\u5c0f\u6c7d\u8f66\u7c7b\u548c\u5ba2\u8f66\u7c7b\r\nclass Car(object):\r\n    def __init__(self,weight):    #\u5b9e\u4f8b\u5316\u5bf9\u8c61\u65f6\u4f20\u5165\u7279\u6709\u5c5e\u6027\r\n        self.weight=weight\r\n        print(\"\u5c0f\u6c7d\u8f66\")\r\n        print(weight,\"t\")          #\u5b9a\u4e49\u7c7b\u5185\u65b9\u6cd5\uff0c\u529f\u80fd\u662f\u6253\u5370\u51fa\u5b9e\u4f8b\u5316\u7684\u5c0f\u6c7d\u8f66\u7684\u91cd\u91cf\r\n    def func(self):\r\n        print(\"\u5c0f\u6c7d\u8f66\u7684\u9a7e\u9a76\u65b9\u6cd5\")\r\nclass Passenger_car(object):\r\n    def __init__(self,weight):\r\n        self.weight=weight\r\n        print(\"\u5ba2\u8f66\")\r\n        print(weight,\"t\")\r\n    def func(self):\r\n        print(\"\u5ba2\u8f66\u7684\u9a7e\u9a76\u65b9\u6cd5\")<\/pre>\n
\u7c7b\u7684\u7ee7\u627f<\/strong><\/div>\n

\u6211\u4eec\u5728\u6784\u9020\u7c7b\u65f6\uff0cPython3\u9ed8\u8ba4\u6211\u4eec\u7ee7\u627f\u4e86object\u8fd9\u4e2a\u57fa\u7c7b\uff0c\u6211\u4e2a\u4eba\u7406\u89e3object\u5c31\u662f\u4e2a\u7a7a\u7684\u7c7b\uff0c\u53ef\u4ee5\u4e0d\u7528\u7ba1\u4e3a\u4f55\u8981\u5728\u62ec\u53f7\u4e2d\u5199\u4e0aobject\uff0c\u8fd9\u662fPython3\u7684\u7279\u6027\uff0c\u5728python2\u4e2d\u5982\u679c\u4f60\u6ca1\u6709\u5199object\u7684\u8bdd\u4e0d\u4f1a\u9ed8\u8ba4\u7ee7\u627f\u4e86object\u8fd9\u4e2a\u57fa\u7c7b\u3002<\/p>\n

\u540c\u6837\u7684\u6211\u4eec\u81ea\u5df1\u5e0c\u671b\u7ee7\u627f\u7684\u7236\u7c7b\u53ea\u9700\u8981\u628aobjetc\u6539\u4e3a\u6211\u4eec\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u540d\u5373\u53ef\u3002\u5b50\u7c7b\u4e2d\u53ef\u4ee5\u62e5\u6709\u7236\u7c7b\u4e2d\u6240\u6709\u7684\u516c\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4f46\u662f\u53ef\u4ee5\u901a\u8fc7\u5728\u53d8\u91cf\u540d\u524d\u52a0\u4e0b\u5212\u7ebf\u4f7f\u5176\u53d8\u4e3a\u79c1\u6709\uff0c\u8fd9\u6837\u5b50\u7c7b\u5c31\u4e0d\u53ef\u4ee5\u8bbf\u95ee\u7236\u7c7b\u4e2d\u7684\u6210\u5458\u4e86\u3002<\/p>\n

class Bus028(Passenger_car):\r\n    def __init__(self,weight): #\u5bf9\u57fa\u7c7b\u65b9\u6cd5\u7684\u91cd\u5199\r\n        self.weight=weight\r\n        print(\"28\u8def\u516c\u4ea4\u8f66\")\r\n        print(weight,\"t\")\r\n    def func(self):           #\u5bf9\u57fa\u7c7b\u65b9\u6cd5\u7684\u91cd\u5199 \r\n        print(\"28\u8def\u516c\u4ea4\u8f66\u7684\u9a7e\u9a76\u65b9\u6cd5\")\r\n    #\u5b50\u7c7b\u5728\u7236\u7c7b\u4e2d\u7ee7\u627f\u8fc7\u6765\u4e86func\u65b9\u6cd5\uff0c\u4f46\u662f\u5728\u7236\u7c7b\u4e2d\u4fee\u6539\u4e86\u539f\u6765\u65b9\u6cd5\u7684\u5185\u5bb9<\/pre>\n
\u591a\u6001\u7684\u4f7f\u7528<\/strong><\/div>\n

\u4ee5\u4e0b\u4e09\u4e2a\u516c\u4ea4\u8f66\u7c7b\u7684\u7236\u7c7b\u5747\u4e3a\u5ba2\u8f66\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u5199\u4e00\u4e2afuncs\u65b9\u6cd5\u4f7f\u5f97\u6bcf\u6b21\u8c03\u7528funcs\u65b9\u6cd5\u65f6\uff0c\u4f20\u5165\u4e0d\u540c\u7684\u5bf9\u8c61\u4ee5\u6267\u884c\u4e0d\u540c\u7684func\u65b9\u6cd5\uff0c\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n

class Bus028(Passenger_car):\r\n    def __init__(self,weight):\r\n        self.weight=weight\r\n        print(\"28\u8def\u516c\u4ea4\u8f66\")\r\n        print(weight,\"t\")\r\n    def func(self):\r\n        print(\"28\u8def\u516c\u4ea4\u8f66\u7684\u9a7e\u9a76\u65b9\u6cd5\")\r\nclass Bus906(Passenger_car):\r\n    def __init__(self,weight):\r\n        self.weight=weight\r\n        print(\"906\u8def\u516c\u4ea4\u8f66\")\r\n        print(weight,\"t\")\r\n    def func(self):\r\n        print(\"906\u8def\u516c\u4ea4\u8f66\u7684\u9a7e\u9a76\u65b9\u6cd5\")\r\nclass BusB32(Passenger_car):\r\n    def __init__(self,weight):\r\n        self.weight=weight\r\n        print(\"B32\u8def\u516c\u4ea4\u8f66\")\r\n        print(weight,\"t\")\r\n    def func(self):\r\n        print(\"B32\u8def\u516c\u4ea4\u8f66\u7684\u9a7e\u9a76\u65b9\u6cd5\")\r\ndef funcs(obj):        #\u6b64\u5904obj\u8868\u793a\u4f20\u5165\u4e00\u4e2a\u5b9e\u4f8b\u5316\u5bf9\u8c61\r\n    obj.func()<\/pre>\n
\u4e3b\u51fd\u6570\uff1a<\/strong><\/span><\/div>\n
from demo01 import *\r\ndef main():\r\n    car01=Car(5)    #\u4f8b\u5316\u4e3a\u5e26\u6709\u91cd\u91cf\u4e3a5t\u7684\u4e00\u4e2a\u5177\u4f53\u5bf9\u8c61\r\n    car01.func()\r\n    passenger_car01=Passenger_car(20)    #\u4f8b\u5316\u4e3a\u5e26\u6709\u91cd\u91cf\u4e3a20t\u7684\u4e00\u4e2a\u5177\u4f53\u5bf9\u8c61\r\n    passenger_car01.func()\r\n    bus028_01=Bus028(15)        #\u4f8b\u5316\u4e3a\u5e26\u6709\u91cd\u91cf\u4e3a15t\u7684\u4e00\u4e2a\u5177\u4f53\u5bf9\u8c61\r\n    bus906_01=Bus906(15)        #\u4f8b\u5316\u4e3a\u5e26\u6709\u91cd\u91cf\u4e3a15t\u7684\u4e00\u4e2a\u5177\u4f53\u5bf9\u8c61\r\n    busB32_01=BusB32(15)        #\u4f8b\u5316\u4e3a\u5e26\u6709\u91cd\u91cf\u4e3a15t\u7684\u4e00\u4e2a\u5177\u4f53\u5bf9\u8c61\r\n    funcs(bus028_01)            #\u8c03\u7528funcs\u65b9\u6cd5\r\n    funcs(bus906_01)\r\n    funcs(busB32_01)\r\n  \r\nif __name__==\"__main__\":\r\n    main()<\/pre>\n

\u53ef\u4ee5\u770b\u5230\uff0c\u6211\u5c06\u5c0f\u6c7d\u8f66\u5b9e\u4f8b\u5316\u4e3a\u5e26\u6709\u91cd\u91cf\u4e3a5t\u7684\u4e00\u4e2a\u5177\u4f53\u5bf9\u8c61\uff0c\u5c06\u5ba2\u8f66\u5b9e\u4f8b\u5316\u4e3a\u5e26\u6709\u91cd\u91cf\u4e3a20t\u7684\u4e00\u4e2a\u5177\u4f53\u5bf9\u8c61\uff0c\u5c06\u4e09\u4e2a\u516c\u4ea4\u8f66\u5b9e\u4f8b\u5316\u4e3a\u5e26\u6709\u91cd\u91cf\u4e3a15t\u7684\u4e00\u4e2a\u5177\u4f53\u5bf9\u8c61.<\/p>\n

\u5982\u4e0a\u56fe\u6240\u793a\uff0c\u6211\u6bcf\u6b21\u5728\u8c03\u7528funcs\u65b9\u6cd5\u65f6\u90fd\u4f20\u5165\u4e86\u4e00\u4e2a\u5b9e\u4f8b\u5316\u5bf9\u8c61\uff0cfuncs\u6839\u636e\u4e0d\u540c\u7684\u5bf9\u8c61\u6267\u884c\u76f8\u5e94\u7684\u5185\u90e8\u65b9\u6cd5\u3002<\/p>\n

\u6ce8\uff1a\uff08\u6211\u4e60\u60ef\u4e0a\u5c06\u65b9\u6cd5\u8bfb\u4f5c\u51fd\u6570\uff0c\u5728\u4ee5\u540e\u7684\u6587\u7ae0\u4e2d\uff0c\u65b9\u6cd5\u4e00\u5f8b\u88ab\u51fd\u6570\u4ee3\u66ff\uff09<\/p>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8ePython\u7c7b\u7684\u7ee7\u627f\u4e0e\u591a\u6001\u8be6\u7ec6\u4ecb\u7ecd\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86\u3002<\/p>\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 \u7c7b\u53d8\u91cf\uff1a\u7c7b\u53d8\u91cf\u5728\u6574\u4e2a\u5b9e\u4f8b\u5316\u7684\u5bf9\u8c61\u4e2d\u662f\u516c\u7528\u7684\u3002\u7c7b […]<\/p>\n","protected":false},"author":668,"featured_media":160711,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-243951","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\/243951","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\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=243951"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/243951\/revisions"}],"predecessor-version":[{"id":243955,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/243951\/revisions\/243955"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/160711"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=243951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=243951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=243951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}