{"id":1035,"date":"2023-03-24T09:47:07","date_gmt":"2023-03-24T01:47:07","guid":{"rendered":""},"modified":"2023-03-24T09:47:07","modified_gmt":"2023-03-24T01:47:07","slug":"Python \u7ee7\u627f","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1035.html","title":{"rendered":"Python \u7ee7\u627f"},"content":{"rendered":"


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

\n

Python \u7ee7\u627f<\/h1>\n

\u641e\u61c2Python\u7c7b\u7ee7\u627f\u7684\u7528\u6cd5<\/span>\n <\/div>\n

\n \u7ee7\u627f\u5141\u8bb8\u5b9a\u4e49\u7ee7\u627f\u53e6\u4e00\u4e2a\u7c7b\u7684\u6240\u6709\u65b9\u6cd5\u548c\u5c5e\u6027\u7684\u7c7b\u3002\u7236\u7c7b\u662f\u7ee7\u627f\u7684\u7c7b\uff0c\u4e5f\u79f0\u4e3a\u57fa\u7c7b\u3002\u5b50\u7c7b\u662f\u4ece\u53e6\u4e00\u4e2a\u7c7b\u7ee7\u627f\u7684\u7c7b\uff0c\u4e5f\u79f0\u4e3a\u6d3e\u751f\u7c7b\u3002\n <\/div>\n

\u521b\u5efa\u7236\u7c7b<\/h2>\n
\n \u521b\u5efa\u4e00\u4e2a\u540d\u4e3a Person \u7684\u7c7b\uff0c\u5176\u4e2d\u5305\u542b firstname \u548c lastname \u5c5e\u6027\u4ee5\u53ca printname \u65b9\u6cd5\uff1a\n <\/div>\n
\n
 class<\/span> Animal:<\/span>
    def<\/span> __init__(self, name, age):<\/span>
        self.name<\/span> = name<\/span>
        self.age<\/span> = age<\/span>

    def<\/span> printinfo(self):<\/span>
        print(\"name is %s , age is %d \" %(self.name,self.age)<\/span>)

# \u5b9e\u73b0\u7279\u5b9a\u529f\u80fd\u7684\u591a\u884c\u4ee3\u7801\u4f7f\u7528 Animal \u6765\u521b\u5efa\u5bf9\u8c61\uff0c\u7136\u540e\u6267\u884c printinfo \u65b9\u6cd5\uff1a<\/span>
x<\/span> = Animal(\"dog\", 6)<\/span>
x<\/span>.printinfo()<\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
name is dog , age is 6<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u521b\u5efa\u5b50\u7c7b<\/h2>\n
\n \u521b\u5efa\u4e00\u4e2a\u540d\u4e3a Cat \u7684\u7c7b\uff0c\u5b83\u5c06\u4ece Animal \u7c7b\u7ee7\u627f\u5c5e\u6027\u548c\u65b9\u6cd5\uff1a\n <\/div>\n
\n
 class<\/span> Cat(Animal):<\/span>
    pass<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6ce8\u610f\uff1a<\/span>\u5982\u679c\u60a8\u4e0d\u60f3\u5411\u8be5\u7c7b\u6dfb\u52a0\u4efb\u4f55\u5176\u4ed6\u5c5e\u6027\u6216\u65b9\u6cd5\uff0c\u8bf7\u4f7f\u7528 pass \u5173\u952e\u5b57\u3002\n <\/div>\n
\n \u73b0\u5728\uff0cCat \u7c7b\u62e5\u6709\u4e0e Animal \u7c7b\u76f8\u540c\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\n <\/div>\n
\n \u4f7f\u7528 Cat \u7c7b\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u7136\u540e\u6267\u884c printinfo \u65b9\u6cd5\uff1a\n <\/div>\n
\n
 class<\/span> Animal:<\/span>
    def<\/span> __init__(self, name, age):<\/span>
        self.name<\/span> = name<\/span>
        self.age<\/span> = age<\/span>

    def<\/span> printinfo(self):<\/span>
        print(\"name is %s , age is %d \" %(self.name,self.age)<\/span>)

class<\/span> Cat(Animal):<\/span>
    pass<\/span> # \u5b9e\u73b0\u7279\u5b9a\u529f\u80fd\u7684\u591a\u884c\u4ee3\u7801\u4f7f\u7528 Animal \u6765\u521b\u5efa\u5bf9\u8c61\uff0c\u7136\u540e\u6267\u884c printinfo \u65b9\u6cd5\uff1a<\/span>
x<\/span> = Cat(\"cat\", 8)<\/span>
x<\/span>.printinfo()<\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
name is cat , age is 8<\/span><\/code><\/pre>\n<\/p><\/div>\n

__init__() \u51fd\u6570<\/h2>\n
\n \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u521b\u5efa\u4e86\u4e00\u4e2a\u5b50\u7c7b\uff0c\u5b83\u7ee7\u627f\u4e86\u7236\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\n <\/div>\n
\n \u6211\u4eec\u60f3\u8981\u628a
\n __init__()<\/b> \u51fd\u6570\u6dfb\u52a0\u5230\u5b50\u7c7b\uff08\u800c\u4e0d\u662f
\n pass<\/b> \u5173\u952e\u5b57\uff09\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\u6bcf\u6b21\u4f7f\u7528\u7c7b\u521b\u5efa\u65b0\u5bf9\u8c61\u65f6\uff0c\u90fd\u4f1a\u81ea\u52a8\u8c03\u7528 __init__() \u51fd\u6570\u3002\n <\/div>\n
\n \u4e3a Cat \u7c7b\u6dfb\u52a0 __init__() \u51fd\u6570\n <\/div>\n
\n
 class<\/span> Cat(Animal):<\/span>
    def<\/span> __init__(self, name, age):<\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6ce8\u610f\uff1a<\/span>\u5f53\u60a8\u6dfb\u52a0
\n __init__()<\/b>\u51fd\u6570\u65f6\uff0c\u5b50\u7c7b\u5c06\u4e0d\u518d\u7ee7\u627f\u7236\u7684
\n __init__()<\/b>\u51fd\u6570\u3002\u5b50\u7684 __init__() \u51fd\u6570\u4f1a\u8986\u76d6\u5bf9\u7236\u7684 __init__() \u51fd\u6570\u7684\u7ee7\u627f\n <\/div>\n
\n \u5982\u9700\u4fdd\u6301\u7236\u7684 __init__() \u51fd\u6570\u7684\u7ee7\u627f\uff0c\u8bf7\u6dfb\u52a0\u5bf9\u7236\u7684 __init__() \u51fd\u6570\u7684\u8c03\u7528\uff1a\n <\/div>\n
\n
 class<\/span> Animal:<\/span>
    def<\/span> __init__(self, name, age):<\/span>
        self.name<\/span> = name<\/span>
        self.age<\/span> = age<\/span>

    def<\/span> printinfo(self):<\/span>
        print(\"name is %s , age is %d \" %(self.name,self.age)<\/span>)

class<\/span> Cat(Animal):<\/span>
    def<\/span> __init__(self, name, age):<\/span>
        Animal.<\/span>__init__(self, name, age)<\/span>
x<\/span> = Cat(\"cat\", 10)<\/span>
x<\/span>.printinfo()<\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
name is cat , age is 10<\/span><\/code><\/pre>\n<\/p><\/div>\n

super() \u51fd\u6570<\/h2>\n
\n Python \u8fd8\u6709\u4e00\u4e2a
\n super()<\/b> \u51fd\u6570\uff0c\u5b83\u4f1a\u4f7f\u5b50\u7c7b\u4ece\u5176\u7236\u7ee7\u627f\u6240\u6709\u65b9\u6cd5\u548c\u5c5e\u6027\uff1a\n <\/div>\n
\n \u901a\u8fc7\u4f7f\u7528 super() \u51fd\u6570\uff0c\u60a8\u4e0d\u5fc5\u4f7f\u7528\u7236\u5143\u7d20\u7684\u540d\u79f0\uff0c\u5b83\u5c06\u81ea\u52a8\u4ece\u5176\u7236\u5143\u7d20\u7ee7\u627f\u65b9\u6cd5\u548c\u5c5e\u6027\u3002\n <\/div>\n
\n
 class<\/span> Animal:<\/span>
    def<\/span> __init__(self, name, age):<\/span>
        self.name<\/span> = name<\/span>
        self.age<\/span> = age<\/span>

    def<\/span> printinfo(self):<\/span>
        print(\"name is %s , age is %d \" %(self.name,self.age)<\/span>)

class<\/span> Cat(Animal):<\/span>
    def<\/span> __init__(self, name, age):<\/span>
        super().<\/span>__init__(name, age)<\/span>
x<\/span> = Cat(\"cat\", 12)<\/span>
x<\/span>.printinfo()<\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
name is cat , age is 12<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u6dfb\u52a0\u5c5e\u6027<\/h2>\n
\n \u6bd4\u5982\u628a\u5c5e\u6027color\u6dfb\u52a0\u5230 Cat \u7c7b\u4e2d\uff1a\n <\/div>\n
\n
 class<\/span> Animal:<\/span>
    def<\/span> __init__(self, name, age):<\/span>
        self.name<\/span> = name<\/span>
        self.age<\/span> = age<\/span>

    def<\/span> printinfo(self):<\/span>
        print(\"name is %s , age is %d \" %(self.name,self.age)<\/span>)

class<\/span> Cat(Animal):<\/span>
    def<\/span> __init__(self, name, age):<\/span>
        super().<\/span>__init__(name, age)<\/span>
        self.color<\/span> = \"block\"<\/span>
x<\/span> = Cat(\"cat\", 12)<\/span>
print(<\/span>\"name is %s , age is %d , color is %s\" %(x.name,x.age,x.color)<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
name is cat , age is 12 , color is block<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728\u8fd9\u4f8b\u5b50\u4e2d\uff0cblock \u5e74\u5e94\u8be5\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u5e76\u5728\u521b\u5efa Cat \u5bf9\u8c61\u65f6\u4f20\u9012\u5230 Cat \u7c7b\u3002\u4e3a\u6b64\uff0c\u8bf7\u5728 __init__() \u51fd\u6570\u4e2d\u6dfb\u52a0\u53e6\u4e00\u4e2a\u53c2\u6570\uff1a\n <\/div>\n
\n
 class<\/span> Animal:<\/span>
    def<\/span> __init__(self, name, age):<\/span>
        self.name<\/span> = name<\/span>
        self.age<\/span> = age<\/span>

    def<\/span> printinfo(self):<\/span>
        print(\"name is %s , age is %d \" %(self.name,self.age)<\/span>)

class<\/span> Cat(Animal):<\/span>
    def<\/span> __init__(self, name, age,color):<\/span>
        super().<\/span>__init__(name, age)<\/span>
        self.color<\/span> = color<\/span>
x<\/span> = Cat(\"cat\", 12,\"block\")<\/span>
print(<\/span>\"name is %s , age is %d , color is %s\" %(x.name,x.age,x.color)<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
name is cat , age is 12 , color is block<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u6dfb\u52a0\u65b9\u6cd5<\/h2>\n
\n \u628a\u540d\u4e3a say \u7684\u65b9\u6cd5\u6dfb\u52a0\u5230 Cat \u7c7b\uff1a\n <\/div>\n
\n
 class<\/span> Animal:<\/span>
    def<\/span> __init__(self, name, age):<\/span>
        self.name<\/span> = name<\/span>
        self.age<\/span> = age<\/span>

    def<\/span> printinfo(self):<\/span>
        print(\"name is %s , age is %d \" %(self.name,self.age)<\/span>)

class<\/span> Cat(Animal):<\/span>
    def<\/span> __init__(self, name, age,color):<\/span>
        super().<\/span>__init__(name, age)<\/span>
        self.color<\/span> = \"block\"<\/span>
    def<\/span> say(self):<\/span>
        print(<\/span>\"name is %s , age is %d , color is %s\" %(self.name,self.age,self.color)<\/span>)
x<\/span> = Cat(\"cat\", 12,\"block\")<\/span>
x<\/span>.say()<\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
name is cat , age is 12 , color is block<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python \u7ee7\u627fzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-1035","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1035"}],"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=1035"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1035\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}