{"id":2122,"date":"2023-11-10T15:10:37","date_gmt":"2023-11-10T07:10:37","guid":{"rendered":""},"modified":"2023-11-10T15:10:37","modified_gmt":"2023-11-10T07:10:37","slug":"Python\u9762\u5411\u5bf9\u8c61\u7684\u6a21\u5f0f","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/2122.html","title":{"rendered":"Python\u9762\u5411\u5bf9\u8c61\u7684\u6a21\u5f0f"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Python\u9762\u5411\u5bf9\u8c61\u7684\u6a21\u5f0f<\/h1>\n

Python\u9762\u5411\u5bf9\u8c61\u7684\u6a21\u5f0f\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n \u9762\u5411\u5bf9\u8c61\u7684\u6a21\u5f0f\u662f\u6700\u5e38\u7528\u7684\u6a21\u5f0f\u3002\u8fd9\u79cd\u6a21\u5f0f\u51e0\u4e4e\u53ef\u4ee5\u5728\u6bcf\u79cd\u7f16\u7a0b\u8bed\u8a00\u4e2d\u627e\u5230\u3002\n <\/div>\n

\u5982\u4f55\u5b9e\u73b0\u9762\u5411\u5bf9\u8c61\u7684\u6a21\u5f0f\uff1f<\/h2>\n
\n \u73b0\u5728\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u5b9e\u73b0\u9762\u5411\u5bf9\u8c61\u7684\u6a21\u5f0f\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
class <\/span>Parrot:
   # class <\/span>attribute
<\/span>    species = \"bird\"<\/span>

   # instance attribute
<\/span>    def <\/span>__init__(self, name, age):
      self.name = name
      self.age = age

# instantiate the Parrot class
<\/span> blu = Parrot(\"Blu\"<\/span>, 10)
woo = Parrot(\"Woo\"<\/span>, 15)
# access the class <\/span>attributes
<\/span> print(\"Blu is <\/span>a {}\"<\/span>.format<\/span>(blu.__class__.species))
print(\"Woo is <\/span>also a {}\"<\/span>.format<\/span>(woo.__class__.species))
# access the instance attributes
<\/span> print(\"{} is <\/span>{} years old\"<\/span><\/span>.format<\/span>( blu.name, blu.age))
print(\"{} is <\/span>{} years old\".format<\/span>( woo.name, woo.age))
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8f93\u51fa<\/h3>\n
\n \u4e0a\u9762\u7684\u7a0b\u5e8f\u751f\u6210\u4ee5\u4e0b\u8f93\u51fa\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
Blu is <\/span>a bird
Woo is <\/span>also a bird
Blu is <\/span>10 years old
Woo is <\/span>15 years old
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8bf4\u660e<\/h3>\n
\n \u8be5\u4ee3\u7801\u5305\u62ec\u7c7b\u5c5e\u6027\u548c\u5b9e\u4f8b\u5c5e\u6027\uff0c\u5b83\u4eec\u6839\u636e\u8f93\u51fa\u7684\u8981\u6c42\u8fdb\u884c\u6253\u5370\u3002\u6709\u591a\u79cd\u529f\u80fd\u6784\u6210\u9762\u5411\u5bf9\u8c61\u6a21\u5f0f\u7684\u4e00\u90e8\u5206\u3002
\n
\u4e0b\u9762\u91cd\u70b9\u4ecb\u7ecd\u4f7f\u7528\u9762\u5411\u5bf9\u8c61\u7684\u6982\u5ff5\u53ca\u5176\u5728Python\u4e2d\u7684\u5b9e\u73b0\u7684\u6a21\u5f0f\u3002\u5f53\u6211\u4eec\u56f4\u7ed5\u8bed\u53e5\u5757\u8bbe\u8ba1\u7a0b\u5e8f\u65f6\uff0c\u8fd9\u4e9b\u8bed\u53e5\u5757\u56f4\u7ed5\u51fd\u6570\u64cd\u4f5c\u6570\u636e\uff0c\u8fd9\u79f0\u4e3a\u9762\u5411\u8fc7\u7a0b\u7684\u7a0b\u5e8f\u8bbe\u8ba1\u3002\u5728\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u4e2d\uff0c\u6709\u4e24\u4e2a\u4e3b\u8981\u7684\u5b9e\u4f8b\uff0c\u5206\u522b\u79f0\u4e3a\u7c7b\u548c\u5bf9\u8c61\u3002\n <\/div>\n

\u5982\u4f55\u5b9e\u73b0\u7c7b\u548c\u5bf9\u8c61\u53d8\u91cf\uff1f<\/h2>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
class <\/span>Robot:
    population = 0
    def <\/span>__init__(self, name):
        self.name = name
        print(\"(Initializing {})\"<\/span>.format<\/span>(self.name))
        Robot.population += 1
    def <\/span>die(self):
        print(\"{} is <\/span>being destroyed!\"<\/span>.format<\/span>(self.name))
        Robot.population -= 1
        if <\/span>Robot.population == 0:
            print(\"{} was the last one.\"<\/span>.format<\/span>(self.name))
        else:<\/span>
            print(\"There are still {:d} robots working.\"<\/span>.format<\/span>(
                Robot.population))
    def <\/span>say_hi(self):
        print(\"Greetings, my masters call me {}.\"<\/span>.format<\/span>(self.name))
    @classmethod
    def <\/span>how_many(cls):
        print(\"We have {:d} robots.\"<\/span>.format<\/span>(cls.population))
droid1 = Robot(\"R2-D2\"<\/span>)
droid1.say_hi<\/span>()
Robot.how_many<\/span>()
droid2 = Robot(\"C-3PO\"<\/span>)
droid2.say_hi<\/span>()
Robot.how_many<\/span>()
print(\"\\nRobots can do some work here.\\n\"<\/span>)
print(\"Robots have finished their work. So let'<\/span>s destroy them.\")
droid1.die<\/span>()
droid2.die<\/span>()
Robot.how_many<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
(Initializing R2-D2)
Greetings, my masters call me R2-D2.
We have 1 robots.
(Initializing C-3PO)
Greetings, my masters call me C-3PO.
We have 2 robots.
Robots can do some work here.
Robots have finished their work. So let's destroy them.
R2-D2 is <\/span>being destroyed!
There are still 1 robots working.
C-3PO is <\/span>being destroyed!
C-3PO was the last one.
We have 0 robots.
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8bf4\u660e\uff1a\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u603b\u4f53\u7c7b\u53d8\u91cf\u79f0\u4e3aRobot.population\uff0c\u800c\u4e0d\u662fself.population\u3002<\/p>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python\u9762\u5411\u5bf9\u8c61\u7684\u6a21\u5f0fzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[164],"tags":[],"class_list":["post-2122","post","type-post","status-publish","format-standard","hentry","category-python-sjms"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/2122"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=2122"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/2122\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=2122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=2122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=2122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}