{"id":386,"date":"2022-02-09T15:10:48","date_gmt":"2022-02-09T07:10:48","guid":{"rendered":"http:\/\/47.98.165.52\/?p=386"},"modified":"2022-02-09T17:16:26","modified_gmt":"2022-02-09T09:16:26","slug":"cls%e5%90%ab%e4%b9%89%e5%8f%8a%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"\/?p=386","title":{"rendered":"cls\u542b\u4e49\u53ca\u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"<p>cls\u542b\u4e49\u53ca\u4f7f\u7528\u65b9\u6cd5<\/p>\n<h2>\u4e00\u3001cls\u542b\u4e49<\/h2>\n<p>python\u4e2dcls\u4ee3\u8868\u7684\u662f\u7c7b\u7684\u672c\u8eab\uff0c\u76f8\u5bf9\u5e94\u7684self\u5219\u662f\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\u5bf9\u8c61\u3002<\/p>\n<h2>\u4e8c\u3001cls\u7528\u6cd5<\/h2>\n<p>\u56e0\u4e3acls\u7b49\u540c\u4e8e\u7c7b\u672c\u8eab\uff0c\u7c7b\u65b9\u6cd5\u4e2d\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528cls\u6765\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n<p>\u901a\u8fc7\u89c2\u5bdfprint\u8f93\u51fa\u7ed3\u679c\uff0c\u4e0d\u96be\u770b\u51faself\u662f\u7ecf\u8fc7\u5b9e\u4f8b\u5316\u5e76\u5206\u914d\u4e86\u5185\u5b58\uff0ccls\u5373\u4e3a\u7c7b\u672c\u8eab\u3002<\/p>\n<pre><code class=\"language-python\">class Person(object):\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n        print(&#039;self:&#039;, self)\n\n    # \u5b9a\u4e49\u4e00\u4e2abuild\u65b9\u6cd5\uff0c\u8fd4\u56de\u4e00\u4e2aperson\u5b9e\u4f8b\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u7b49\u4ef7\u4e8ePerson()\u3002\n    @classmethod\n    def build(cls):\n        # cls()\u7b49\u4e8ePerson()\n        p = cls(&quot;Tom&quot;, 18)\n        print(&#039;cls:&#039;, cls)\n        return p\n\nif __name__ == &#039;__main__&#039;:\n    person = Person.build()\n    print(person, person.name, person.age)<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">In [71]: runfile(&#039;C:\/Users\/Administrator\/.spyder-py3\/temp.py&#039;, wdir=&#039;C:\/Users\/Administrator\/.spyder-py3&#039;)\nself: &lt;__main__.Person object at 0x00000153E5DBF048&gt;\ncls: &lt;class &#039;__main__.Person&#039;&gt;\n&lt;__main__.Person object at 0x00000153E5DBF048&gt; Tom 18<\/code><\/pre>\n<h2>classmethod\u4f7f\u7528\u573a\u666f<\/h2>\n<p>\u5b98\u65b9\u7684\u8bf4\u6cd5\uff1a<br \/>\nclassmethod(function)<br \/>\n\u4e2d\u6587\u8bf4\u660e\uff1a<br \/>\nclassmethod\u662f\u7528\u6765\u6307\u5b9a\u4e00\u4e2a\u7c7b\u7684\u65b9\u6cd5\u4e3a\u7c7b\u65b9\u6cd5\uff0c\u6ca1\u6709\u6b64\u53c2\u6570\u6307\u5b9a\u7684\u7c7b\u7684\u65b9\u6cd5\u4e3a\u5b9e\u4f8b\u65b9\u6cd5\uff0c\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">class C:\n    @classmethod\n    def f(cls, arg1, arg2, ...): ...<\/code><\/pre>\n<p>\u770b\u540e\u4e4b\u540e\u771f\u662f\u4e00\u5934\u96fe\u6c34\u3002\u8bf4\u7684\u5565\u5b50\u4e1c\u897f\u5462\uff1f\uff1f\uff1f<\/p>\n<p>\u81ea\u5df1\u5230\u56fd\u5916\u7684\u8bba\u575b\u770b\u5176\u4ed6\u7684\u4f8b\u5b50\u548c\u89e3\u91ca\uff0c\u987f\u65f6\u5c31\u5f88\u660e\u6717\u3002 \u4e0b\u9762\u81ea\u5df1\u7528\u4f8b\u5b50\u6765\u8bf4\u660e\u3002<\/p>\n<p>\u770b\u4e0b\u9762\u7684\u5b9a\u4e49\u7684\u4e00\u4e2a\u65f6\u95f4\u7c7b\uff1a<\/p>\n<pre><code class=\"language-python\">class Data_test(object):\n    day=0\n    month=0\n    year=0\n    def __init__(self,year=0,month=0,day=0):\n        self.day=day\n        self.month=month\n        self.year=year\n\n    def out_date(self):\n        print &quot;year :&quot;\n        print self.year\n        print &quot;month :&quot;\n        print self.month\n        print &quot;day :&quot;\n        print self.day\nt=Data_test(2016,8,1)\nt.out_date()<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">year :\n2016\nmonth :\n8\nday :\n1<\/code><\/pre>\n<p>\u7b26\u5408\u671f\u671b\u3002<\/p>\n<p>\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f &quot;2016-8-1&quot; \u8fd9\u6837\u7684\u5b57\u7b26\u683c\u5f0f\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u8c03\u7528Date_test \u7c7b\u524d\u505a\u4e00\u4e0b\u5904\u7406\uff1a<\/p>\n<pre><code class=\"language-python\">string_date=&#039;2016-8-1&#039;\nyear,month,day=map(int,string_date.split(&#039;-&#039;))\ns=Data_test(year,month,day)<\/code><\/pre>\n<p>\u5148\u628a\u20182016-8-1\u2019 \u5206\u89e3\u6210 year\uff0cmonth\uff0cday \u4e09\u4e2a\u53d8\u91cf\uff0c\u7136\u540e\u8f6c\u6210int\uff0c\u518d\u8c03\u7528Date_test(year,month,day)\u51fd\u6570\u3002 \u4e5f\u5f88\u7b26\u5408\u671f\u671b\u3002<\/p>\n<p>\u90a3\u6211\u53ef\u4e0d\u53ef\u4ee5\u628a\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5904\u7406\u7684\u51fd\u6570\u653e\u5230 Date_test \u7c7b\u5f53\u4e2d\u5462\uff1f<\/p>\n<p>\u90a3\u4e48@classmethod \u5c31\u5f00\u59cb\u51fa\u573a\u4e86<\/p>\n<pre><code class=\"language-python\">class Data_test2(object):\n    day=0\n    month=0\n    year=0\n    def __init__(self,year=0,month=0,day=0):\n        self.day=day\n        self.month=month\n        self.year=year\n\n    @classmethod\n    def get_date(cls,\nstring_date):\n        #\u8fd9\u91cc\u7b2c\u4e00\u4e2a\u53c2\u6570\u662fcls\uff0c \u8868\u793a\u8c03\u7528\u5f53\u524d\u7684\u7c7b\u540d\n        year,month,day=map(int,string_date.split(&#039;-&#039;))\n        date1=cls(year,month,day)\n        #\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u521d\u59cb\u5316\u540e\u7684\u7c7b\n        return date1\n\n    def out_date(self):\n        print &quot;year :&quot;\n        print self.year\n        print &quot;month :&quot;\n        print self.month\n        print &quot;day :&quot;\n        print self.day<\/code><\/pre>\n<p>\u5728Date_test\u7c7b\u91cc\u9762\u521b\u5efa\u4e00\u4e2a\u6210\u5458\u51fd\u6570\uff0c \u524d\u9762\u7528\u4e86@classmethod\u88c5\u9970\u3002 \u5b83\u7684\u4f5c\u7528\u5c31\u662f\u6709\u70b9\u50cf\u9759\u6001\u7c7b\uff0c\u6bd4\u9759\u6001\u7c7b\u4e0d\u4e00\u6837\u7684\u5c31\u662f\u5b83\u53ef\u4ee5\u4f20\u8fdb\u6765\u4e00\u4e2a\u5f53\u524d\u7c7b\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002<\/p>\n<p>\u90a3\u4e48\u5982\u4f55\u8c03\u7528\u5462\uff1f<\/p>\n<pre><code class=\"language-python\">r=Data_test2.get_date(&quot;2016-8-6&quot;)\nr.out_date()<\/code><\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre><code class=\"language-python\">year :\n2016\nmonth :\n8\nday :\n1<\/code><\/pre>\n<p>\u8fd9\u6837\u5b50\u7b49\u4e8e\u5148\u8c03\u7528get_date\uff08\uff09\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5904\u7406\uff0c\u7136\u540e\u624d\u4f7f\u7528Data_test\u7684\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u3002<\/p>\n<p>\u8fd9\u6837\u7684\u597d\u5904\u5c31\u662f\u4f60\u4ee5\u540e\u91cd\u6784\u7c7b\u7684\u65f6\u5019\u4e0d\u5fc5\u8981\u4fee\u6539\u6784\u9020\u51fd\u6570\uff0c\u53ea\u9700\u8981\u989d\u5916\u6dfb\u52a0\u4f60\u8981\u5904\u7406\u7684\u51fd\u6570\uff0c\u7136\u540e\u4f7f\u7528\u88c5\u9970\u7b26 @classmethod \u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\uff1a<a href=\"http:\/\/30daydo.com\/article\/89\">http:\/\/30daydo.com\/article\/89<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>cls\u542b\u4e49\u53ca\u4f7f\u7528\u65b9\u6cd5 \u4e00\u3001cls\u542b\u4e49 python\u4e2dcls\u4ee3\u8868\u7684\u662f\u7c7b\u7684\u672c\u8eab\uff0c\u76f8\u5bf9\u5e94\u7684self\u5219\u662f\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\u5bf9\u8c61\u3002 \u4e8c\u3001cls\u7528\u6cd5 \u56e0\u4e3a   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[35],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/386"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=386"}],"version-history":[{"count":2,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/386\/revisions"}],"predecessor-version":[{"id":388,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/386\/revisions\/388"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=386"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}