[{"data":1,"prerenderedAt":5740},["ShallowReactive",2],{"post-2025-07-13-python":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"tags":11,"body":14,"_type":5734,"_id":5735,"_source":5736,"_file":5737,"_stem":5738,"_extension":5739},"\u002Fblog\u002F2025-07-13-python","blog",false,"","Python-面向对象编程","Python面向对象编程：类与对象、继承、多态、封装、魔术方法与设计模式。","2025-07-13",[12,13],"Python","基础",{"type":15,"children":16,"toc":5699},"root",[17,22,30,42,47,54,59,68,101,109,118,124,141,149,154,160,168,175,187,204,212,218,230,238,249,255,272,280,286,291,297,311,350,355,436,441,472,478,483,488,502,572,577,600,605,613,626,639,673,701,713,770,775,783,795,800,854,865,873,930,1036,1044,1090,1103,1212,1218,1296,1309,1426,1432,1551,1564,1697,1703,1940,1945,2090,2128,2134,2139,2145,2155,2178,2182,2227,2235,2240,2254,2258,2300,2305,2313,2356,2502,2507,2515,2521,2529,2535,2545,2553,2558,2651,2656,2670,2675,2683,2688,2693,2701,2720,2875,2880,2888,2899,2922,2930,2935,2943,2951,2957,2967,2972,3056,3060,3074,3078,3085,3101,3109,3119,3211,3216,3224,3230,3266,3320,3361,3431,3436,3444,3449,3501,3538,3637,3642,3650,3682,3688,3706,3712,3744,3756,3786,3790,3850,3869,3969,3974,3982,3997,4005,4011,4029,4050,4142,4147,4155,4167,4190,4195,4203,4212,4217,4225,4251,4435,4440,4448,4453,4461,4467,4473,4478,4489,4501,4532,4536,4619,4627,4698,4868,4899,4907,4913,4918,4951,5025,5037,5045,5051,5138,5258,5263,5271,5298,5312,5320,5325,5333,5345,5353,5427,5680,5685,5693],{"type":18,"tag":19,"props":20,"children":21},"element","hr",{},[],{"type":18,"tag":23,"props":24,"children":26},"h2",{"id":25},"_1-面向对象概述",[27],{"type":28,"value":29},"text","1 面向对象概述",{"type":18,"tag":31,"props":32,"children":33},"p",{},[34,40],{"type":18,"tag":35,"props":36,"children":37},"strong",{},[38],{"type":28,"value":39},"面向对象",{"type":28,"value":41},"（Object Oriented）的英文缩写是OO，它是一种设计思想。从20世纪60年代提出面向对象的概念到现在，它已经发展成为一种比较成熟的编程思想，并且逐步成为目前软件开发领域的主流技术。如我们经常听说的面向对象编程（Object Oriented Programming，即OOP）就是主要针对大型软件设计而提出的，它可以使软件设计更加灵活，并且能更好地进行代码复用。",{"type":18,"tag":31,"props":43,"children":44},{},[45],{"type":28,"value":46},"面向对象中的对象（Object），通常是指客观世界中存在的对象，具有唯一性，对象之间各不相同，各有各的特点，每一个对象都有自己的运动规律和内部状态；对象与对象之间又是可以相互联系、相互作用的。另外，对象也可以是一个抽象的事物，例如，可以从圆形、正方形、三角形等图形抽象出一个简单图形，简单图形就是一个对象，它有自己的属性和行为，图形中边的个数是它的属性，图形的面积也是它的属性，输出图形的面积就是它的行为。概括地讲，面向对象技术是一种从组织结构上模拟客观世界的方法。",{"type":18,"tag":48,"props":49,"children":51},"h3",{"id":50},"_11对象",[52],{"type":28,"value":53},"1.1  对象",{"type":18,"tag":31,"props":55,"children":56},{},[57],{"type":28,"value":58},"对象是一个抽象概念，英文称作“Object”，表示任意存在的事物。世间万物皆对象！现实世界中，随处可见的一种事物就是对象，对象是事物存在的实体，如一个人，如图所示。",{"type":18,"tag":31,"props":60,"children":61},{},[62],{"type":18,"tag":63,"props":64,"children":67},"img",{"alt":65,"src":66},"Pasted image 20250809082544","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250809082544.png",[],{"type":18,"tag":31,"props":69,"children":70},{},[71,73,78,80,85,87,92,94,99],{"type":28,"value":72},"通常将对象划分为两个部分，即",{"type":18,"tag":35,"props":74,"children":75},{},[76],{"type":28,"value":77},"静态部分",{"type":28,"value":79},"与",{"type":18,"tag":35,"props":81,"children":82},{},[83],{"type":28,"value":84},"动态部分",{"type":28,"value":86},"。",{"type":18,"tag":35,"props":88,"children":89},{},[90],{"type":28,"value":91},"静态部分被称为“属性”",{"type":28,"value":93},"，任何对象都具备自身属性，这些属性不仅是客观存在的，而且是不能被忽视的，如人的性别；",{"type":18,"tag":35,"props":95,"children":96},{},[97],{"type":28,"value":98},"动态部分指的是对象的行为",{"type":28,"value":100},"，即对象执行的动作，如人可以跑步，如下图所示。",{"type":18,"tag":31,"props":102,"children":103},{},[104],{"type":18,"tag":63,"props":105,"children":108},{"alt":106,"src":107},"Pasted image 20250809082832","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250809082832.png",[],{"type":18,"tag":110,"props":111,"children":112},"blockquote",{},[113],{"type":18,"tag":31,"props":114,"children":115},{},[116],{"type":28,"value":117},"说明：在Python中，一切都是对象。不仅是具体的事物称为对象，字符串、函数等也都是对象。这说明Python天生就是面向对象的。",{"type":18,"tag":48,"props":119,"children":121},{"id":120},"_12类",[122],{"type":28,"value":123},"1.2  类",{"type":18,"tag":31,"props":125,"children":126},{},[127,132,134,139],{"type":18,"tag":35,"props":128,"children":129},{},[130],{"type":28,"value":131},"类是封装对象的属性和行为的载体",{"type":28,"value":133},"，反过来说",{"type":18,"tag":35,"props":135,"children":136},{},[137],{"type":28,"value":138},"具有相同属性和行为的一类实体被称为类",{"type":28,"value":140},"。例如，把雁群比作大雁类，那么大雁类就具备了喙、翅膀和爪等属性，觅食、飞行和睡觉等行为，而一只要从北方飞往南方的大雁则被视为大雁类的一个对象。大雁类和大雁对象的关系如图所示。",{"type":18,"tag":31,"props":142,"children":143},{},[144],{"type":18,"tag":63,"props":145,"children":148},{"alt":146,"src":147},"Pasted image 20250809083115","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250809083115.png",[],{"type":18,"tag":31,"props":150,"children":151},{},[152],{"type":28,"value":153},"在Python语言中，类是一种抽象概念，如定义一个大雁类（Geese），在该类中，可以定义每个对象共有的属性和方法；而一只要从北方飞往南方的大雁则是大雁类的一个对象（wildGeese），对象是类的实例。",{"type":18,"tag":48,"props":155,"children":157},{"id":156},"_13面向对象程序设计的特点",[158],{"type":28,"value":159},"1.3  面向对象程序设计的特点",{"type":18,"tag":31,"props":161,"children":162},{},[163],{"type":18,"tag":35,"props":164,"children":165},{},[166],{"type":28,"value":167},"面向对象程序设计具有三大基本特征：封装、继承和多态。",{"type":18,"tag":169,"props":170,"children":172},"h4",{"id":171},"_131-封装",[173],{"type":28,"value":174},"1.3.1 封装",{"type":18,"tag":31,"props":176,"children":177},{},[178,180,185],{"type":28,"value":179},"封装是面向对象编程的核心思想，",{"type":18,"tag":35,"props":181,"children":182},{},[183],{"type":28,"value":184},"将对象的属性和行为封装起来，其载体就是类，类通常会对客户隐藏其实现细节，这就是封装的思想",{"type":28,"value":186},"。例如，用户使用计算机，只需要使用手指敲击键盘就可以实现一些功能，而不需要知道计算机内部是如何工作的。",{"type":18,"tag":31,"props":188,"children":189},{},[190,195,197,202],{"type":18,"tag":35,"props":191,"children":192},{},[193],{"type":28,"value":194},"采用封装思想保证了类内部数据结构的完整性",{"type":28,"value":196},"，使用该类的用户不能直接看到类中的数据结构，而只能执行类允许公开的数据，这样就",{"type":18,"tag":35,"props":198,"children":199},{},[200],{"type":28,"value":201},"避免了外部对内部数据的影响，提高了程序的可维护性",{"type":28,"value":203},"。使用类实现封装特性如图所示。",{"type":18,"tag":31,"props":205,"children":206},{},[207],{"type":18,"tag":63,"props":208,"children":211},{"alt":209,"src":210},"Pasted image 20250809084838","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250809084838.png",[],{"type":18,"tag":169,"props":213,"children":215},{"id":214},"_132-继承",[216],{"type":28,"value":217},"1.3.2 继承",{"type":18,"tag":31,"props":219,"children":220},{},[221,223,228],{"type":28,"value":222},"矩形、菱形、平行四边形和梯形等都是四边形。因为四边形与它们具有共同的特征：拥有4条边。只要将四边形适当地延伸，就会得到矩形、菱形、平行四边形和梯形4种图形。以平行四边形为例，如果把平行四边形看作四边形的延伸，那么平行四边形就复用了四边形的属性和行为，同时添加了平行四边形特有的属性和行为，如平行四边形的对边平行且相等。在Python中，可以把平行四边形类看作是继承四边形类后产生的类，其中，将",{"type":18,"tag":35,"props":224,"children":225},{},[226],{"type":28,"value":227},"类似于平行四边形的类称为子类，将类似于四边形的类称为父类或超类",{"type":28,"value":229},"。值得注意的是，在阐述平行四边形和四边形的关系时，可以说平行四边形是特殊的四边形，但不能说四边形是平行四边形。同理，Python中可以说子类的实例都是父类的实例，但不能说父类的实例是子类的实例，四边形类层次结构示意图如图所示。",{"type":18,"tag":31,"props":231,"children":232},{},[233],{"type":18,"tag":63,"props":234,"children":237},{"alt":235,"src":236},"Pasted image 20250809085417","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250809085417.png",[],{"type":18,"tag":31,"props":239,"children":240},{},[241,243,248],{"type":28,"value":242},"综上所述，",{"type":18,"tag":35,"props":244,"children":245},{},[246],{"type":28,"value":247},"继承是实现重复利用的重要手段，子类通过继承复用了父类的属性和行为的同时又添加了子类特有的属性和行为",{"type":28,"value":86},{"type":18,"tag":169,"props":250,"children":252},{"id":251},"_133-多态",[253],{"type":28,"value":254},"1.3.3 多态",{"type":18,"tag":31,"props":256,"children":257},{},[258,263,265,270],{"type":18,"tag":35,"props":259,"children":260},{},[261],{"type":28,"value":262},"将父类对象应用于子类的特征就是多态",{"type":28,"value":264},"。比如创建一个螺丝类，螺丝类有两个属性：粗细和螺纹密度；然后再创建了两个类，一个是长螺丝类，一个短螺丝类，并且它们都继承了螺丝类。这样长螺丝类和短螺丝类不仅具有相同的特征（粗细相同，且螺纹密度也相同），还具有不同的特征（一个长，一个短，长的可以用来固定大型支架，短的可以固定生活中的家具）。综上所述，一个螺丝类衍生出不同的子类，",{"type":18,"tag":35,"props":266,"children":267},{},[268],{"type":28,"value":269},"子类继承父类特征的同时，也具备了自己的特征，并且能够实现不同的效果，这就是多态化的结构",{"type":28,"value":271},"。螺丝类层次结构示意图如图所示。",{"type":18,"tag":31,"props":273,"children":274},{},[275],{"type":18,"tag":63,"props":276,"children":279},{"alt":277,"src":278},"Pasted image 20250809085755","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250809085755.png",[],{"type":18,"tag":23,"props":281,"children":283},{"id":282},"_2-定义并创建类的实例",[284],{"type":28,"value":285},"2 定义并创建类的实例",{"type":18,"tag":31,"props":287,"children":288},{},[289],{"type":28,"value":290},"在Python中，类表示具有相同属性和方法的对象的集合。在使用类时，需要先定义类，然后再创建类的实例，通过类的实例就可以访问类中的属性和方法了。",{"type":18,"tag":48,"props":292,"children":294},{"id":293},"_21定义类",[295],{"type":28,"value":296},"2.1  定义类",{"type":18,"tag":31,"props":298,"children":299},{},[300,302,309],{"type":28,"value":301},"在Python中，类的定义使用",{"type":18,"tag":303,"props":304,"children":306},"code",{"className":305},[],[307],{"type":28,"value":308},"class",{"type":28,"value":310},"关键字来实现，语法如下：",{"type":18,"tag":312,"props":313,"children":317},"pre",{"code":314,"language":315,"meta":7,"className":316,"style":7},"class ClassName:\n    '''类的帮助信息'''          # 类文档字符串\n    statement                 # 类体\n","python","language-python shiki shiki-themes github-dark",[318],{"type":18,"tag":303,"props":319,"children":320},{"__ignoreMap":7},[321,332,341],{"type":18,"tag":322,"props":323,"children":326},"span",{"class":324,"line":325},"line",1,[327],{"type":18,"tag":322,"props":328,"children":329},{},[330],{"type":28,"value":331},"class ClassName:\n",{"type":18,"tag":322,"props":333,"children":335},{"class":324,"line":334},2,[336],{"type":18,"tag":322,"props":337,"children":338},{},[339],{"type":28,"value":340},"    '''类的帮助信息'''          # 类文档字符串\n",{"type":18,"tag":322,"props":342,"children":344},{"class":324,"line":343},3,[345],{"type":18,"tag":322,"props":346,"children":347},{},[348],{"type":28,"value":349},"    statement                 # 类体\n",{"type":18,"tag":31,"props":351,"children":352},{},[353],{"type":28,"value":354},"参数说明：",{"type":18,"tag":356,"props":357,"children":358},"ul",{},[359,385,410],{"type":18,"tag":360,"props":361,"children":362},"li",{},[363,369,371,376,378,383],{"type":18,"tag":303,"props":364,"children":366},{"className":365},[],[367],{"type":28,"value":368},"ClassName",{"type":28,"value":370},"：用于",{"type":18,"tag":35,"props":372,"children":373},{},[374],{"type":28,"value":375},"指定类名",{"type":28,"value":377},"，一般使用大写字母开头，如果类名中包括两个单词，第二个单词的首字母也大写，这种命名方法也称为“",{"type":18,"tag":35,"props":379,"children":380},{},[381],{"type":28,"value":382},"驼峰式命名法",{"type":28,"value":384},"”，这是惯例。当然，也可根据自己的习惯命名，但是一般推荐按照惯例来命名。",{"type":18,"tag":360,"props":386,"children":387},{},[388,394,395,400,402,408],{"type":18,"tag":303,"props":389,"children":391},{"className":390},[],[392],{"type":28,"value":393},"'''类的帮助信息'''",{"type":28,"value":370},{"type":18,"tag":35,"props":396,"children":397},{},[398],{"type":28,"value":399},"指定类的文档字符串",{"type":28,"value":401},"，定义该字符串后，在创建类的对象时，输入类名和左侧的括号",{"type":18,"tag":303,"props":403,"children":405},{"className":404},[],[406],{"type":28,"value":407},"(",{"type":28,"value":409},"后，将显示该信息。",{"type":18,"tag":360,"props":411,"children":412},{},[413,419,421,426,428,434],{"type":18,"tag":303,"props":414,"children":416},{"className":415},[],[417],{"type":28,"value":418},"statement",{"type":28,"value":420},"：",{"type":18,"tag":35,"props":422,"children":423},{},[424],{"type":28,"value":425},"类体，主要由类变量（或类成员）、方法和属性等定义语句组成",{"type":28,"value":427},"。如果在定义类时，没想好类的具体功能，也可以在类体中直接使用",{"type":18,"tag":303,"props":429,"children":431},{"className":430},[],[432],{"type":28,"value":433},"pass",{"type":28,"value":435},"语句代替。",{"type":18,"tag":31,"props":437,"children":438},{},[439],{"type":28,"value":440},"例如，下面以大雁为例声明一个类，代码如下：",{"type":18,"tag":312,"props":442,"children":444},{"code":443,"language":315,"meta":7,"className":316,"style":7},"class Geese:\n    '''大雁类'''\n    pass\n",[445],{"type":18,"tag":303,"props":446,"children":447},{"__ignoreMap":7},[448,456,464],{"type":18,"tag":322,"props":449,"children":450},{"class":324,"line":325},[451],{"type":18,"tag":322,"props":452,"children":453},{},[454],{"type":28,"value":455},"class Geese:\n",{"type":18,"tag":322,"props":457,"children":458},{"class":324,"line":334},[459],{"type":18,"tag":322,"props":460,"children":461},{},[462],{"type":28,"value":463},"    '''大雁类'''\n",{"type":18,"tag":322,"props":465,"children":466},{"class":324,"line":343},[467],{"type":18,"tag":322,"props":468,"children":469},{},[470],{"type":28,"value":471},"    pass\n",{"type":18,"tag":48,"props":473,"children":475},{"id":474},"_22创建类的实例",[476],{"type":28,"value":477},"2.2  创建类的实例",{"type":18,"tag":31,"props":479,"children":480},{},[481],{"type":28,"value":482},"定义完类后，并不会真正创建一个实例。这有点像一个汽车的设计图。设计图可以告诉你汽车看上去怎么样，但设计图本身不是一个汽车。你不能开走它，它只能用来建造真正的汽车，而且可以使用它制造很多汽车。那么如何创建实例呢？",{"type":18,"tag":31,"props":484,"children":485},{},[486],{"type":28,"value":487},"class语句本身并不创建该类的任何实例。所以在类定义完成以后，可以创建类的实例，即实例化该类的对象。创建类的实例的语法如下：",{"type":18,"tag":312,"props":489,"children":491},{"code":490,"language":315,"meta":7,"className":316,"style":7},"ClassName(parameterlist)\n",[492],{"type":18,"tag":303,"props":493,"children":494},{"__ignoreMap":7},[495],{"type":18,"tag":322,"props":496,"children":497},{"class":324,"line":325},[498],{"type":18,"tag":322,"props":499,"children":500},{},[501],{"type":28,"value":490},{"type":18,"tag":31,"props":503,"children":504},{},[505,507,512,514,519,521,526,528,534,535,540,542,548,550,555,557,563,565,570],{"type":28,"value":506},"其中，",{"type":18,"tag":303,"props":508,"children":510},{"className":509},[],[511],{"type":28,"value":368},{"type":28,"value":513},"是",{"type":18,"tag":35,"props":515,"children":516},{},[517],{"type":28,"value":518},"必选参数",{"type":28,"value":520},"，用于",{"type":18,"tag":35,"props":522,"children":523},{},[524],{"type":28,"value":525},"指定具体的类",{"type":28,"value":527},"；",{"type":18,"tag":303,"props":529,"children":531},{"className":530},[],[532],{"type":28,"value":533},"parameterlist",{"type":28,"value":513},{"type":18,"tag":35,"props":536,"children":537},{},[538],{"type":28,"value":539},"可选参数",{"type":28,"value":541},"，当创建一个类时，没有创建",{"type":18,"tag":303,"props":543,"children":545},{"className":544},[],[546],{"type":28,"value":547},"__init__()",{"type":28,"value":549},"方法，或者",{"type":18,"tag":303,"props":551,"children":553},{"className":552},[],[554],{"type":28,"value":547},{"type":28,"value":556},"方法只有一个",{"type":18,"tag":303,"props":558,"children":560},{"className":559},[],[561],{"type":28,"value":562},"self参数",{"type":28,"value":564},"时，",{"type":18,"tag":303,"props":566,"children":568},{"className":567},[],[569],{"type":28,"value":533},{"type":28,"value":571},"可以省略。",{"type":18,"tag":31,"props":573,"children":574},{},[575],{"type":28,"value":576},"例如，创建Geese类的实例，可以使用下面的代码：",{"type":18,"tag":312,"props":578,"children":580},{"code":579,"language":315,"meta":7,"className":316,"style":7},"wildGoose = Geese()   # 创建大雁类的实例\nprint(wildGoose)\n",[581],{"type":18,"tag":303,"props":582,"children":583},{"__ignoreMap":7},[584,592],{"type":18,"tag":322,"props":585,"children":586},{"class":324,"line":325},[587],{"type":18,"tag":322,"props":588,"children":589},{},[590],{"type":28,"value":591},"wildGoose = Geese()   # 创建大雁类的实例\n",{"type":18,"tag":322,"props":593,"children":594},{"class":324,"line":334},[595],{"type":18,"tag":322,"props":596,"children":597},{},[598],{"type":28,"value":599},"print(wildGoose)\n",{"type":18,"tag":31,"props":601,"children":602},{},[603],{"type":28,"value":604},"执行上面代码后，将显示类似下面的内容：",{"type":18,"tag":312,"props":606,"children":608},{"code":607},"\u003C__main__.Geese object at 0x00000000002F47AC8>\n",[609],{"type":18,"tag":303,"props":610,"children":611},{"__ignoreMap":7},[612],{"type":28,"value":607},{"type":18,"tag":31,"props":614,"children":615},{},[616,618,624],{"type":28,"value":617},"从上面的执行结果中可以看出，",{"type":18,"tag":303,"props":619,"children":621},{"className":620},[],[622],{"type":28,"value":623},"wildGoose",{"type":28,"value":625},"是Geese类的实例。",{"type":18,"tag":23,"props":627,"children":629},{"id":628},"_3-创建__init__方法",[630,632,637],{"type":28,"value":631},"3 创建",{"type":18,"tag":303,"props":633,"children":635},{"className":634},[],[636],{"type":28,"value":547},{"type":28,"value":638},"方法",{"type":18,"tag":31,"props":640,"children":641},{},[642,644,649,651,656,658,664,666,671],{"type":28,"value":643},"在创建类后，可以手动创建一个",{"type":18,"tag":303,"props":645,"children":647},{"className":646},[],[648],{"type":28,"value":547},{"type":28,"value":650},"方法。该方法是一个特殊的方法，类似Java语言中的构造方法。每当创建一个类的新实例时，Python都会自动执行它。",{"type":18,"tag":303,"props":652,"children":654},{"className":653},[],[655],{"type":28,"value":547},{"type":28,"value":657},"方法必须包含一个",{"type":18,"tag":303,"props":659,"children":661},{"className":660},[],[662],{"type":28,"value":663},"self",{"type":28,"value":665},"参数，并且必须是第一个参数。self参数是一个指向实例本身的引用，用于访问类中的属性和方法。在方法调用时会自动传递实际参数self，因此当",{"type":18,"tag":303,"props":667,"children":669},{"className":668},[],[670],{"type":28,"value":547},{"type":28,"value":672},"方法只有一个参数时，在创建类的实例时，就不需要指定实际参数了。",{"type":18,"tag":110,"props":674,"children":675},{},[676],{"type":18,"tag":31,"props":677,"children":678},{},[679,681,686,688,693,695,700],{"type":28,"value":680},"说明：在",{"type":18,"tag":303,"props":682,"children":684},{"className":683},[],[685],{"type":28,"value":547},{"type":28,"value":687},"方法的名称中，",{"type":18,"tag":35,"props":689,"children":690},{},[691],{"type":28,"value":692},"开头和结尾处是两个下划线",{"type":28,"value":694},"（中间没有空格），这是一种约定，旨在",{"type":18,"tag":35,"props":696,"children":697},{},[698],{"type":28,"value":699},"区分Python默认方法和普通方法",{"type":28,"value":86},{"type":18,"tag":31,"props":702,"children":703},{},[704,706,711],{"type":28,"value":705},"例如，下面仍然以大雁为例声明一个类，并且创建",{"type":18,"tag":303,"props":707,"children":709},{"className":708},[],[710],{"type":28,"value":547},{"type":28,"value":712},"方法，代码如下：",{"type":18,"tag":312,"props":714,"children":716},{"code":715,"language":315,"meta":7,"className":316,"style":7},"class Geese:\n    '''大雁类'''\n    def __init__(self):        # 构造方法\n        print(\"我是大雁类！\")\n\nwildGoose = Geese()            # 创建大雁类的实例\n",[717],{"type":18,"tag":303,"props":718,"children":719},{"__ignoreMap":7},[720,727,734,742,751,761],{"type":18,"tag":322,"props":721,"children":722},{"class":324,"line":325},[723],{"type":18,"tag":322,"props":724,"children":725},{},[726],{"type":28,"value":455},{"type":18,"tag":322,"props":728,"children":729},{"class":324,"line":334},[730],{"type":18,"tag":322,"props":731,"children":732},{},[733],{"type":28,"value":463},{"type":18,"tag":322,"props":735,"children":736},{"class":324,"line":343},[737],{"type":18,"tag":322,"props":738,"children":739},{},[740],{"type":28,"value":741},"    def __init__(self):        # 构造方法\n",{"type":18,"tag":322,"props":743,"children":745},{"class":324,"line":744},4,[746],{"type":18,"tag":322,"props":747,"children":748},{},[749],{"type":28,"value":750},"        print(\"我是大雁类！\")\n",{"type":18,"tag":322,"props":752,"children":754},{"class":324,"line":753},5,[755],{"type":18,"tag":322,"props":756,"children":758},{"emptyLinePlaceholder":757},true,[759],{"type":28,"value":760},"\n",{"type":18,"tag":322,"props":762,"children":764},{"class":324,"line":763},6,[765],{"type":18,"tag":322,"props":766,"children":767},{},[768],{"type":28,"value":769},"wildGoose = Geese()            # 创建大雁类的实例\n",{"type":18,"tag":31,"props":771,"children":772},{},[773],{"type":28,"value":774},"运行上面的代码，将输出以下内容：",{"type":18,"tag":312,"props":776,"children":778},{"code":777},"我是大雁类！\n",[779],{"type":18,"tag":303,"props":780,"children":781},{"__ignoreMap":7},[782],{"type":28,"value":777},{"type":18,"tag":31,"props":784,"children":785},{},[786,788,793],{"type":28,"value":787},"从上面的运行结果可以看出，在创建大雁类的实例时，虽然没有为",{"type":18,"tag":303,"props":789,"children":791},{"className":790},[],[792],{"type":28,"value":547},{"type":28,"value":794},"方法指定参数，但是该方法会自动执行。",{"type":18,"tag":31,"props":796,"children":797},{},[798],{"type":28,"value":799},"常见错误：在为类创建__init__()方法时，在开发环境中运行下面代码：",{"type":18,"tag":312,"props":801,"children":803},{"code":802,"language":315,"meta":7,"className":316,"style":7},"class Geese:  \n    '''大雁类'''  \n    def __init__():            # 构造方法  \n       print(\"我是大雁类！\")  \n  \nwildGoose = Geese()            # 创建大雁类的实例\n",[804],{"type":18,"tag":303,"props":805,"children":806},{"__ignoreMap":7},[807,815,823,831,839,847],{"type":18,"tag":322,"props":808,"children":809},{"class":324,"line":325},[810],{"type":18,"tag":322,"props":811,"children":812},{},[813],{"type":28,"value":814},"class Geese:  \n",{"type":18,"tag":322,"props":816,"children":817},{"class":324,"line":334},[818],{"type":18,"tag":322,"props":819,"children":820},{},[821],{"type":28,"value":822},"    '''大雁类'''  \n",{"type":18,"tag":322,"props":824,"children":825},{"class":324,"line":343},[826],{"type":18,"tag":322,"props":827,"children":828},{},[829],{"type":28,"value":830},"    def __init__():            # 构造方法  \n",{"type":18,"tag":322,"props":832,"children":833},{"class":324,"line":744},[834],{"type":18,"tag":322,"props":835,"children":836},{},[837],{"type":28,"value":838},"       print(\"我是大雁类！\")  \n",{"type":18,"tag":322,"props":840,"children":841},{"class":324,"line":753},[842],{"type":18,"tag":322,"props":843,"children":844},{},[845],{"type":28,"value":846},"  \n",{"type":18,"tag":322,"props":848,"children":849},{"class":324,"line":763},[850],{"type":18,"tag":322,"props":851,"children":852},{},[853],{"type":28,"value":769},{"type":18,"tag":31,"props":855,"children":856},{},[857,859,864],{"type":28,"value":858},"将显示如图所示的异常信息。该错误的解决方法是在第3行代码的括号中添加",{"type":18,"tag":303,"props":860,"children":862},{"className":861},[],[863],{"type":28,"value":663},{"type":28,"value":86},{"type":18,"tag":31,"props":866,"children":867},{},[868],{"type":18,"tag":63,"props":869,"children":872},{"alt":870,"src":871},"Pasted image 20250809092822","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250809092822.png",[],{"type":18,"tag":31,"props":874,"children":875},{},[876,878,883,885,890,892,898,900,905,907,913,915,921,923,929],{"type":28,"value":877},"在",{"type":18,"tag":303,"props":879,"children":881},{"className":880},[],[882],{"type":28,"value":547},{"type":28,"value":884},"方法中，除了",{"type":18,"tag":303,"props":886,"children":888},{"className":887},[],[889],{"type":28,"value":663},{"type":28,"value":891},"参数外，还可以自定义一些参数，参数间使用逗号",{"type":18,"tag":303,"props":893,"children":895},{"className":894},[],[896],{"type":28,"value":897},",",{"type":28,"value":899},"进行分隔。例如，下面的代码将在创建",{"type":18,"tag":303,"props":901,"children":903},{"className":902},[],[904],{"type":28,"value":547},{"type":28,"value":906},"方法时，再指定3个参数，分别是",{"type":18,"tag":303,"props":908,"children":910},{"className":909},[],[911],{"type":28,"value":912},"beak",{"type":28,"value":914},"、",{"type":18,"tag":303,"props":916,"children":918},{"className":917},[],[919],{"type":28,"value":920},"wing",{"type":28,"value":922},"和",{"type":18,"tag":303,"props":924,"children":926},{"className":925},[],[927],{"type":28,"value":928},"claw",{"type":28,"value":86},{"type":18,"tag":312,"props":931,"children":933},{"code":932,"language":315,"meta":7,"className":316,"style":7},"class Geese:  \n    '''大雁类'''  \n    def __init__(self,beak,wing,claw):               # 构造方法  \n        print(\"我是大雁类！我有以下特征：\")  \n        print(beak)                                  # 输出喙的特征  \n        print(wing)                                  # 输出翅膀的特征  \n        print(claw)                                  # 输出爪子的特征  \n  \nbeak_1 = \"喙的基部较高，长度和头部的长度几乎相等\"        # 喙的特征  \nwing_1 = \"翅膀长而尖\"                                 # 翅膀的特征  \nclaw_1 = \"爪子是蹼状的\"                               # 爪子的特征  \nwildGoose = Geese(beak_1,wing_1,claw_1)             # 创建大雁类的实例\n",[934],{"type":18,"tag":303,"props":935,"children":936},{"__ignoreMap":7},[937,944,951,959,967,975,983,992,1000,1009,1018,1027],{"type":18,"tag":322,"props":938,"children":939},{"class":324,"line":325},[940],{"type":18,"tag":322,"props":941,"children":942},{},[943],{"type":28,"value":814},{"type":18,"tag":322,"props":945,"children":946},{"class":324,"line":334},[947],{"type":18,"tag":322,"props":948,"children":949},{},[950],{"type":28,"value":822},{"type":18,"tag":322,"props":952,"children":953},{"class":324,"line":343},[954],{"type":18,"tag":322,"props":955,"children":956},{},[957],{"type":28,"value":958},"    def __init__(self,beak,wing,claw):               # 构造方法  \n",{"type":18,"tag":322,"props":960,"children":961},{"class":324,"line":744},[962],{"type":18,"tag":322,"props":963,"children":964},{},[965],{"type":28,"value":966},"        print(\"我是大雁类！我有以下特征：\")  \n",{"type":18,"tag":322,"props":968,"children":969},{"class":324,"line":753},[970],{"type":18,"tag":322,"props":971,"children":972},{},[973],{"type":28,"value":974},"        print(beak)                                  # 输出喙的特征  \n",{"type":18,"tag":322,"props":976,"children":977},{"class":324,"line":763},[978],{"type":18,"tag":322,"props":979,"children":980},{},[981],{"type":28,"value":982},"        print(wing)                                  # 输出翅膀的特征  \n",{"type":18,"tag":322,"props":984,"children":986},{"class":324,"line":985},7,[987],{"type":18,"tag":322,"props":988,"children":989},{},[990],{"type":28,"value":991},"        print(claw)                                  # 输出爪子的特征  \n",{"type":18,"tag":322,"props":993,"children":995},{"class":324,"line":994},8,[996],{"type":18,"tag":322,"props":997,"children":998},{},[999],{"type":28,"value":846},{"type":18,"tag":322,"props":1001,"children":1003},{"class":324,"line":1002},9,[1004],{"type":18,"tag":322,"props":1005,"children":1006},{},[1007],{"type":28,"value":1008},"beak_1 = \"喙的基部较高，长度和头部的长度几乎相等\"        # 喙的特征  \n",{"type":18,"tag":322,"props":1010,"children":1012},{"class":324,"line":1011},10,[1013],{"type":18,"tag":322,"props":1014,"children":1015},{},[1016],{"type":28,"value":1017},"wing_1 = \"翅膀长而尖\"                                 # 翅膀的特征  \n",{"type":18,"tag":322,"props":1019,"children":1021},{"class":324,"line":1020},11,[1022],{"type":18,"tag":322,"props":1023,"children":1024},{},[1025],{"type":28,"value":1026},"claw_1 = \"爪子是蹼状的\"                               # 爪子的特征  \n",{"type":18,"tag":322,"props":1028,"children":1030},{"class":324,"line":1029},12,[1031],{"type":18,"tag":322,"props":1032,"children":1033},{},[1034],{"type":28,"value":1035},"wildGoose = Geese(beak_1,wing_1,claw_1)             # 创建大雁类的实例\n",{"type":18,"tag":31,"props":1037,"children":1038},{},[1039],{"type":18,"tag":63,"props":1040,"children":1043},{"alt":1041,"src":1042},"Pasted image 20250809093346","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250809093346.png",[],{"type":18,"tag":31,"props":1045,"children":1046},{},[1047,1049,1055,1056,1062,1063,1069,1071,1076,1077,1082,1083,1088],{"type":28,"value":1048},"在 Python 类中，",{"type":18,"tag":303,"props":1050,"children":1052},{"className":1051},[],[1053],{"type":28,"value":1054},"__init__",{"type":28,"value":914},{"type":18,"tag":303,"props":1057,"children":1059},{"className":1058},[],[1060],{"type":28,"value":1061},"__enter__",{"type":28,"value":914},{"type":18,"tag":303,"props":1064,"children":1066},{"className":1065},[],[1067],{"type":28,"value":1068},"__exit__",{"type":28,"value":1070}," 是三个核心的 “魔术方法”（Magic Methods），分别负责",{"type":18,"tag":35,"props":1072,"children":1073},{},[1074],{"type":28,"value":1075},"初始化实例",{"type":28,"value":914},{"type":18,"tag":35,"props":1078,"children":1079},{},[1080],{"type":28,"value":1081},"上下文管理的进入",{"type":28,"value":922},{"type":18,"tag":35,"props":1084,"children":1085},{},[1086],{"type":28,"value":1087},"上下文管理的退出",{"type":28,"value":1089},"，三者功能和使用场景完全不同，以下是详细拆解：",{"type":18,"tag":48,"props":1091,"children":1093},{"id":1092},"_1__init__实例的-构造初始化方法",[1094,1096,1101],{"type":28,"value":1095},"1. ",{"type":18,"tag":303,"props":1097,"children":1099},{"className":1098},[],[1100],{"type":28,"value":1054},{"type":28,"value":1102},"：实例的 “构造初始化方法”",{"type":18,"tag":356,"props":1104,"children":1105},{},[1106,1138,1163],{"type":18,"tag":360,"props":1107,"children":1108},{},[1109,1114,1116,1122,1124,1129,1131,1136],{"type":18,"tag":35,"props":1110,"children":1111},{},[1112],{"type":28,"value":1113},"核心作用",{"type":28,"value":1115},"：当通过 ",{"type":18,"tag":303,"props":1117,"children":1119},{"className":1118},[],[1120],{"type":28,"value":1121},"类名()",{"type":28,"value":1123}," 创建实例时，",{"type":18,"tag":303,"props":1125,"children":1127},{"className":1126},[],[1128],{"type":28,"value":1054},{"type":28,"value":1130}," 会",{"type":18,"tag":35,"props":1132,"children":1133},{},[1134],{"type":28,"value":1135},"自动被调用",{"type":28,"value":1137},"，用于初始化实例的属性、分配资源（如变量赋值、打开文件等），是实例创建后的 “第一站”。",{"type":18,"tag":360,"props":1139,"children":1140},{},[1141,1146,1148,1154,1156,1161],{"type":18,"tag":35,"props":1142,"children":1143},{},[1144],{"type":28,"value":1145},"本质",{"type":28,"value":1147},"：它不是 “构造函数”（真正创建实例的是 ",{"type":18,"tag":303,"props":1149,"children":1151},{"className":1150},[],[1152],{"type":28,"value":1153},"__new__",{"type":28,"value":1155},"），而是 “初始化函数”，接收刚创建的实例（",{"type":18,"tag":303,"props":1157,"children":1159},{"className":1158},[],[1160],{"type":28,"value":663},{"type":28,"value":1162},"）并对其初始化。",{"type":18,"tag":360,"props":1164,"children":1165},{},[1166,1171,1173],{"type":18,"tag":35,"props":1167,"children":1168},{},[1169],{"type":28,"value":1170},"关键特征",{"type":28,"value":1172},"：\n",{"type":18,"tag":356,"props":1174,"children":1175},{},[1176,1188,1201],{"type":18,"tag":360,"props":1177,"children":1178},{},[1179,1181,1186],{"type":28,"value":1180},"第一个参数必须是 ",{"type":18,"tag":303,"props":1182,"children":1184},{"className":1183},[],[1185],{"type":28,"value":663},{"type":28,"value":1187},"，代表当前实例本身。",{"type":18,"tag":360,"props":1189,"children":1190},{},[1191,1193,1199],{"type":28,"value":1192},"无返回值（若强行返回非 ",{"type":18,"tag":303,"props":1194,"children":1196},{"className":1195},[],[1197],{"type":28,"value":1198},"None",{"type":28,"value":1200}," 会报错）。",{"type":18,"tag":360,"props":1202,"children":1203},{},[1204,1206,1211],{"type":28,"value":1205},"若未自定义，Python 会提供默认的空 ",{"type":18,"tag":303,"props":1207,"children":1209},{"className":1208},[],[1210],{"type":28,"value":1054},{"type":28,"value":86},{"type":18,"tag":169,"props":1213,"children":1215},{"id":1214},"示例",[1216],{"type":28,"value":1217},"示例：",{"type":18,"tag":312,"props":1219,"children":1221},{"code":1220,"language":315,"meta":7,"className":316,"style":7},"class Student:\n    # 自定义__init__，初始化实例的name和age属性\n    def __init__(self, name, age):\n        self.name = name  # 给实例绑定name属性\n        self.age = age    # 给实例绑定age属性\n\n# 创建实例时，自动调用__init__，传入name和age参数\nstu = Student(\"张三\", 20)\nprint(stu.name)  # 输出：张三（__init__初始化的属性可直接使用）\n",[1222],{"type":18,"tag":303,"props":1223,"children":1224},{"__ignoreMap":7},[1225,1233,1241,1249,1257,1265,1272,1280,1288],{"type":18,"tag":322,"props":1226,"children":1227},{"class":324,"line":325},[1228],{"type":18,"tag":322,"props":1229,"children":1230},{},[1231],{"type":28,"value":1232},"class Student:\n",{"type":18,"tag":322,"props":1234,"children":1235},{"class":324,"line":334},[1236],{"type":18,"tag":322,"props":1237,"children":1238},{},[1239],{"type":28,"value":1240},"    # 自定义__init__，初始化实例的name和age属性\n",{"type":18,"tag":322,"props":1242,"children":1243},{"class":324,"line":343},[1244],{"type":18,"tag":322,"props":1245,"children":1246},{},[1247],{"type":28,"value":1248},"    def __init__(self, name, age):\n",{"type":18,"tag":322,"props":1250,"children":1251},{"class":324,"line":744},[1252],{"type":18,"tag":322,"props":1253,"children":1254},{},[1255],{"type":28,"value":1256},"        self.name = name  # 给实例绑定name属性\n",{"type":18,"tag":322,"props":1258,"children":1259},{"class":324,"line":753},[1260],{"type":18,"tag":322,"props":1261,"children":1262},{},[1263],{"type":28,"value":1264},"        self.age = age    # 给实例绑定age属性\n",{"type":18,"tag":322,"props":1266,"children":1267},{"class":324,"line":763},[1268],{"type":18,"tag":322,"props":1269,"children":1270},{"emptyLinePlaceholder":757},[1271],{"type":28,"value":760},{"type":18,"tag":322,"props":1273,"children":1274},{"class":324,"line":985},[1275],{"type":18,"tag":322,"props":1276,"children":1277},{},[1278],{"type":28,"value":1279},"# 创建实例时，自动调用__init__，传入name和age参数\n",{"type":18,"tag":322,"props":1281,"children":1282},{"class":324,"line":994},[1283],{"type":18,"tag":322,"props":1284,"children":1285},{},[1286],{"type":28,"value":1287},"stu = Student(\"张三\", 20)\n",{"type":18,"tag":322,"props":1289,"children":1290},{"class":324,"line":1002},[1291],{"type":18,"tag":322,"props":1292,"children":1293},{},[1294],{"type":28,"value":1295},"print(stu.name)  # 输出：张三（__init__初始化的属性可直接使用）\n",{"type":18,"tag":48,"props":1297,"children":1299},{"id":1298},"_2__enter__上下文管理器的-进入方法",[1300,1302,1307],{"type":28,"value":1301},"2. ",{"type":18,"tag":303,"props":1303,"children":1305},{"className":1304},[],[1306],{"type":28,"value":1061},{"type":28,"value":1308},"：上下文管理器的 “进入方法”",{"type":18,"tag":356,"props":1310,"children":1311},{},[1312,1348,1379],{"type":18,"tag":360,"props":1313,"children":1314},{},[1315,1319,1321,1327,1329,1334,1336,1341,1342,1346],{"type":18,"tag":35,"props":1316,"children":1317},{},[1318],{"type":28,"value":1113},{"type":28,"value":1320},"：当实例被用于 ",{"type":18,"tag":303,"props":1322,"children":1324},{"className":1323},[],[1325],{"type":28,"value":1326},"with",{"type":28,"value":1328}," 语句（上下文管理器）时，进入 ",{"type":18,"tag":303,"props":1330,"children":1332},{"className":1331},[],[1333],{"type":28,"value":1326},{"type":28,"value":1335}," 代码块前，",{"type":18,"tag":303,"props":1337,"children":1339},{"className":1338},[],[1340],{"type":28,"value":1061},{"type":28,"value":1130},{"type":18,"tag":35,"props":1343,"children":1344},{},[1345],{"type":28,"value":1135},{"type":28,"value":1347},"，主要用于 “准备资源”（如创建数据库连接、打开文件、获取锁等）。",{"type":18,"tag":360,"props":1349,"children":1350},{},[1351,1356,1358,1363,1365,1370,1372,1377],{"type":18,"tag":35,"props":1352,"children":1353},{},[1354],{"type":28,"value":1355},"使用前提",{"type":28,"value":1357},"：类必须同时实现 ",{"type":18,"tag":303,"props":1359,"children":1361},{"className":1360},[],[1362],{"type":28,"value":1061},{"type":28,"value":1364}," 和 ",{"type":18,"tag":303,"props":1366,"children":1368},{"className":1367},[],[1369],{"type":28,"value":1068},{"type":28,"value":1371},"，才是合格的 “上下文管理器”，才能用在 ",{"type":18,"tag":303,"props":1373,"children":1375},{"className":1374},[],[1376],{"type":28,"value":1326},{"type":28,"value":1378}," 中。",{"type":18,"tag":360,"props":1380,"children":1381},{},[1382,1386,1387],{"type":18,"tag":35,"props":1383,"children":1384},{},[1385],{"type":28,"value":1170},{"type":28,"value":1172},{"type":18,"tag":356,"props":1388,"children":1389},{},[1390,1402],{"type":18,"tag":360,"props":1391,"children":1392},{},[1393,1395,1400],{"type":28,"value":1394},"第一个参数是 ",{"type":18,"tag":303,"props":1396,"children":1398},{"className":1397},[],[1399],{"type":28,"value":663},{"type":28,"value":1401},"，代表当前实例。",{"type":18,"tag":360,"props":1403,"children":1404},{},[1405,1410,1412,1417,1419,1424],{"type":18,"tag":35,"props":1406,"children":1407},{},[1408],{"type":28,"value":1409},"必须有返回值",{"type":28,"value":1411},"：通常返回 ",{"type":18,"tag":303,"props":1413,"children":1415},{"className":1414},[],[1416],{"type":28,"value":663},{"type":28,"value":1418},"（让 ",{"type":18,"tag":303,"props":1420,"children":1422},{"className":1421},[],[1423],{"type":28,"value":1326},{"type":28,"value":1425}," 语句的变量直接指向实例，方便后续操作）。",{"type":18,"tag":169,"props":1427,"children":1429},{"id":1428},"示例结合数据库场景",[1430],{"type":28,"value":1431},"示例（结合数据库场景）：",{"type":18,"tag":312,"props":1433,"children":1435},{"code":1434,"language":315,"meta":7,"className":316,"style":7},"class DBConn:\n    def __init__(self, db_config):\n        self.db_config = db_config\n        self.conn = None  # 初始化时暂不创建连接\n\n    # 进入with块时，创建数据库连接（准备资源）\n    def __enter__(self):\n        self.conn = pymysql.connect(**self.db_config)  # 准备连接资源\n        return self  # 返回实例，让with变量指向它\n\n# 使用with语句：进入时自动调用__enter__\nwith DBConn(config) as db:\n    # db就是__enter__返回的实例，可直接使用其conn属性\n    print(db.conn)  # 输出：数据库连接对象\n",[1436],{"type":18,"tag":303,"props":1437,"children":1438},{"__ignoreMap":7},[1439,1447,1455,1463,1471,1478,1486,1494,1502,1510,1517,1525,1533,1542],{"type":18,"tag":322,"props":1440,"children":1441},{"class":324,"line":325},[1442],{"type":18,"tag":322,"props":1443,"children":1444},{},[1445],{"type":28,"value":1446},"class DBConn:\n",{"type":18,"tag":322,"props":1448,"children":1449},{"class":324,"line":334},[1450],{"type":18,"tag":322,"props":1451,"children":1452},{},[1453],{"type":28,"value":1454},"    def __init__(self, db_config):\n",{"type":18,"tag":322,"props":1456,"children":1457},{"class":324,"line":343},[1458],{"type":18,"tag":322,"props":1459,"children":1460},{},[1461],{"type":28,"value":1462},"        self.db_config = db_config\n",{"type":18,"tag":322,"props":1464,"children":1465},{"class":324,"line":744},[1466],{"type":18,"tag":322,"props":1467,"children":1468},{},[1469],{"type":28,"value":1470},"        self.conn = None  # 初始化时暂不创建连接\n",{"type":18,"tag":322,"props":1472,"children":1473},{"class":324,"line":753},[1474],{"type":18,"tag":322,"props":1475,"children":1476},{"emptyLinePlaceholder":757},[1477],{"type":28,"value":760},{"type":18,"tag":322,"props":1479,"children":1480},{"class":324,"line":763},[1481],{"type":18,"tag":322,"props":1482,"children":1483},{},[1484],{"type":28,"value":1485},"    # 进入with块时，创建数据库连接（准备资源）\n",{"type":18,"tag":322,"props":1487,"children":1488},{"class":324,"line":985},[1489],{"type":18,"tag":322,"props":1490,"children":1491},{},[1492],{"type":28,"value":1493},"    def __enter__(self):\n",{"type":18,"tag":322,"props":1495,"children":1496},{"class":324,"line":994},[1497],{"type":18,"tag":322,"props":1498,"children":1499},{},[1500],{"type":28,"value":1501},"        self.conn = pymysql.connect(**self.db_config)  # 准备连接资源\n",{"type":18,"tag":322,"props":1503,"children":1504},{"class":324,"line":1002},[1505],{"type":18,"tag":322,"props":1506,"children":1507},{},[1508],{"type":28,"value":1509},"        return self  # 返回实例，让with变量指向它\n",{"type":18,"tag":322,"props":1511,"children":1512},{"class":324,"line":1011},[1513],{"type":18,"tag":322,"props":1514,"children":1515},{"emptyLinePlaceholder":757},[1516],{"type":28,"value":760},{"type":18,"tag":322,"props":1518,"children":1519},{"class":324,"line":1020},[1520],{"type":18,"tag":322,"props":1521,"children":1522},{},[1523],{"type":28,"value":1524},"# 使用with语句：进入时自动调用__enter__\n",{"type":18,"tag":322,"props":1526,"children":1527},{"class":324,"line":1029},[1528],{"type":18,"tag":322,"props":1529,"children":1530},{},[1531],{"type":28,"value":1532},"with DBConn(config) as db:\n",{"type":18,"tag":322,"props":1534,"children":1536},{"class":324,"line":1535},13,[1537],{"type":18,"tag":322,"props":1538,"children":1539},{},[1540],{"type":28,"value":1541},"    # db就是__enter__返回的实例，可直接使用其conn属性\n",{"type":18,"tag":322,"props":1543,"children":1545},{"class":324,"line":1544},14,[1546],{"type":18,"tag":322,"props":1547,"children":1548},{},[1549],{"type":28,"value":1550},"    print(db.conn)  # 输出：数据库连接对象\n",{"type":18,"tag":48,"props":1552,"children":1554},{"id":1553},"_3__exit__上下文管理器的-退出方法",[1555,1557,1562],{"type":28,"value":1556},"3. ",{"type":18,"tag":303,"props":1558,"children":1560},{"className":1559},[],[1561],{"type":28,"value":1068},{"type":28,"value":1563},"：上下文管理器的 “退出方法”",{"type":18,"tag":356,"props":1565,"children":1566},{},[1567,1595],{"type":18,"tag":360,"props":1568,"children":1569},{},[1570,1574,1576,1581,1583,1588,1589,1593],{"type":18,"tag":35,"props":1571,"children":1572},{},[1573],{"type":28,"value":1113},{"type":28,"value":1575},"：当 ",{"type":18,"tag":303,"props":1577,"children":1579},{"className":1578},[],[1580],{"type":28,"value":1326},{"type":28,"value":1582}," 代码块执行结束（无论正常结束还是抛出异常），",{"type":18,"tag":303,"props":1584,"children":1586},{"className":1585},[],[1587],{"type":28,"value":1068},{"type":28,"value":1130},{"type":18,"tag":35,"props":1590,"children":1591},{},[1592],{"type":28,"value":1135},{"type":28,"value":1594},"，主要用于 “清理资源”（如关闭数据库连接、关闭文件、释放锁等），是上下文管理的 “最后一站”。",{"type":18,"tag":360,"props":1596,"children":1597},{},[1598,1602,1603],{"type":18,"tag":35,"props":1599,"children":1600},{},[1601],{"type":28,"value":1170},{"type":28,"value":1172},{"type":18,"tag":356,"props":1604,"children":1605},{},[1606,1662],{"type":18,"tag":360,"props":1607,"children":1608},{},[1609,1611,1616,1618,1624,1626,1631,1633,1639,1641,1646,1647,1653,1655,1660],{"type":28,"value":1610},"有 4 个参数：",{"type":18,"tag":303,"props":1612,"children":1614},{"className":1613},[],[1615],{"type":28,"value":663},{"type":28,"value":1617},"（实例）、",{"type":18,"tag":303,"props":1619,"children":1621},{"className":1620},[],[1622],{"type":28,"value":1623},"exc_type",{"type":28,"value":1625},"（异常类型，无异常则为 ",{"type":18,"tag":303,"props":1627,"children":1629},{"className":1628},[],[1630],{"type":28,"value":1198},{"type":28,"value":1632},"）、",{"type":18,"tag":303,"props":1634,"children":1636},{"className":1635},[],[1637],{"type":28,"value":1638},"exc_val",{"type":28,"value":1640},"（异常实例，无异常则为 ",{"type":18,"tag":303,"props":1642,"children":1644},{"className":1643},[],[1645],{"type":28,"value":1198},{"type":28,"value":1632},{"type":18,"tag":303,"props":1648,"children":1650},{"className":1649},[],[1651],{"type":28,"value":1652},"exc_tb",{"type":28,"value":1654},"（异常追踪栈，无异常则为 ",{"type":18,"tag":303,"props":1656,"children":1658},{"className":1657},[],[1659],{"type":28,"value":1198},{"type":28,"value":1661},"）。",{"type":18,"tag":360,"props":1663,"children":1664},{},[1665,1667,1673,1675,1681,1683,1688,1690,1695],{"type":28,"value":1666},"返回值为布尔值（",{"type":18,"tag":303,"props":1668,"children":1670},{"className":1669},[],[1671],{"type":28,"value":1672},"True",{"type":28,"value":1674},"\u002F",{"type":18,"tag":303,"props":1676,"children":1678},{"className":1677},[],[1679],{"type":28,"value":1680},"False",{"type":28,"value":1682},"）：若返回 ",{"type":18,"tag":303,"props":1684,"children":1686},{"className":1685},[],[1687],{"type":28,"value":1672},{"type":28,"value":1689},"，表示 “已处理异常”，外部不会再捕获该异常；若返回 ",{"type":18,"tag":303,"props":1691,"children":1693},{"className":1692},[],[1694],{"type":28,"value":1680},{"type":28,"value":1696}," 或无返回值，异常会向外传播。",{"type":18,"tag":169,"props":1698,"children":1700},{"id":1699},"示例完善数据库连接的关闭逻辑",[1701],{"type":28,"value":1702},"示例（完善数据库连接的关闭逻辑）：",{"type":18,"tag":312,"props":1704,"children":1706},{"code":1705,"language":315,"meta":7,"className":316,"style":7},"class DBConn:\n    def __init__(self, db_config):\n        self.db_config = db_config\n        self.conn = None\n        self.cursor = None\n\n    def __enter__(self):\n        self.conn = pymysql.connect(**self.db_config)\n        self.cursor = self.conn.cursor()\n        return self\n\n    # 退出with块时，自动清理资源\n    def __exit__(self, exc_type, exc_val, exc_tb):\n        # 1. 关闭cursor（先关轻量资源）\n        if self.cursor:\n            self.cursor.close()\n        # 2. 关闭conn（再关重量级资源）\n        if self.conn:\n            self.conn.close()\n        # 3. 处理异常（可选）\n        if exc_type:\n            print(f\"发生异常：{exc_val}\")\n            # return True  # 若加这行，外部不会再捕获该异常\n\n# 使用with：结束后自动调用__exit__，关闭连接\nwith DBConn(config) as db:\n    db.cursor.execute(\"SELECT 1\")\n# 此时conn和cursor已被__exit__自动关闭\n",[1707],{"type":18,"tag":303,"props":1708,"children":1709},{"__ignoreMap":7},[1710,1717,1724,1731,1739,1747,1754,1761,1769,1777,1785,1792,1800,1808,1816,1825,1834,1843,1852,1861,1870,1879,1888,1897,1905,1914,1922,1931],{"type":18,"tag":322,"props":1711,"children":1712},{"class":324,"line":325},[1713],{"type":18,"tag":322,"props":1714,"children":1715},{},[1716],{"type":28,"value":1446},{"type":18,"tag":322,"props":1718,"children":1719},{"class":324,"line":334},[1720],{"type":18,"tag":322,"props":1721,"children":1722},{},[1723],{"type":28,"value":1454},{"type":18,"tag":322,"props":1725,"children":1726},{"class":324,"line":343},[1727],{"type":18,"tag":322,"props":1728,"children":1729},{},[1730],{"type":28,"value":1462},{"type":18,"tag":322,"props":1732,"children":1733},{"class":324,"line":744},[1734],{"type":18,"tag":322,"props":1735,"children":1736},{},[1737],{"type":28,"value":1738},"        self.conn = None\n",{"type":18,"tag":322,"props":1740,"children":1741},{"class":324,"line":753},[1742],{"type":18,"tag":322,"props":1743,"children":1744},{},[1745],{"type":28,"value":1746},"        self.cursor = None\n",{"type":18,"tag":322,"props":1748,"children":1749},{"class":324,"line":763},[1750],{"type":18,"tag":322,"props":1751,"children":1752},{"emptyLinePlaceholder":757},[1753],{"type":28,"value":760},{"type":18,"tag":322,"props":1755,"children":1756},{"class":324,"line":985},[1757],{"type":18,"tag":322,"props":1758,"children":1759},{},[1760],{"type":28,"value":1493},{"type":18,"tag":322,"props":1762,"children":1763},{"class":324,"line":994},[1764],{"type":18,"tag":322,"props":1765,"children":1766},{},[1767],{"type":28,"value":1768},"        self.conn = pymysql.connect(**self.db_config)\n",{"type":18,"tag":322,"props":1770,"children":1771},{"class":324,"line":1002},[1772],{"type":18,"tag":322,"props":1773,"children":1774},{},[1775],{"type":28,"value":1776},"        self.cursor = self.conn.cursor()\n",{"type":18,"tag":322,"props":1778,"children":1779},{"class":324,"line":1011},[1780],{"type":18,"tag":322,"props":1781,"children":1782},{},[1783],{"type":28,"value":1784},"        return self\n",{"type":18,"tag":322,"props":1786,"children":1787},{"class":324,"line":1020},[1788],{"type":18,"tag":322,"props":1789,"children":1790},{"emptyLinePlaceholder":757},[1791],{"type":28,"value":760},{"type":18,"tag":322,"props":1793,"children":1794},{"class":324,"line":1029},[1795],{"type":18,"tag":322,"props":1796,"children":1797},{},[1798],{"type":28,"value":1799},"    # 退出with块时，自动清理资源\n",{"type":18,"tag":322,"props":1801,"children":1802},{"class":324,"line":1535},[1803],{"type":18,"tag":322,"props":1804,"children":1805},{},[1806],{"type":28,"value":1807},"    def __exit__(self, exc_type, exc_val, exc_tb):\n",{"type":18,"tag":322,"props":1809,"children":1810},{"class":324,"line":1544},[1811],{"type":18,"tag":322,"props":1812,"children":1813},{},[1814],{"type":28,"value":1815},"        # 1. 关闭cursor（先关轻量资源）\n",{"type":18,"tag":322,"props":1817,"children":1819},{"class":324,"line":1818},15,[1820],{"type":18,"tag":322,"props":1821,"children":1822},{},[1823],{"type":28,"value":1824},"        if self.cursor:\n",{"type":18,"tag":322,"props":1826,"children":1828},{"class":324,"line":1827},16,[1829],{"type":18,"tag":322,"props":1830,"children":1831},{},[1832],{"type":28,"value":1833},"            self.cursor.close()\n",{"type":18,"tag":322,"props":1835,"children":1837},{"class":324,"line":1836},17,[1838],{"type":18,"tag":322,"props":1839,"children":1840},{},[1841],{"type":28,"value":1842},"        # 2. 关闭conn（再关重量级资源）\n",{"type":18,"tag":322,"props":1844,"children":1846},{"class":324,"line":1845},18,[1847],{"type":18,"tag":322,"props":1848,"children":1849},{},[1850],{"type":28,"value":1851},"        if self.conn:\n",{"type":18,"tag":322,"props":1853,"children":1855},{"class":324,"line":1854},19,[1856],{"type":18,"tag":322,"props":1857,"children":1858},{},[1859],{"type":28,"value":1860},"            self.conn.close()\n",{"type":18,"tag":322,"props":1862,"children":1864},{"class":324,"line":1863},20,[1865],{"type":18,"tag":322,"props":1866,"children":1867},{},[1868],{"type":28,"value":1869},"        # 3. 处理异常（可选）\n",{"type":18,"tag":322,"props":1871,"children":1873},{"class":324,"line":1872},21,[1874],{"type":18,"tag":322,"props":1875,"children":1876},{},[1877],{"type":28,"value":1878},"        if exc_type:\n",{"type":18,"tag":322,"props":1880,"children":1882},{"class":324,"line":1881},22,[1883],{"type":18,"tag":322,"props":1884,"children":1885},{},[1886],{"type":28,"value":1887},"            print(f\"发生异常：{exc_val}\")\n",{"type":18,"tag":322,"props":1889,"children":1891},{"class":324,"line":1890},23,[1892],{"type":18,"tag":322,"props":1893,"children":1894},{},[1895],{"type":28,"value":1896},"            # return True  # 若加这行，外部不会再捕获该异常\n",{"type":18,"tag":322,"props":1898,"children":1900},{"class":324,"line":1899},24,[1901],{"type":18,"tag":322,"props":1902,"children":1903},{"emptyLinePlaceholder":757},[1904],{"type":28,"value":760},{"type":18,"tag":322,"props":1906,"children":1908},{"class":324,"line":1907},25,[1909],{"type":18,"tag":322,"props":1910,"children":1911},{},[1912],{"type":28,"value":1913},"# 使用with：结束后自动调用__exit__，关闭连接\n",{"type":18,"tag":322,"props":1915,"children":1917},{"class":324,"line":1916},26,[1918],{"type":18,"tag":322,"props":1919,"children":1920},{},[1921],{"type":28,"value":1532},{"type":18,"tag":322,"props":1923,"children":1925},{"class":324,"line":1924},27,[1926],{"type":18,"tag":322,"props":1927,"children":1928},{},[1929],{"type":28,"value":1930},"    db.cursor.execute(\"SELECT 1\")\n",{"type":18,"tag":322,"props":1932,"children":1934},{"class":324,"line":1933},28,[1935],{"type":18,"tag":322,"props":1936,"children":1937},{},[1938],{"type":28,"value":1939},"# 此时conn和cursor已被__exit__自动关闭\n",{"type":18,"tag":48,"props":1941,"children":1943},{"id":1942},"三者核心区别与关联",[1944],{"type":28,"value":1942},{"type":18,"tag":1946,"props":1947,"children":1948},"table",{},[1949,1977],{"type":18,"tag":1950,"props":1951,"children":1952},"thead",{},[1953],{"type":18,"tag":1954,"props":1955,"children":1956},"tr",{},[1957,1962,1967,1972],{"type":18,"tag":1958,"props":1959,"children":1960},"th",{},[1961],{"type":28,"value":638},{"type":18,"tag":1958,"props":1963,"children":1964},{},[1965],{"type":28,"value":1966},"触发时机",{"type":18,"tag":1958,"props":1968,"children":1969},{},[1970],{"type":28,"value":1971},"核心功能",{"type":18,"tag":1958,"props":1973,"children":1974},{},[1975],{"type":28,"value":1976},"依赖关系",{"type":18,"tag":1978,"props":1979,"children":1980},"tbody",{},[1981,2013,2053],{"type":18,"tag":1954,"props":1982,"children":1983},{},[1984,1993,2003,2008],{"type":18,"tag":1985,"props":1986,"children":1987},"td",{},[1988],{"type":18,"tag":303,"props":1989,"children":1991},{"className":1990},[],[1992],{"type":28,"value":1054},{"type":18,"tag":1985,"props":1994,"children":1995},{},[1996,2001],{"type":18,"tag":303,"props":1997,"children":1999},{"className":1998},[],[2000],{"type":28,"value":1121},{"type":28,"value":2002}," 创建实例时",{"type":18,"tag":1985,"props":2004,"children":2005},{},[2006],{"type":28,"value":2007},"初始化实例属性",{"type":18,"tag":1985,"props":2009,"children":2010},{},[2011],{"type":28,"value":2012},"无（独立存在）",{"type":18,"tag":1954,"props":2014,"children":2015},{},[2016,2024,2036,2041],{"type":18,"tag":1985,"props":2017,"children":2018},{},[2019],{"type":18,"tag":303,"props":2020,"children":2022},{"className":2021},[],[2023],{"type":28,"value":1061},{"type":18,"tag":1985,"props":2025,"children":2026},{},[2027,2029,2034],{"type":28,"value":2028},"进入 ",{"type":18,"tag":303,"props":2030,"children":2032},{"className":2031},[],[2033],{"type":28,"value":1326},{"type":28,"value":2035}," 代码块时",{"type":18,"tag":1985,"props":2037,"children":2038},{},[2039],{"type":28,"value":2040},"准备上下文资源（如建连接）",{"type":18,"tag":1985,"props":2042,"children":2043},{},[2044,2046,2051],{"type":28,"value":2045},"必须与 ",{"type":18,"tag":303,"props":2047,"children":2049},{"className":2048},[],[2050],{"type":28,"value":1068},{"type":28,"value":2052}," 成对出现",{"type":18,"tag":1954,"props":2054,"children":2055},{},[2056,2064,2075,2080],{"type":18,"tag":1985,"props":2057,"children":2058},{},[2059],{"type":18,"tag":303,"props":2060,"children":2062},{"className":2061},[],[2063],{"type":28,"value":1068},{"type":18,"tag":1985,"props":2065,"children":2066},{},[2067,2069,2074],{"type":28,"value":2068},"退出 ",{"type":18,"tag":303,"props":2070,"children":2072},{"className":2071},[],[2073],{"type":28,"value":1326},{"type":28,"value":2035},{"type":18,"tag":1985,"props":2076,"children":2077},{},[2078],{"type":28,"value":2079},"清理上下文资源（如关连接）",{"type":18,"tag":1985,"props":2081,"children":2082},{},[2083,2084,2089],{"type":28,"value":2045},{"type":18,"tag":303,"props":2085,"children":2087},{"className":2086},[],[2088],{"type":28,"value":1061},{"type":28,"value":2052},{"type":18,"tag":31,"props":2091,"children":2092},{},[2093,2098,2100,2105,2107,2112,2114,2119,2121,2126],{"type":18,"tag":35,"props":2094,"children":2095},{},[2096],{"type":28,"value":2097},"关联场景",{"type":28,"value":2099},"：在数据库、文件等需要 “用完即关” 的场景中，通常会同时实现三者 ——",{"type":18,"tag":303,"props":2101,"children":2103},{"className":2102},[],[2104],{"type":28,"value":1054},{"type":28,"value":2106}," 存配置，",{"type":18,"tag":303,"props":2108,"children":2110},{"className":2109},[],[2111],{"type":28,"value":1061},{"type":28,"value":2113}," 建连接，",{"type":18,"tag":303,"props":2115,"children":2117},{"className":2116},[],[2118],{"type":28,"value":1068},{"type":28,"value":2120}," 关连接，通过 ",{"type":18,"tag":303,"props":2122,"children":2124},{"className":2123},[],[2125],{"type":28,"value":1326},{"type":28,"value":2127}," 语句实现 “自动资源管理”。",{"type":18,"tag":23,"props":2129,"children":2131},{"id":2130},"_4-创建类的成员并访问",[2132],{"type":28,"value":2133},"4 创建类的成员并访问",{"type":18,"tag":31,"props":2135,"children":2136},{},[2137],{"type":28,"value":2138},"类的成员主要由实例方法和数据成员组成。在类中创建了类的成员后，可以通过类的实例进行访问。",{"type":18,"tag":48,"props":2140,"children":2142},{"id":2141},"_41-创建实例方法并访问",[2143],{"type":28,"value":2144},"4.1 创建实例方法并访问",{"type":18,"tag":31,"props":2146,"children":2147},{},[2148,2153],{"type":18,"tag":35,"props":2149,"children":2150},{},[2151],{"type":28,"value":2152},"所谓实例方法是指在类中定义的函数。该函数是一种在类的实例上操作的函数",{"type":28,"value":2154},"。同__init__()方法一样，实例方法的第一个参数必须是self，并且必须包含一个self参数。创建实例方法的语法格式如下：",{"type":18,"tag":312,"props":2156,"children":2158},{"code":2157,"language":315,"meta":7,"className":316,"style":7},"def functionName(self,parameterlist):  \n    block\n",[2159],{"type":18,"tag":303,"props":2160,"children":2161},{"__ignoreMap":7},[2162,2170],{"type":18,"tag":322,"props":2163,"children":2164},{"class":324,"line":325},[2165],{"type":18,"tag":322,"props":2166,"children":2167},{},[2168],{"type":28,"value":2169},"def functionName(self,parameterlist):  \n",{"type":18,"tag":322,"props":2171,"children":2172},{"class":324,"line":334},[2173],{"type":18,"tag":322,"props":2174,"children":2175},{},[2176],{"type":28,"value":2177},"    block\n",{"type":18,"tag":31,"props":2179,"children":2180},{},[2181],{"type":28,"value":354},{"type":18,"tag":356,"props":2183,"children":2184},{},[2185,2196,2206,2216],{"type":18,"tag":360,"props":2186,"children":2187},{},[2188,2194],{"type":18,"tag":303,"props":2189,"children":2191},{"className":2190},[],[2192],{"type":28,"value":2193},"functionName",{"type":28,"value":2195},"：用于指定方法名，一般使用小写字母开头。",{"type":18,"tag":360,"props":2197,"children":2198},{},[2199,2204],{"type":18,"tag":303,"props":2200,"children":2202},{"className":2201},[],[2203],{"type":28,"value":663},{"type":28,"value":2205},"：必要参数，表示类的实例，其名称可以是self以外的单词，使用self只是一个惯例而已。",{"type":18,"tag":360,"props":2207,"children":2208},{},[2209,2214],{"type":18,"tag":303,"props":2210,"children":2212},{"className":2211},[],[2213],{"type":28,"value":533},{"type":28,"value":2215},"：用于指定除self参数以外的参数，各参数间使用逗号“,”进行分隔。",{"type":18,"tag":360,"props":2217,"children":2218},{},[2219,2225],{"type":18,"tag":303,"props":2220,"children":2222},{"className":2221},[],[2223],{"type":28,"value":2224},"block",{"type":28,"value":2226},"：方法体，实现的具体功能。",{"type":18,"tag":110,"props":2228,"children":2229},{},[2230],{"type":18,"tag":31,"props":2231,"children":2232},{},[2233],{"type":28,"value":2234},"说明：实例方法和Python中的函数的主要区别就是，函数实现的是某个独立的功能，而实例方法是实现类中的一个行为，是类的一部分。",{"type":18,"tag":31,"props":2236,"children":2237},{},[2238],{"type":28,"value":2239},"实例方法创建完成后，可以通过类的实例名称和点（.）操作符进行访问，语法格式如下：",{"type":18,"tag":312,"props":2241,"children":2243},{"code":2242,"language":315,"meta":7,"className":316,"style":7},"instanceName.functionName(parametervalue)\n",[2244],{"type":18,"tag":303,"props":2245,"children":2246},{"__ignoreMap":7},[2247],{"type":18,"tag":322,"props":2248,"children":2249},{"class":324,"line":325},[2250],{"type":18,"tag":322,"props":2251,"children":2252},{},[2253],{"type":28,"value":2242},{"type":18,"tag":31,"props":2255,"children":2256},{},[2257],{"type":28,"value":354},{"type":18,"tag":356,"props":2259,"children":2260},{},[2261,2272,2282],{"type":18,"tag":360,"props":2262,"children":2263},{},[2264,2270],{"type":18,"tag":303,"props":2265,"children":2267},{"className":2266},[],[2268],{"type":28,"value":2269},"instanceName",{"type":28,"value":2271},"：为类的实例名称。",{"type":18,"tag":360,"props":2273,"children":2274},{},[2275,2280],{"type":18,"tag":303,"props":2276,"children":2278},{"className":2277},[],[2279],{"type":28,"value":2193},{"type":28,"value":2281},"：为要调用的方法名称。",{"type":18,"tag":360,"props":2283,"children":2284},{},[2285,2291,2293,2298],{"type":18,"tag":303,"props":2286,"children":2288},{"className":2287},[],[2289],{"type":28,"value":2290},"parametervalue",{"type":28,"value":2292},"：表示为方法指定对应的实际参数，其值的个数与创建实例方法中",{"type":18,"tag":303,"props":2294,"children":2296},{"className":2295},[],[2297],{"type":28,"value":533},{"type":28,"value":2299},"的个数相同。",{"type":18,"tag":31,"props":2301,"children":2302},{},[2303],{"type":28,"value":2304},"下面通过一个具体的实例演示创建实例方法并访问。",{"type":18,"tag":31,"props":2306,"children":2307},{},[2308],{"type":18,"tag":35,"props":2309,"children":2310},{},[2311],{"type":28,"value":2312},"实例  创建大雁类并定义飞行方法",{"type":18,"tag":31,"props":2314,"children":2315},{},[2316,2318,2324,2326,2332,2334,2340,2342,2347,2349,2354],{"type":28,"value":2317},"在IDLE中创建一个名称为",{"type":18,"tag":303,"props":2319,"children":2321},{"className":2320},[],[2322],{"type":28,"value":2323},"geese.py",{"type":28,"value":2325},"的文件，然后在该文件中定义一个大雁类",{"type":18,"tag":303,"props":2327,"children":2329},{"className":2328},[],[2330],{"type":28,"value":2331},"Geese",{"type":28,"value":2333},"，并定义一个构造方法，然后再定义一个实例方法",{"type":18,"tag":303,"props":2335,"children":2337},{"className":2336},[],[2338],{"type":28,"value":2339},"fly()",{"type":28,"value":2341},"，该方法有两个参数，一个是",{"type":18,"tag":303,"props":2343,"children":2345},{"className":2344},[],[2346],{"type":28,"value":663},{"type":28,"value":2348},"，另一个用于指定飞行状态，最后再创建大雁类的实例，并调用实例方法",{"type":18,"tag":303,"props":2350,"children":2352},{"className":2351},[],[2353],{"type":28,"value":2339},{"type":28,"value":2355},"，代码如下：",{"type":18,"tag":312,"props":2357,"children":2359},{"code":2358,"language":315,"meta":7,"className":316,"style":7},"class Geese:                                                    # 创建大雁类  \n    '''大雁类'''  \n    def __init__(self, beak, wing, claw):                       # 构造方法  \n        print(\"我是大雁类！我有以下特征：\")  \n        print(beak)                                             # 输出喙的特征  \n        print(wing)                                             # 输出翅膀的特征  \n        print(claw)                                             # 输出爪子的特征  \n  \n    def fly(self, state):                                       # 定义飞行方法  \n        print(state)  \n  \n'''**************调用方法*********************'''  \n  \nbeak_1 = \"喙的基部较高，长度和头部的长度几乎相等\"                   # 喙的特征  \nwing_1 = \"翅膀长而尖\"                                            # 翅膀的特征  \nclaw_1 = \"爪子是蹼状的\"                                          # 爪子的特征  \nwildGoose = Geese(beak_1, wing_1, claw_1)                       # 创建大雁类的实例  \nwildGoose.fly(\"我飞行的时候，一会儿排成个人字，一会排成个一字\")      # 调用实例方法\n",[2360],{"type":18,"tag":303,"props":2361,"children":2362},{"__ignoreMap":7},[2363,2371,2378,2386,2393,2401,2409,2417,2424,2432,2440,2447,2455,2462,2470,2478,2486,2494],{"type":18,"tag":322,"props":2364,"children":2365},{"class":324,"line":325},[2366],{"type":18,"tag":322,"props":2367,"children":2368},{},[2369],{"type":28,"value":2370},"class Geese:                                                    # 创建大雁类  \n",{"type":18,"tag":322,"props":2372,"children":2373},{"class":324,"line":334},[2374],{"type":18,"tag":322,"props":2375,"children":2376},{},[2377],{"type":28,"value":822},{"type":18,"tag":322,"props":2379,"children":2380},{"class":324,"line":343},[2381],{"type":18,"tag":322,"props":2382,"children":2383},{},[2384],{"type":28,"value":2385},"    def __init__(self, beak, wing, claw):                       # 构造方法  \n",{"type":18,"tag":322,"props":2387,"children":2388},{"class":324,"line":744},[2389],{"type":18,"tag":322,"props":2390,"children":2391},{},[2392],{"type":28,"value":966},{"type":18,"tag":322,"props":2394,"children":2395},{"class":324,"line":753},[2396],{"type":18,"tag":322,"props":2397,"children":2398},{},[2399],{"type":28,"value":2400},"        print(beak)                                             # 输出喙的特征  \n",{"type":18,"tag":322,"props":2402,"children":2403},{"class":324,"line":763},[2404],{"type":18,"tag":322,"props":2405,"children":2406},{},[2407],{"type":28,"value":2408},"        print(wing)                                             # 输出翅膀的特征  \n",{"type":18,"tag":322,"props":2410,"children":2411},{"class":324,"line":985},[2412],{"type":18,"tag":322,"props":2413,"children":2414},{},[2415],{"type":28,"value":2416},"        print(claw)                                             # 输出爪子的特征  \n",{"type":18,"tag":322,"props":2418,"children":2419},{"class":324,"line":994},[2420],{"type":18,"tag":322,"props":2421,"children":2422},{},[2423],{"type":28,"value":846},{"type":18,"tag":322,"props":2425,"children":2426},{"class":324,"line":1002},[2427],{"type":18,"tag":322,"props":2428,"children":2429},{},[2430],{"type":28,"value":2431},"    def fly(self, state):                                       # 定义飞行方法  \n",{"type":18,"tag":322,"props":2433,"children":2434},{"class":324,"line":1011},[2435],{"type":18,"tag":322,"props":2436,"children":2437},{},[2438],{"type":28,"value":2439},"        print(state)  \n",{"type":18,"tag":322,"props":2441,"children":2442},{"class":324,"line":1020},[2443],{"type":18,"tag":322,"props":2444,"children":2445},{},[2446],{"type":28,"value":846},{"type":18,"tag":322,"props":2448,"children":2449},{"class":324,"line":1029},[2450],{"type":18,"tag":322,"props":2451,"children":2452},{},[2453],{"type":28,"value":2454},"'''**************调用方法*********************'''  \n",{"type":18,"tag":322,"props":2456,"children":2457},{"class":324,"line":1535},[2458],{"type":18,"tag":322,"props":2459,"children":2460},{},[2461],{"type":28,"value":846},{"type":18,"tag":322,"props":2463,"children":2464},{"class":324,"line":1544},[2465],{"type":18,"tag":322,"props":2466,"children":2467},{},[2468],{"type":28,"value":2469},"beak_1 = \"喙的基部较高，长度和头部的长度几乎相等\"                   # 喙的特征  \n",{"type":18,"tag":322,"props":2471,"children":2472},{"class":324,"line":1818},[2473],{"type":18,"tag":322,"props":2474,"children":2475},{},[2476],{"type":28,"value":2477},"wing_1 = \"翅膀长而尖\"                                            # 翅膀的特征  \n",{"type":18,"tag":322,"props":2479,"children":2480},{"class":324,"line":1827},[2481],{"type":18,"tag":322,"props":2482,"children":2483},{},[2484],{"type":28,"value":2485},"claw_1 = \"爪子是蹼状的\"                                          # 爪子的特征  \n",{"type":18,"tag":322,"props":2487,"children":2488},{"class":324,"line":1836},[2489],{"type":18,"tag":322,"props":2490,"children":2491},{},[2492],{"type":28,"value":2493},"wildGoose = Geese(beak_1, wing_1, claw_1)                       # 创建大雁类的实例  \n",{"type":18,"tag":322,"props":2495,"children":2496},{"class":324,"line":1845},[2497],{"type":18,"tag":322,"props":2498,"children":2499},{},[2500],{"type":28,"value":2501},"wildGoose.fly(\"我飞行的时候，一会儿排成个人字，一会排成个一字\")      # 调用实例方法\n",{"type":18,"tag":31,"props":2503,"children":2504},{},[2505],{"type":28,"value":2506},"运行结果如图所示：",{"type":18,"tag":31,"props":2508,"children":2509},{},[2510],{"type":18,"tag":63,"props":2511,"children":2514},{"alt":2512,"src":2513},"Pasted image 20250809095521","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250809095521.png",[],{"type":18,"tag":48,"props":2516,"children":2518},{"id":2517},"_42-创建数据成员并访问",[2519],{"type":28,"value":2520},"4.2 创建数据成员并访问",{"type":18,"tag":31,"props":2522,"children":2523},{},[2524],{"type":18,"tag":35,"props":2525,"children":2526},{},[2527],{"type":28,"value":2528},"数据成员是指在类中定义的变量，即属性，根据定义位置，又可以分为类属性和实例属性。",{"type":18,"tag":169,"props":2530,"children":2532},{"id":2531},"_421-类属性",[2533],{"type":28,"value":2534},"4.2.1 类属性",{"type":18,"tag":31,"props":2536,"children":2537},{},[2538,2543],{"type":18,"tag":35,"props":2539,"children":2540},{},[2541],{"type":28,"value":2542},"类属性是指定义在类中，并且在函数体外的属性",{"type":28,"value":2544},"。类属性可以在类的所有实例之间共享值，也就是在所有实例化的对象中公用。",{"type":18,"tag":110,"props":2546,"children":2547},{},[2548],{"type":18,"tag":31,"props":2549,"children":2550},{},[2551],{"type":28,"value":2552},"说明：类属性可以通过类名称或者实例名访问。",{"type":18,"tag":31,"props":2554,"children":2555},{},[2556],{"type":28,"value":2557},"例如，定义一个雁类Geese，在该类中定义3个类属性，用于记录雁类的特征，代码如下：",{"type":18,"tag":312,"props":2559,"children":2561},{"code":2560,"language":315,"meta":7,"className":316,"style":7},"class Geese:  \n    '''雁类'''  \n    neck = \"脖子较长\"                           # 定义类属性（脖子）  \n    wing = \"振翅频率高\"                         # 定义类属性（翅膀）  \n    leg = \"腿位于身体的中心支点，行走自如\"        # 定义类属性（腿）  \n  \n    def __init__(self):                       # 实例方法（相当于构造方法）  \n        print(\"我属于雁类！我有以下特征：\")  \n        print(Geese.neck)                     # 输出脖子的特征  \n        print(Geese.wing)                     # 输出翅膀的特征  \n        print(Geese.leg)                      # 输出腿的特征\n",[2562],{"type":18,"tag":303,"props":2563,"children":2564},{"__ignoreMap":7},[2565,2572,2580,2588,2596,2604,2611,2619,2627,2635,2643],{"type":18,"tag":322,"props":2566,"children":2567},{"class":324,"line":325},[2568],{"type":18,"tag":322,"props":2569,"children":2570},{},[2571],{"type":28,"value":814},{"type":18,"tag":322,"props":2573,"children":2574},{"class":324,"line":334},[2575],{"type":18,"tag":322,"props":2576,"children":2577},{},[2578],{"type":28,"value":2579},"    '''雁类'''  \n",{"type":18,"tag":322,"props":2581,"children":2582},{"class":324,"line":343},[2583],{"type":18,"tag":322,"props":2584,"children":2585},{},[2586],{"type":28,"value":2587},"    neck = \"脖子较长\"                           # 定义类属性（脖子）  \n",{"type":18,"tag":322,"props":2589,"children":2590},{"class":324,"line":744},[2591],{"type":18,"tag":322,"props":2592,"children":2593},{},[2594],{"type":28,"value":2595},"    wing = \"振翅频率高\"                         # 定义类属性（翅膀）  \n",{"type":18,"tag":322,"props":2597,"children":2598},{"class":324,"line":753},[2599],{"type":18,"tag":322,"props":2600,"children":2601},{},[2602],{"type":28,"value":2603},"    leg = \"腿位于身体的中心支点，行走自如\"        # 定义类属性（腿）  \n",{"type":18,"tag":322,"props":2605,"children":2606},{"class":324,"line":763},[2607],{"type":18,"tag":322,"props":2608,"children":2609},{},[2610],{"type":28,"value":846},{"type":18,"tag":322,"props":2612,"children":2613},{"class":324,"line":985},[2614],{"type":18,"tag":322,"props":2615,"children":2616},{},[2617],{"type":28,"value":2618},"    def __init__(self):                       # 实例方法（相当于构造方法）  \n",{"type":18,"tag":322,"props":2620,"children":2621},{"class":324,"line":994},[2622],{"type":18,"tag":322,"props":2623,"children":2624},{},[2625],{"type":28,"value":2626},"        print(\"我属于雁类！我有以下特征：\")  \n",{"type":18,"tag":322,"props":2628,"children":2629},{"class":324,"line":1002},[2630],{"type":18,"tag":322,"props":2631,"children":2632},{},[2633],{"type":28,"value":2634},"        print(Geese.neck)                     # 输出脖子的特征  \n",{"type":18,"tag":322,"props":2636,"children":2637},{"class":324,"line":1011},[2638],{"type":18,"tag":322,"props":2639,"children":2640},{},[2641],{"type":28,"value":2642},"        print(Geese.wing)                     # 输出翅膀的特征  \n",{"type":18,"tag":322,"props":2644,"children":2645},{"class":324,"line":1020},[2646],{"type":18,"tag":322,"props":2647,"children":2648},{},[2649],{"type":28,"value":2650},"        print(Geese.leg)                      # 输出腿的特征\n",{"type":18,"tag":31,"props":2652,"children":2653},{},[2654],{"type":28,"value":2655},"创建上面的类Geese，然后创建该类的实例，代码如下：",{"type":18,"tag":312,"props":2657,"children":2659},{"code":2658,"language":315,"meta":7,"className":316,"style":7},"geese = Geese()     # 实例化一个大雁类的对象\n",[2660],{"type":18,"tag":303,"props":2661,"children":2662},{"__ignoreMap":7},[2663],{"type":18,"tag":322,"props":2664,"children":2665},{"class":324,"line":325},[2666],{"type":18,"tag":322,"props":2667,"children":2668},{},[2669],{"type":28,"value":2658},{"type":18,"tag":31,"props":2671,"children":2672},{},[2673],{"type":28,"value":2674},"应用上面的代码创建Geese类的实例后，将显示以下内容：",{"type":18,"tag":312,"props":2676,"children":2678},{"code":2677},"我是雁类！我有以下特征：\n脖子较长\n振翅频率高\n腿位于身体的中心支点，行走自如\n",[2679],{"type":18,"tag":303,"props":2680,"children":2681},{"__ignoreMap":7},[2682],{"type":28,"value":2677},{"type":18,"tag":31,"props":2684,"children":2685},{},[2686],{"type":28,"value":2687},"下面通过一个具体的实例演示类属性在类的所有实例之间共享值的应用。",{"type":18,"tag":31,"props":2689,"children":2690},{},[2691],{"type":28,"value":2692},"场景模拟：春天来了，有一群大雁从南方返回北方。现在想要输出每只大雁的特征以及大雁的数量。",{"type":18,"tag":31,"props":2694,"children":2695},{},[2696],{"type":18,"tag":35,"props":2697,"children":2698},{},[2699],{"type":28,"value":2700},"实例 通过类属性统计类的实例个数",{"type":18,"tag":31,"props":2702,"children":2703},{},[2704,2705,2711,2713,2718],{"type":28,"value":2317},{"type":18,"tag":303,"props":2706,"children":2708},{"className":2707},[],[2709],{"type":28,"value":2710},"geese_a.py",{"type":28,"value":2712},"的文件，然后在该文件中定义一个雁类",{"type":18,"tag":303,"props":2714,"children":2716},{"className":2715},[],[2717],{"type":28,"value":2331},{"type":28,"value":2719},"，并在该类中定义4个类属性，前3个用于记录雁类的特征，第4个用于记录实例编号，然后定义一个构造方法，在该构造方法中将记录实例编号的类属性进行加1操作，并输出4个类属性的值，最后通过for循环创建4个雁类的实例，代码如下：",{"type":18,"tag":312,"props":2721,"children":2723},{"code":2722,"language":315,"meta":7,"className":316,"style":7},"class Geese:  \n    '''雁类'''  \n    neck = \"脖子较长\"                           # 类属性（脖子）  \n    wing = \"振翅频率高\"                         # 类属性（翅膀）  \n    leg = \"腿位于身体的中心支点，行走自如\"         # 类属性（腿）  \n    number = 0                                 # 编号  \n  \n    def __init__(self):                        # 构造方法  \n        Geese.number += 1                      # 将编号加1  \n        print(\"\\n我是第\"+str(Geese.number)+\"只大雁，我属于雁类！我有以下特征：\")  \n        print(Geese.neck)                      # 输出脖子的特征  \n        print(Geese.wing)                      # 输出翅膀的特征  \n        print(Geese.leg)                       # 输出腿的特征  \n  \n# 创建4个雁类的对象（相当于有4只大雁)  \nlist1 = []  \nfor i in range(4):                             # 循环4次  \n     list1.append(Geese())                     # 创建一个雁类的实例  \nprint(\"一共有\"+str(Geese.number)+\"只大雁\")\n",[2724],{"type":18,"tag":303,"props":2725,"children":2726},{"__ignoreMap":7},[2727,2734,2741,2749,2757,2765,2773,2780,2788,2796,2804,2812,2820,2828,2835,2843,2851,2859,2867],{"type":18,"tag":322,"props":2728,"children":2729},{"class":324,"line":325},[2730],{"type":18,"tag":322,"props":2731,"children":2732},{},[2733],{"type":28,"value":814},{"type":18,"tag":322,"props":2735,"children":2736},{"class":324,"line":334},[2737],{"type":18,"tag":322,"props":2738,"children":2739},{},[2740],{"type":28,"value":2579},{"type":18,"tag":322,"props":2742,"children":2743},{"class":324,"line":343},[2744],{"type":18,"tag":322,"props":2745,"children":2746},{},[2747],{"type":28,"value":2748},"    neck = \"脖子较长\"                           # 类属性（脖子）  \n",{"type":18,"tag":322,"props":2750,"children":2751},{"class":324,"line":744},[2752],{"type":18,"tag":322,"props":2753,"children":2754},{},[2755],{"type":28,"value":2756},"    wing = \"振翅频率高\"                         # 类属性（翅膀）  \n",{"type":18,"tag":322,"props":2758,"children":2759},{"class":324,"line":753},[2760],{"type":18,"tag":322,"props":2761,"children":2762},{},[2763],{"type":28,"value":2764},"    leg = \"腿位于身体的中心支点，行走自如\"         # 类属性（腿）  \n",{"type":18,"tag":322,"props":2766,"children":2767},{"class":324,"line":763},[2768],{"type":18,"tag":322,"props":2769,"children":2770},{},[2771],{"type":28,"value":2772},"    number = 0                                 # 编号  \n",{"type":18,"tag":322,"props":2774,"children":2775},{"class":324,"line":985},[2776],{"type":18,"tag":322,"props":2777,"children":2778},{},[2779],{"type":28,"value":846},{"type":18,"tag":322,"props":2781,"children":2782},{"class":324,"line":994},[2783],{"type":18,"tag":322,"props":2784,"children":2785},{},[2786],{"type":28,"value":2787},"    def __init__(self):                        # 构造方法  \n",{"type":18,"tag":322,"props":2789,"children":2790},{"class":324,"line":1002},[2791],{"type":18,"tag":322,"props":2792,"children":2793},{},[2794],{"type":28,"value":2795},"        Geese.number += 1                      # 将编号加1  \n",{"type":18,"tag":322,"props":2797,"children":2798},{"class":324,"line":1011},[2799],{"type":18,"tag":322,"props":2800,"children":2801},{},[2802],{"type":28,"value":2803},"        print(\"\\n我是第\"+str(Geese.number)+\"只大雁，我属于雁类！我有以下特征：\")  \n",{"type":18,"tag":322,"props":2805,"children":2806},{"class":324,"line":1020},[2807],{"type":18,"tag":322,"props":2808,"children":2809},{},[2810],{"type":28,"value":2811},"        print(Geese.neck)                      # 输出脖子的特征  \n",{"type":18,"tag":322,"props":2813,"children":2814},{"class":324,"line":1029},[2815],{"type":18,"tag":322,"props":2816,"children":2817},{},[2818],{"type":28,"value":2819},"        print(Geese.wing)                      # 输出翅膀的特征  \n",{"type":18,"tag":322,"props":2821,"children":2822},{"class":324,"line":1535},[2823],{"type":18,"tag":322,"props":2824,"children":2825},{},[2826],{"type":28,"value":2827},"        print(Geese.leg)                       # 输出腿的特征  \n",{"type":18,"tag":322,"props":2829,"children":2830},{"class":324,"line":1544},[2831],{"type":18,"tag":322,"props":2832,"children":2833},{},[2834],{"type":28,"value":846},{"type":18,"tag":322,"props":2836,"children":2837},{"class":324,"line":1818},[2838],{"type":18,"tag":322,"props":2839,"children":2840},{},[2841],{"type":28,"value":2842},"# 创建4个雁类的对象（相当于有4只大雁)  \n",{"type":18,"tag":322,"props":2844,"children":2845},{"class":324,"line":1827},[2846],{"type":18,"tag":322,"props":2847,"children":2848},{},[2849],{"type":28,"value":2850},"list1 = []  \n",{"type":18,"tag":322,"props":2852,"children":2853},{"class":324,"line":1836},[2854],{"type":18,"tag":322,"props":2855,"children":2856},{},[2857],{"type":28,"value":2858},"for i in range(4):                             # 循环4次  \n",{"type":18,"tag":322,"props":2860,"children":2861},{"class":324,"line":1845},[2862],{"type":18,"tag":322,"props":2863,"children":2864},{},[2865],{"type":28,"value":2866},"     list1.append(Geese())                     # 创建一个雁类的实例  \n",{"type":18,"tag":322,"props":2868,"children":2869},{"class":324,"line":1854},[2870],{"type":18,"tag":322,"props":2871,"children":2872},{},[2873],{"type":28,"value":2874},"print(\"一共有\"+str(Geese.number)+\"只大雁\")\n",{"type":18,"tag":31,"props":2876,"children":2877},{},[2878],{"type":28,"value":2879},"运行结果如下图所示：",{"type":18,"tag":31,"props":2881,"children":2882},{},[2883],{"type":18,"tag":63,"props":2884,"children":2887},{"alt":2885,"src":2886},"Pasted image 20250809101300","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250809101300.png",[],{"type":18,"tag":31,"props":2889,"children":2890},{},[2891,2892,2897],{"type":28,"value":877},{"type":18,"tag":35,"props":2893,"children":2894},{},[2895],{"type":28,"value":2896},"Python中除了可以通过类名称访问类属性，还可以动态地为类和对象添加属性",{"type":28,"value":2898},"。例如，在实例02的基础上为雁类添加一个beak属性，并通过类的实例访问该属性，可以在上面代码的后面再添加以下代码：",{"type":18,"tag":312,"props":2900,"children":2902},{"code":2901,"language":315,"meta":7,"className":316,"style":7},"Geese.beak = \"喙的基部较高，长度和头部的长度几乎相等\"   # 添加类属性  \nprint(\"第2只大雁的喙：\",list1[1].beak)               # 访问类属性\n",[2903],{"type":18,"tag":303,"props":2904,"children":2905},{"__ignoreMap":7},[2906,2914],{"type":18,"tag":322,"props":2907,"children":2908},{"class":324,"line":325},[2909],{"type":18,"tag":322,"props":2910,"children":2911},{},[2912],{"type":28,"value":2913},"Geese.beak = \"喙的基部较高，长度和头部的长度几乎相等\"   # 添加类属性  \n",{"type":18,"tag":322,"props":2915,"children":2916},{"class":324,"line":334},[2917],{"type":18,"tag":322,"props":2918,"children":2919},{},[2920],{"type":28,"value":2921},"print(\"第2只大雁的喙：\",list1[1].beak)               # 访问类属性\n",{"type":18,"tag":110,"props":2923,"children":2924},{},[2925],{"type":18,"tag":31,"props":2926,"children":2927},{},[2928],{"type":28,"value":2929},"说明：上面的代码只是以第2只大雁为例进行演示，读者也可以换成其他的大雁试试。",{"type":18,"tag":31,"props":2931,"children":2932},{},[2933],{"type":28,"value":2934},"运行后，将在原来的结果后面再显示以下内容：",{"type":18,"tag":312,"props":2936,"children":2938},{"code":2937},"第2只大雁的喙：喙的基部较高，长度和头部的长度几乎相等\n",[2939],{"type":18,"tag":303,"props":2940,"children":2941},{"__ignoreMap":7},[2942],{"type":28,"value":2937},{"type":18,"tag":110,"props":2944,"children":2945},{},[2946],{"type":18,"tag":31,"props":2947,"children":2948},{},[2949],{"type":28,"value":2950},"说明：除了可以动态地为类和对象添加属性，也可以修改类属性。修改结果将作用于该类的所有实例。",{"type":18,"tag":169,"props":2952,"children":2954},{"id":2953},"_422-实例属性",[2955],{"type":28,"value":2956},"4.2.2 实例属性",{"type":18,"tag":31,"props":2958,"children":2959},{},[2960,2965],{"type":18,"tag":35,"props":2961,"children":2962},{},[2963],{"type":28,"value":2964},"实例属性是指定义在类的方法中的属性",{"type":28,"value":2966},"，只作用于当前实例中。",{"type":18,"tag":31,"props":2968,"children":2969},{},[2970],{"type":28,"value":2971},"例如，定义一个雁类Geese，在该类的__init__()方法中定义3个实例属性，用于记录雁类的特征，代码如下：",{"type":18,"tag":312,"props":2973,"children":2975},{"code":2974,"language":315,"meta":7,"className":316,"style":7},"class Geese:  \n    '''雁类'''  \n    def __init__(self):                              # 实例方法（相当于构造方法）  \n        self.neck = \"脖子较长\"                        # 定义实例属性（脖子）  \n        self.wing = \"振翅频率高\"                      # 定义实例属性（翅膀）  \n        self.leg = \"腿位于身体的中心支点，行走自如\"     # 定义实例属性（腿）  \n        print(\"我属于雁类！我有以下特征：\")  \n        print(self.neck)                           # 输出脖子的特征  \n        print(self.wing)                           # 输出翅膀的特征  \n        print(self.leg)                            # 输出腿的特征\n",[2976],{"type":18,"tag":303,"props":2977,"children":2978},{"__ignoreMap":7},[2979,2986,2993,3001,3009,3017,3025,3032,3040,3048],{"type":18,"tag":322,"props":2980,"children":2981},{"class":324,"line":325},[2982],{"type":18,"tag":322,"props":2983,"children":2984},{},[2985],{"type":28,"value":814},{"type":18,"tag":322,"props":2987,"children":2988},{"class":324,"line":334},[2989],{"type":18,"tag":322,"props":2990,"children":2991},{},[2992],{"type":28,"value":2579},{"type":18,"tag":322,"props":2994,"children":2995},{"class":324,"line":343},[2996],{"type":18,"tag":322,"props":2997,"children":2998},{},[2999],{"type":28,"value":3000},"    def __init__(self):                              # 实例方法（相当于构造方法）  \n",{"type":18,"tag":322,"props":3002,"children":3003},{"class":324,"line":744},[3004],{"type":18,"tag":322,"props":3005,"children":3006},{},[3007],{"type":28,"value":3008},"        self.neck = \"脖子较长\"                        # 定义实例属性（脖子）  \n",{"type":18,"tag":322,"props":3010,"children":3011},{"class":324,"line":753},[3012],{"type":18,"tag":322,"props":3013,"children":3014},{},[3015],{"type":28,"value":3016},"        self.wing = \"振翅频率高\"                      # 定义实例属性（翅膀）  \n",{"type":18,"tag":322,"props":3018,"children":3019},{"class":324,"line":763},[3020],{"type":18,"tag":322,"props":3021,"children":3022},{},[3023],{"type":28,"value":3024},"        self.leg = \"腿位于身体的中心支点，行走自如\"     # 定义实例属性（腿）  \n",{"type":18,"tag":322,"props":3026,"children":3027},{"class":324,"line":985},[3028],{"type":18,"tag":322,"props":3029,"children":3030},{},[3031],{"type":28,"value":2626},{"type":18,"tag":322,"props":3033,"children":3034},{"class":324,"line":994},[3035],{"type":18,"tag":322,"props":3036,"children":3037},{},[3038],{"type":28,"value":3039},"        print(self.neck)                           # 输出脖子的特征  \n",{"type":18,"tag":322,"props":3041,"children":3042},{"class":324,"line":1002},[3043],{"type":18,"tag":322,"props":3044,"children":3045},{},[3046],{"type":28,"value":3047},"        print(self.wing)                           # 输出翅膀的特征  \n",{"type":18,"tag":322,"props":3049,"children":3050},{"class":324,"line":1011},[3051],{"type":18,"tag":322,"props":3052,"children":3053},{},[3054],{"type":28,"value":3055},"        print(self.leg)                            # 输出腿的特征\n",{"type":18,"tag":31,"props":3057,"children":3058},{},[3059],{"type":28,"value":2655},{"type":18,"tag":312,"props":3061,"children":3063},{"code":3062,"language":315,"meta":7,"className":316,"style":7},"geese = Geese()                                  # 实例化一个雁类的对象\n",[3064],{"type":18,"tag":303,"props":3065,"children":3066},{"__ignoreMap":7},[3067],{"type":18,"tag":322,"props":3068,"children":3069},{"class":324,"line":325},[3070],{"type":18,"tag":322,"props":3071,"children":3072},{},[3073],{"type":28,"value":3062},{"type":18,"tag":31,"props":3075,"children":3076},{},[3077],{"type":28,"value":2674},{"type":18,"tag":312,"props":3079,"children":3080},{"code":2677},[3081],{"type":18,"tag":303,"props":3082,"children":3083},{"__ignoreMap":7},[3084],{"type":28,"value":2677},{"type":18,"tag":110,"props":3086,"children":3087},{},[3088],{"type":18,"tag":31,"props":3089,"children":3090},{},[3091,3093,3099],{"type":28,"value":3092},"说明：实例属性只能通过实例名访问。如果通过类名访问实例属性，如执行",{"type":18,"tag":303,"props":3094,"children":3096},{"className":3095},[],[3097],{"type":28,"value":3098},"print(Geese.neck)",{"type":28,"value":3100},"将抛出如图所示的异常。",{"type":18,"tag":31,"props":3102,"children":3103},{},[3104],{"type":18,"tag":63,"props":3105,"children":3108},{"alt":3106,"src":3107},"Pasted image 20250809102417","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250809102417.png",[],{"type":18,"tag":31,"props":3110,"children":3111},{},[3112,3117],{"type":18,"tag":35,"props":3113,"children":3114},{},[3115],{"type":28,"value":3116},"对于实例属性也可以通过实例名称修改，与类属性不同，通过实例名称修改实例属性后，并不影响该类的另一个实例中相应的实例属性的值",{"type":28,"value":3118},"。例如，定义一个雁类，并在__init__()方法中定义一个实例属性，然后创建两个Geese类的实例，并且修改第一个实例的实例属性，最后分别输出实例1和实例2的实例属性，代码如下：",{"type":18,"tag":312,"props":3120,"children":3122},{"code":3121,"language":315,"meta":7,"className":316,"style":7},"class Geese:  \n    '''雁类'''  \n    def __init__(self):                           # 实例方法（相当于构造方法）  \n        self.neck = \"脖子较长\"                     # 定义实例属性（脖子）  \n        print(self.neck)                          # 输出脖子的特征  \n  \ngoose1 = Geese()                                 # 创建Geese类的实例1  \ngoose2 = Geese()                                 # 创建Geese类的实例2  \ngoose1.neck = \"脖子没有天鹅的长\"                   # 修改实例属性  \nprint(\"goose1的neck属性：\",goose1.neck)  \nprint(\"goose2的neck属性：\",goose2.neck)\n",[3123],{"type":18,"tag":303,"props":3124,"children":3125},{"__ignoreMap":7},[3126,3133,3140,3148,3156,3164,3171,3179,3187,3195,3203],{"type":18,"tag":322,"props":3127,"children":3128},{"class":324,"line":325},[3129],{"type":18,"tag":322,"props":3130,"children":3131},{},[3132],{"type":28,"value":814},{"type":18,"tag":322,"props":3134,"children":3135},{"class":324,"line":334},[3136],{"type":18,"tag":322,"props":3137,"children":3138},{},[3139],{"type":28,"value":2579},{"type":18,"tag":322,"props":3141,"children":3142},{"class":324,"line":343},[3143],{"type":18,"tag":322,"props":3144,"children":3145},{},[3146],{"type":28,"value":3147},"    def __init__(self):                           # 实例方法（相当于构造方法）  \n",{"type":18,"tag":322,"props":3149,"children":3150},{"class":324,"line":744},[3151],{"type":18,"tag":322,"props":3152,"children":3153},{},[3154],{"type":28,"value":3155},"        self.neck = \"脖子较长\"                     # 定义实例属性（脖子）  \n",{"type":18,"tag":322,"props":3157,"children":3158},{"class":324,"line":753},[3159],{"type":18,"tag":322,"props":3160,"children":3161},{},[3162],{"type":28,"value":3163},"        print(self.neck)                          # 输出脖子的特征  \n",{"type":18,"tag":322,"props":3165,"children":3166},{"class":324,"line":763},[3167],{"type":18,"tag":322,"props":3168,"children":3169},{},[3170],{"type":28,"value":846},{"type":18,"tag":322,"props":3172,"children":3173},{"class":324,"line":985},[3174],{"type":18,"tag":322,"props":3175,"children":3176},{},[3177],{"type":28,"value":3178},"goose1 = Geese()                                 # 创建Geese类的实例1  \n",{"type":18,"tag":322,"props":3180,"children":3181},{"class":324,"line":994},[3182],{"type":18,"tag":322,"props":3183,"children":3184},{},[3185],{"type":28,"value":3186},"goose2 = Geese()                                 # 创建Geese类的实例2  \n",{"type":18,"tag":322,"props":3188,"children":3189},{"class":324,"line":1002},[3190],{"type":18,"tag":322,"props":3191,"children":3192},{},[3193],{"type":28,"value":3194},"goose1.neck = \"脖子没有天鹅的长\"                   # 修改实例属性  \n",{"type":18,"tag":322,"props":3196,"children":3197},{"class":324,"line":1011},[3198],{"type":18,"tag":322,"props":3199,"children":3200},{},[3201],{"type":28,"value":3202},"print(\"goose1的neck属性：\",goose1.neck)  \n",{"type":18,"tag":322,"props":3204,"children":3205},{"class":324,"line":1020},[3206],{"type":18,"tag":322,"props":3207,"children":3208},{},[3209],{"type":28,"value":3210},"print(\"goose2的neck属性：\",goose2.neck)\n",{"type":18,"tag":31,"props":3212,"children":3213},{},[3214],{"type":28,"value":3215},"运行上面的代码，将显示以下内容：",{"type":18,"tag":312,"props":3217,"children":3219},{"code":3218},"脖子较长\n脖子较长\ngoose1的neck属性： 脖子没有天鹅的长\ngoose2的neck属性： 脖子较长\n",[3220],{"type":18,"tag":303,"props":3221,"children":3222},{"__ignoreMap":7},[3223],{"type":28,"value":3218},{"type":18,"tag":23,"props":3225,"children":3227},{"id":3226},"_5-访问限制",[3228],{"type":28,"value":3229},"5 访问限制",{"type":18,"tag":31,"props":3231,"children":3232},{},[3233,3235,3264],{"type":28,"value":3234},"在类的内部可以定义属性和方法，而在类的外部则可以直接调用属性或方法来操作数据，从而隐藏了类内部的复杂逻辑。但是Python并没有对属性和方法的访问权限进行限制。",{"type":18,"tag":35,"props":3236,"children":3237},{},[3238,3240,3246,3248,3254,3256,3262],{"type":28,"value":3239},"为了保证类内部的某些属性或方法不被外部所访问，可以在属性或方法名前面添加单下划线",{"type":18,"tag":303,"props":3241,"children":3243},{"className":3242},[],[3244],{"type":28,"value":3245},"(_foo)",{"type":28,"value":3247},"、双下划线",{"type":18,"tag":303,"props":3249,"children":3251},{"className":3250},[],[3252],{"type":28,"value":3253},"(__foo)",{"type":28,"value":3255},"或首尾加双下划线",{"type":18,"tag":303,"props":3257,"children":3259},{"className":3258},[],[3260],{"type":28,"value":3261},"(__foo__)",{"type":28,"value":3263},"，从而限制访问权限",{"type":28,"value":3265},"。其中，单下划线、双下划线、首尾双下划线的作用如下：",{"type":18,"tag":31,"props":3267,"children":3268},{},[3269,3271,3276,3278,3283,3285,3290,3292,3298,3303,3305,3310,3312,3318],{"type":28,"value":3270},"（1）",{"type":18,"tag":35,"props":3272,"children":3273},{},[3274],{"type":28,"value":3275},"首尾双下划线表示定义特殊方法，一般是系统定义名字",{"type":28,"value":3277},"，如",{"type":18,"tag":303,"props":3279,"children":3281},{"className":3280},[],[3282],{"type":28,"value":547},{"type":28,"value":3284},"。\n（2）以",{"type":18,"tag":35,"props":3286,"children":3287},{},[3288],{"type":28,"value":3289},"单下划线开头",{"type":28,"value":3291},"的表示",{"type":18,"tag":303,"props":3293,"children":3295},{"className":3294},[],[3296],{"type":28,"value":3297},"protected",{"type":18,"tag":35,"props":3299,"children":3300},{},[3301],{"type":28,"value":3302},"保护类型",{"type":28,"value":3304},"的成员，",{"type":18,"tag":35,"props":3306,"children":3307},{},[3308],{"type":28,"value":3309},"只允许类本身和子类进行访问",{"type":28,"value":3311},"，但不能使用",{"type":18,"tag":303,"props":3313,"children":3315},{"className":3314},[],[3316],{"type":28,"value":3317},"from module import *",{"type":28,"value":3319},"语句导入。",{"type":18,"tag":31,"props":3321,"children":3322},{},[3323,3325,3331,3333,3339,3341,3346,3348,3353,3355,3360],{"type":28,"value":3324},"例如，创建一个",{"type":18,"tag":303,"props":3326,"children":3328},{"className":3327},[],[3329],{"type":28,"value":3330},"Swan",{"type":28,"value":3332},"类，定义保护属性",{"type":18,"tag":303,"props":3334,"children":3336},{"className":3335},[],[3337],{"type":28,"value":3338},"_neck_swan",{"type":28,"value":3340},"，并使用",{"type":18,"tag":303,"props":3342,"children":3344},{"className":3343},[],[3345],{"type":28,"value":547},{"type":28,"value":3347},"方法访问该属性，然后创建",{"type":18,"tag":303,"props":3349,"children":3351},{"className":3350},[],[3352],{"type":28,"value":3330},{"type":28,"value":3354},"类的实例，并通过实例名输出保护属性",{"type":18,"tag":303,"props":3356,"children":3358},{"className":3357},[],[3359],{"type":28,"value":3338},{"type":28,"value":2355},{"type":18,"tag":312,"props":3362,"children":3364},{"code":3363,"language":315,"meta":7,"className":316,"style":7},"class Swan:  \n    '''天鹅类'''  \n    _neck_swan = '天鹅的脖子很长'                   # 定义保护属性  \n    def __init__(self):  \n        print(\"__init__():\", Swan._neck_swan)     # 在实例方法中访问保护属性  \n  \nswan = Swan()                                     # 创建Swan类的实例  \nprint(\"直接访问:\" , swan._neck_swan)               # 保护属性可以通过实例名访问\n",[3365],{"type":18,"tag":303,"props":3366,"children":3367},{"__ignoreMap":7},[3368,3376,3384,3392,3400,3408,3415,3423],{"type":18,"tag":322,"props":3369,"children":3370},{"class":324,"line":325},[3371],{"type":18,"tag":322,"props":3372,"children":3373},{},[3374],{"type":28,"value":3375},"class Swan:  \n",{"type":18,"tag":322,"props":3377,"children":3378},{"class":324,"line":334},[3379],{"type":18,"tag":322,"props":3380,"children":3381},{},[3382],{"type":28,"value":3383},"    '''天鹅类'''  \n",{"type":18,"tag":322,"props":3385,"children":3386},{"class":324,"line":343},[3387],{"type":18,"tag":322,"props":3388,"children":3389},{},[3390],{"type":28,"value":3391},"    _neck_swan = '天鹅的脖子很长'                   # 定义保护属性  \n",{"type":18,"tag":322,"props":3393,"children":3394},{"class":324,"line":744},[3395],{"type":18,"tag":322,"props":3396,"children":3397},{},[3398],{"type":28,"value":3399},"    def __init__(self):  \n",{"type":18,"tag":322,"props":3401,"children":3402},{"class":324,"line":753},[3403],{"type":18,"tag":322,"props":3404,"children":3405},{},[3406],{"type":28,"value":3407},"        print(\"__init__():\", Swan._neck_swan)     # 在实例方法中访问保护属性  \n",{"type":18,"tag":322,"props":3409,"children":3410},{"class":324,"line":763},[3411],{"type":18,"tag":322,"props":3412,"children":3413},{},[3414],{"type":28,"value":846},{"type":18,"tag":322,"props":3416,"children":3417},{"class":324,"line":985},[3418],{"type":18,"tag":322,"props":3419,"children":3420},{},[3421],{"type":28,"value":3422},"swan = Swan()                                     # 创建Swan类的实例  \n",{"type":18,"tag":322,"props":3424,"children":3425},{"class":324,"line":994},[3426],{"type":18,"tag":322,"props":3427,"children":3428},{},[3429],{"type":28,"value":3430},"print(\"直接访问:\" , swan._neck_swan)               # 保护属性可以通过实例名访问\n",{"type":18,"tag":31,"props":3432,"children":3433},{},[3434],{"type":28,"value":3435},"执行下面的代码，将显示以下内容：",{"type":18,"tag":312,"props":3437,"children":3439},{"code":3438},"__init__(): 天鹅的脖子很长\n直接访问: 天鹅的脖子很长\n",[3440],{"type":18,"tag":303,"props":3441,"children":3442},{"__ignoreMap":7},[3443],{"type":28,"value":3438},{"type":18,"tag":31,"props":3445,"children":3446},{},[3447],{"type":28,"value":3448},"从上面的运行结果中可以看出，保护属性可以通过实例名访问。",{"type":18,"tag":31,"props":3450,"children":3451},{},[3452,3454,3459,3461,3467,3472,3473,3478,3480,3485,3487,3500],{"type":28,"value":3453},"（3）",{"type":18,"tag":35,"props":3455,"children":3456},{},[3457],{"type":28,"value":3458},"双下划线",{"type":28,"value":3460},"表示",{"type":18,"tag":303,"props":3462,"children":3464},{"className":3463},[],[3465],{"type":28,"value":3466},"private",{"type":18,"tag":35,"props":3468,"children":3469},{},[3470],{"type":28,"value":3471},"私有类型",{"type":28,"value":3304},{"type":18,"tag":35,"props":3474,"children":3475},{},[3476],{"type":28,"value":3477},"只允许定义该方法的类本身进行访问",{"type":28,"value":3479},"，而且也",{"type":18,"tag":35,"props":3481,"children":3482},{},[3483],{"type":28,"value":3484},"不能通过类的实例进行访问",{"type":28,"value":3486},"，但是",{"type":18,"tag":35,"props":3488,"children":3489},{},[3490,3492,3498],{"type":28,"value":3491},"可以通过",{"type":18,"tag":303,"props":3493,"children":3495},{"className":3494},[],[3496],{"type":28,"value":3497},"类的实例名._类名__xxx",{"type":28,"value":3499},"方式访问",{"type":28,"value":86},{"type":18,"tag":31,"props":3502,"children":3503},{},[3504,3505,3510,3512,3518,3519,3524,3525,3530,3532,3537],{"type":28,"value":3324},{"type":18,"tag":303,"props":3506,"children":3508},{"className":3507},[],[3509],{"type":28,"value":3330},{"type":28,"value":3511},"类，定义私有属性",{"type":18,"tag":303,"props":3513,"children":3515},{"className":3514},[],[3516],{"type":28,"value":3517},"__neck_swan",{"type":28,"value":3340},{"type":18,"tag":303,"props":3520,"children":3522},{"className":3521},[],[3523],{"type":28,"value":547},{"type":28,"value":3347},{"type":18,"tag":303,"props":3526,"children":3528},{"className":3527},[],[3529],{"type":28,"value":3330},{"type":28,"value":3531},"类的实例，并通过实例名输出私有属性",{"type":18,"tag":303,"props":3533,"children":3535},{"className":3534},[],[3536],{"type":28,"value":3517},{"type":28,"value":2355},{"type":18,"tag":312,"props":3539,"children":3541},{"code":3540,"language":315,"meta":7,"className":316,"style":7},"class Swan:  \n    '''天鹅类'''  \n    __neck_swan = '天鹅的脖子很长'               # 定义私有属性  \n    def __init__(self):  \n        print(\"__init__():\", Swan.__neck_swan)  # 在实例方法中访问私有属性  \n  \nswan = Swan()                                   # 创建Swan类的实例  \nprint(\"加入类名:\" , swan._Swan__neck_swan)       # 私有属性，可以通过“实例名._类名__xxx”方式访问  \ntry:  \n    print(\"直接访问:\" , swan.__neck_swan)            # 私有属性不能通过实例名访问，出错 \nexcept AttributeError:  \n    print(\"访问出错\")\n",[3542],{"type":18,"tag":303,"props":3543,"children":3544},{"__ignoreMap":7},[3545,3552,3559,3567,3574,3582,3589,3597,3605,3613,3621,3629],{"type":18,"tag":322,"props":3546,"children":3547},{"class":324,"line":325},[3548],{"type":18,"tag":322,"props":3549,"children":3550},{},[3551],{"type":28,"value":3375},{"type":18,"tag":322,"props":3553,"children":3554},{"class":324,"line":334},[3555],{"type":18,"tag":322,"props":3556,"children":3557},{},[3558],{"type":28,"value":3383},{"type":18,"tag":322,"props":3560,"children":3561},{"class":324,"line":343},[3562],{"type":18,"tag":322,"props":3563,"children":3564},{},[3565],{"type":28,"value":3566},"    __neck_swan = '天鹅的脖子很长'               # 定义私有属性  \n",{"type":18,"tag":322,"props":3568,"children":3569},{"class":324,"line":744},[3570],{"type":18,"tag":322,"props":3571,"children":3572},{},[3573],{"type":28,"value":3399},{"type":18,"tag":322,"props":3575,"children":3576},{"class":324,"line":753},[3577],{"type":18,"tag":322,"props":3578,"children":3579},{},[3580],{"type":28,"value":3581},"        print(\"__init__():\", Swan.__neck_swan)  # 在实例方法中访问私有属性  \n",{"type":18,"tag":322,"props":3583,"children":3584},{"class":324,"line":763},[3585],{"type":18,"tag":322,"props":3586,"children":3587},{},[3588],{"type":28,"value":846},{"type":18,"tag":322,"props":3590,"children":3591},{"class":324,"line":985},[3592],{"type":18,"tag":322,"props":3593,"children":3594},{},[3595],{"type":28,"value":3596},"swan = Swan()                                   # 创建Swan类的实例  \n",{"type":18,"tag":322,"props":3598,"children":3599},{"class":324,"line":994},[3600],{"type":18,"tag":322,"props":3601,"children":3602},{},[3603],{"type":28,"value":3604},"print(\"加入类名:\" , swan._Swan__neck_swan)       # 私有属性，可以通过“实例名._类名__xxx”方式访问  \n",{"type":18,"tag":322,"props":3606,"children":3607},{"class":324,"line":1002},[3608],{"type":18,"tag":322,"props":3609,"children":3610},{},[3611],{"type":28,"value":3612},"try:  \n",{"type":18,"tag":322,"props":3614,"children":3615},{"class":324,"line":1011},[3616],{"type":18,"tag":322,"props":3617,"children":3618},{},[3619],{"type":28,"value":3620},"    print(\"直接访问:\" , swan.__neck_swan)            # 私有属性不能通过实例名访问，出错 \n",{"type":18,"tag":322,"props":3622,"children":3623},{"class":324,"line":1020},[3624],{"type":18,"tag":322,"props":3625,"children":3626},{},[3627],{"type":28,"value":3628},"except AttributeError:  \n",{"type":18,"tag":322,"props":3630,"children":3631},{"class":324,"line":1029},[3632],{"type":18,"tag":322,"props":3633,"children":3634},{},[3635],{"type":28,"value":3636},"    print(\"访问出错\")\n",{"type":18,"tag":31,"props":3638,"children":3639},{},[3640],{"type":28,"value":3641},"执行上面的代码后，将输出如图13所示的结果。",{"type":18,"tag":31,"props":3643,"children":3644},{},[3645],{"type":18,"tag":63,"props":3646,"children":3649},{"alt":3647,"src":3648},"Pasted image 20250809204614","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250809204614.png",[],{"type":18,"tag":31,"props":3651,"children":3652},{},[3653,3655,3668,3670,3681],{"type":28,"value":3654},"从上面的运行结果可以看出：",{"type":18,"tag":35,"props":3656,"children":3657},{},[3658,3660,3666],{"type":28,"value":3659},"私有属性不能直接通过",{"type":18,"tag":303,"props":3661,"children":3663},{"className":3662},[],[3664],{"type":28,"value":3665},"实例名+属性名",{"type":28,"value":3667},"访问",{"type":28,"value":3669},"，可以在类的实例方法中访问，也",{"type":18,"tag":35,"props":3671,"children":3672},{},[3673,3674,3680],{"type":28,"value":3491},{"type":18,"tag":303,"props":3675,"children":3677},{"className":3676},[],[3678],{"type":28,"value":3679},"实例名._类名__xxx",{"type":28,"value":3499},{"type":28,"value":86},{"type":18,"tag":23,"props":3683,"children":3685},{"id":3684},"_6-属性property",[3686],{"type":28,"value":3687},"6 属性（Property）",{"type":18,"tag":31,"props":3689,"children":3690},{},[3691,3693,3698,3700,3705],{"type":28,"value":3692},"此部分介绍的属性与上述部分介绍的类属性和实例属性不同。",{"type":18,"tag":35,"props":3694,"children":3695},{},[3696],{"type":28,"value":3697},"上述部分的属性将返回所存储的值，而此部分要介绍的属性则是一种特殊的属性，访问它时将计算它的值",{"type":28,"value":3699},"。另外，",{"type":18,"tag":35,"props":3701,"children":3702},{},[3703],{"type":28,"value":3704},"该属性还可以为属性添加安全保护机制",{"type":28,"value":86},{"type":18,"tag":48,"props":3707,"children":3709},{"id":3708},"_61创建用于计算的属性",[3710],{"type":28,"value":3711},"6.1  创建用于计算的属性",{"type":18,"tag":31,"props":3713,"children":3714},{},[3715,3717,3723,3725,3730,3731,3742],{"type":28,"value":3716},"在Python中，可以通过",{"type":18,"tag":303,"props":3718,"children":3720},{"className":3719},[],[3721],{"type":28,"value":3722},"@property",{"type":28,"value":3724},"（装饰器）",{"type":18,"tag":35,"props":3726,"children":3727},{},[3728],{"type":28,"value":3729},"将一个方法转换为属性，从而实现用于计算的属性",{"type":28,"value":86},{"type":18,"tag":35,"props":3732,"children":3733},{},[3734,3736],{"type":28,"value":3735},"将方法转换为属性后，可以直接通过方法名来访问方法，而不需要再添加一对小括号",{"type":18,"tag":303,"props":3737,"children":3739},{"className":3738},[],[3740],{"type":28,"value":3741},"()",{"type":28,"value":3743},"，这样可以让代码更加简洁。",{"type":18,"tag":31,"props":3745,"children":3746},{},[3747,3749,3754],{"type":28,"value":3748},"通过",{"type":18,"tag":303,"props":3750,"children":3752},{"className":3751},[],[3753],{"type":28,"value":3722},{"type":28,"value":3755},"创建用于计算的属性的语法格式如下：",{"type":18,"tag":312,"props":3757,"children":3759},{"code":3758,"language":315,"meta":7,"className":316,"style":7},"@property\ndef methodname(self):  \n    block\n",[3760],{"type":18,"tag":303,"props":3761,"children":3762},{"__ignoreMap":7},[3763,3771,3779],{"type":18,"tag":322,"props":3764,"children":3765},{"class":324,"line":325},[3766],{"type":18,"tag":322,"props":3767,"children":3768},{},[3769],{"type":28,"value":3770},"@property\n",{"type":18,"tag":322,"props":3772,"children":3773},{"class":324,"line":334},[3774],{"type":18,"tag":322,"props":3775,"children":3776},{},[3777],{"type":28,"value":3778},"def methodname(self):  \n",{"type":18,"tag":322,"props":3780,"children":3781},{"class":324,"line":343},[3782],{"type":18,"tag":322,"props":3783,"children":3784},{},[3785],{"type":28,"value":2177},{"type":18,"tag":31,"props":3787,"children":3788},{},[3789],{"type":28,"value":354},{"type":18,"tag":356,"props":3791,"children":3792},{},[3793,3810,3826],{"type":18,"tag":360,"props":3794,"children":3795},{},[3796,3802,3803,3808],{"type":18,"tag":303,"props":3797,"children":3799},{"className":3798},[],[3800],{"type":28,"value":3801},"methodname",{"type":28,"value":370},{"type":18,"tag":35,"props":3804,"children":3805},{},[3806],{"type":28,"value":3807},"指定方法名",{"type":28,"value":3809},"，一般使用小写字母开头。该名称最后将作为创建的属性名。",{"type":18,"tag":360,"props":3811,"children":3812},{},[3813,3818,3820,3825],{"type":18,"tag":303,"props":3814,"children":3816},{"className":3815},[],[3817],{"type":28,"value":663},{"type":28,"value":3819},"：必要参数，表示",{"type":18,"tag":35,"props":3821,"children":3822},{},[3823],{"type":28,"value":3824},"类的实例",{"type":28,"value":86},{"type":18,"tag":360,"props":3827,"children":3828},{},[3829,3834,3835,3840,3842,3848],{"type":18,"tag":303,"props":3830,"children":3832},{"className":3831},[],[3833],{"type":28,"value":2224},{"type":28,"value":420},{"type":18,"tag":35,"props":3836,"children":3837},{},[3838],{"type":28,"value":3839},"方法体",{"type":28,"value":3841},"，实现的具体功能。在方法体中，通常以",{"type":18,"tag":303,"props":3843,"children":3845},{"className":3844},[],[3846],{"type":28,"value":3847},"return",{"type":28,"value":3849},"语句结束，用于返回计算结果。",{"type":18,"tag":31,"props":3851,"children":3852},{},[3853,3855,3860,3862,3867],{"type":28,"value":3854},"例如，定义一个矩形类，在",{"type":18,"tag":303,"props":3856,"children":3858},{"className":3857},[],[3859],{"type":28,"value":547},{"type":28,"value":3861},"方法中定义两个实例属性，然后再定义一个计算矩形面积的方法，并应用",{"type":18,"tag":303,"props":3863,"children":3865},{"className":3864},[],[3866],{"type":28,"value":3722},{"type":28,"value":3868},"将其转换为属性，最后创建类的实例，并访问转换后的属性，代码如下：",{"type":18,"tag":312,"props":3870,"children":3872},{"code":3871,"language":315,"meta":7,"className":316,"style":7},"class Rect:  \n  \n    def __init__(self,width,height):  \n        self.width = width                  # 矩形的宽  \n        self.height = height                # 矩形的高  \n  \n    @property                               # 将方法转换为属性  \n    def area(self):                         # 计算矩形的面积的方法  \n        return self.width*self.height       # 返回矩形的面积  \n  \nrect = Rect(800,600)          # 创建类的实例  \nprint(\"面积为：\",rect.area)                 # 输出属性的值\n",[3873],{"type":18,"tag":303,"props":3874,"children":3875},{"__ignoreMap":7},[3876,3884,3891,3899,3907,3915,3922,3930,3938,3946,3953,3961],{"type":18,"tag":322,"props":3877,"children":3878},{"class":324,"line":325},[3879],{"type":18,"tag":322,"props":3880,"children":3881},{},[3882],{"type":28,"value":3883},"class Rect:  \n",{"type":18,"tag":322,"props":3885,"children":3886},{"class":324,"line":334},[3887],{"type":18,"tag":322,"props":3888,"children":3889},{},[3890],{"type":28,"value":846},{"type":18,"tag":322,"props":3892,"children":3893},{"class":324,"line":343},[3894],{"type":18,"tag":322,"props":3895,"children":3896},{},[3897],{"type":28,"value":3898},"    def __init__(self,width,height):  \n",{"type":18,"tag":322,"props":3900,"children":3901},{"class":324,"line":744},[3902],{"type":18,"tag":322,"props":3903,"children":3904},{},[3905],{"type":28,"value":3906},"        self.width = width                  # 矩形的宽  \n",{"type":18,"tag":322,"props":3908,"children":3909},{"class":324,"line":753},[3910],{"type":18,"tag":322,"props":3911,"children":3912},{},[3913],{"type":28,"value":3914},"        self.height = height                # 矩形的高  \n",{"type":18,"tag":322,"props":3916,"children":3917},{"class":324,"line":763},[3918],{"type":18,"tag":322,"props":3919,"children":3920},{},[3921],{"type":28,"value":846},{"type":18,"tag":322,"props":3923,"children":3924},{"class":324,"line":985},[3925],{"type":18,"tag":322,"props":3926,"children":3927},{},[3928],{"type":28,"value":3929},"    @property                               # 将方法转换为属性  \n",{"type":18,"tag":322,"props":3931,"children":3932},{"class":324,"line":994},[3933],{"type":18,"tag":322,"props":3934,"children":3935},{},[3936],{"type":28,"value":3937},"    def area(self):                         # 计算矩形的面积的方法  \n",{"type":18,"tag":322,"props":3939,"children":3940},{"class":324,"line":1002},[3941],{"type":18,"tag":322,"props":3942,"children":3943},{},[3944],{"type":28,"value":3945},"        return self.width*self.height       # 返回矩形的面积  \n",{"type":18,"tag":322,"props":3947,"children":3948},{"class":324,"line":1011},[3949],{"type":18,"tag":322,"props":3950,"children":3951},{},[3952],{"type":28,"value":846},{"type":18,"tag":322,"props":3954,"children":3955},{"class":324,"line":1020},[3956],{"type":18,"tag":322,"props":3957,"children":3958},{},[3959],{"type":28,"value":3960},"rect = Rect(800,600)          # 创建类的实例  \n",{"type":18,"tag":322,"props":3962,"children":3963},{"class":324,"line":1029},[3964],{"type":18,"tag":322,"props":3965,"children":3966},{},[3967],{"type":28,"value":3968},"print(\"面积为：\",rect.area)                 # 输出属性的值\n",{"type":18,"tag":31,"props":3970,"children":3971},{},[3972],{"type":28,"value":3973},"运行上面的代码，将显示以下运行结果：",{"type":18,"tag":312,"props":3975,"children":3977},{"code":3976},"面积为： 480000\n",[3978],{"type":18,"tag":303,"props":3979,"children":3980},{"__ignoreMap":7},[3981],{"type":28,"value":3976},{"type":18,"tag":110,"props":3983,"children":3984},{},[3985],{"type":18,"tag":31,"props":3986,"children":3987},{},[3988,3990,3995],{"type":28,"value":3989},"注意：通过",{"type":18,"tag":303,"props":3991,"children":3993},{"className":3992},[],[3994],{"type":28,"value":3722},{"type":28,"value":3996},"转换后的属性不能重新赋值，如果对其重新赋值，将抛出如图所示的异常信息。",{"type":18,"tag":31,"props":3998,"children":3999},{},[4000],{"type":18,"tag":63,"props":4001,"children":4004},{"alt":4002,"src":4003},"Pasted image 20250809211454","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250809211454.png",[],{"type":18,"tag":48,"props":4006,"children":4008},{"id":4007},"_62为属性添加安全保护机制",[4009],{"type":28,"value":4010},"6.2  为属性添加安全保护机制",{"type":18,"tag":31,"props":4012,"children":4013},{},[4014,4016,4028],{"type":28,"value":4015},"在Python中，默认情况下，创建的类属性或者实例是可以在类体外进行修改的，如果想要限制其不能在类体外修改，可以将其设置为私有的，但设置为私有后，在类体外也不能直接通过实例名+属性名获取它的值。如果",{"type":18,"tag":35,"props":4017,"children":4018},{},[4019,4021,4026],{"type":28,"value":4020},"想要创建一个可以读取但不能修改的属性，那么可以使用",{"type":18,"tag":303,"props":4022,"children":4024},{"className":4023},[],[4025],{"type":28,"value":3722},{"type":28,"value":4027},"实现只读属性",{"type":28,"value":86},{"type":18,"tag":31,"props":4030,"children":4031},{},[4032,4034,4040,4042,4048],{"type":28,"value":4033},"例如，创建一个电视节目类",{"type":18,"tag":303,"props":4035,"children":4037},{"className":4036},[],[4038],{"type":28,"value":4039},"TVshow",{"type":28,"value":4041},"，再创建一个",{"type":18,"tag":303,"props":4043,"children":4045},{"className":4044},[],[4046],{"type":28,"value":4047},"show",{"type":28,"value":4049},"属性，用于显示当前播放的电视节目，代码如下：",{"type":18,"tag":312,"props":4051,"children":4053},{"code":4052,"language":315,"meta":7,"className":316,"style":7},"class TVshow:   # 定义电视节目类  \n  \n    def __init__(self,show):  \n        self.__show = show  \n  \n    @property                            # 将方法转换为属性  \n    def show(self):                      # 定义show()方法  \n        return self.__show               # 返回私有属性的值  \n  \ntvshow = TVshow(\"正在播放《战狼2》\")       # 创建类的实例  \nprint(\"默认：\",tvshow.show)               # 获取属性值\n",[4054],{"type":18,"tag":303,"props":4055,"children":4056},{"__ignoreMap":7},[4057,4065,4072,4080,4088,4095,4103,4111,4119,4126,4134],{"type":18,"tag":322,"props":4058,"children":4059},{"class":324,"line":325},[4060],{"type":18,"tag":322,"props":4061,"children":4062},{},[4063],{"type":28,"value":4064},"class TVshow:   # 定义电视节目类  \n",{"type":18,"tag":322,"props":4066,"children":4067},{"class":324,"line":334},[4068],{"type":18,"tag":322,"props":4069,"children":4070},{},[4071],{"type":28,"value":846},{"type":18,"tag":322,"props":4073,"children":4074},{"class":324,"line":343},[4075],{"type":18,"tag":322,"props":4076,"children":4077},{},[4078],{"type":28,"value":4079},"    def __init__(self,show):  \n",{"type":18,"tag":322,"props":4081,"children":4082},{"class":324,"line":744},[4083],{"type":18,"tag":322,"props":4084,"children":4085},{},[4086],{"type":28,"value":4087},"        self.__show = show  \n",{"type":18,"tag":322,"props":4089,"children":4090},{"class":324,"line":753},[4091],{"type":18,"tag":322,"props":4092,"children":4093},{},[4094],{"type":28,"value":846},{"type":18,"tag":322,"props":4096,"children":4097},{"class":324,"line":763},[4098],{"type":18,"tag":322,"props":4099,"children":4100},{},[4101],{"type":28,"value":4102},"    @property                            # 将方法转换为属性  \n",{"type":18,"tag":322,"props":4104,"children":4105},{"class":324,"line":985},[4106],{"type":18,"tag":322,"props":4107,"children":4108},{},[4109],{"type":28,"value":4110},"    def show(self):                      # 定义show()方法  \n",{"type":18,"tag":322,"props":4112,"children":4113},{"class":324,"line":994},[4114],{"type":18,"tag":322,"props":4115,"children":4116},{},[4117],{"type":28,"value":4118},"        return self.__show               # 返回私有属性的值  \n",{"type":18,"tag":322,"props":4120,"children":4121},{"class":324,"line":1002},[4122],{"type":18,"tag":322,"props":4123,"children":4124},{},[4125],{"type":28,"value":846},{"type":18,"tag":322,"props":4127,"children":4128},{"class":324,"line":1011},[4129],{"type":18,"tag":322,"props":4130,"children":4131},{},[4132],{"type":28,"value":4133},"tvshow = TVshow(\"正在播放《战狼2》\")       # 创建类的实例  \n",{"type":18,"tag":322,"props":4135,"children":4136},{"class":324,"line":1020},[4137],{"type":18,"tag":322,"props":4138,"children":4139},{},[4140],{"type":28,"value":4141},"print(\"默认：\",tvshow.show)               # 获取属性值\n",{"type":18,"tag":31,"props":4143,"children":4144},{},[4145],{"type":28,"value":4146},"执行上面的代码，将显示以下内容：",{"type":18,"tag":312,"props":4148,"children":4150},{"code":4149},"默认： 正在播放《战狼2》\n",[4151],{"type":18,"tag":303,"props":4152,"children":4153},{"__ignoreMap":7},[4154],{"type":28,"value":4149},{"type":18,"tag":31,"props":4156,"children":4157},{},[4158,4160,4165],{"type":28,"value":4159},"通过上面的方法创建的",{"type":18,"tag":303,"props":4161,"children":4163},{"className":4162},[],[4164],{"type":28,"value":4047},{"type":28,"value":4166},"属性是只读的，尝试修改该属性的值，再重新获取。在上面代码中添加以下代码：",{"type":18,"tag":312,"props":4168,"children":4170},{"code":4169,"language":315,"meta":7,"className":316,"style":7},"tvshow.show = \"正在播放《红海行动》\"         # 修改属性值  \nprint(\"修改后：\",tvshow.show)              # 获取属性值\n",[4171],{"type":18,"tag":303,"props":4172,"children":4173},{"__ignoreMap":7},[4174,4182],{"type":18,"tag":322,"props":4175,"children":4176},{"class":324,"line":325},[4177],{"type":18,"tag":322,"props":4178,"children":4179},{},[4180],{"type":28,"value":4181},"tvshow.show = \"正在播放《红海行动》\"         # 修改属性值  \n",{"type":18,"tag":322,"props":4183,"children":4184},{"class":324,"line":334},[4185],{"type":18,"tag":322,"props":4186,"children":4187},{},[4188],{"type":28,"value":4189},"print(\"修改后：\",tvshow.show)              # 获取属性值\n",{"type":18,"tag":31,"props":4191,"children":4192},{},[4193],{"type":28,"value":4194},"运行后，将显示如图所示的运行结果，其中红字的异常信息就是修改属性show时抛出的异常。",{"type":18,"tag":31,"props":4196,"children":4197},{},[4198],{"type":18,"tag":63,"props":4199,"children":4202},{"alt":4200,"src":4201},"Pasted image 20250809212121","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250809212121.png",[],{"type":18,"tag":31,"props":4204,"children":4205},{},[4206,4211],{"type":18,"tag":35,"props":4207,"children":4208},{},[4209],{"type":28,"value":4210},"通过属性不仅可以将属性设置为只读属性，而且可以为属性设置拦截器，即允许对属性进行修改，但修改时需要遵守一定的约束",{"type":28,"value":86},{"type":18,"tag":31,"props":4213,"children":4214},{},[4215],{"type":28,"value":4216},"场景模拟：某电视台开设了电影点播功能，但要求只能从指定的几个电影（如《战狼2》《红海行动》《西游记女儿国》《熊出没・变形记》）中选择一个。",{"type":18,"tag":31,"props":4218,"children":4219},{},[4220],{"type":18,"tag":35,"props":4221,"children":4222},{},[4223],{"type":28,"value":4224},"实例  在模拟电影点播功能时应用属性",{"type":18,"tag":31,"props":4226,"children":4227},{},[4228,4229,4235,4237,4242,4244,4249],{"type":28,"value":2317},{"type":18,"tag":303,"props":4230,"children":4232},{"className":4231},[],[4233],{"type":28,"value":4234},"film.py",{"type":28,"value":4236},"的文件，然后在该文件中定义一个电视节目类",{"type":18,"tag":303,"props":4238,"children":4240},{"className":4239},[],[4241],{"type":28,"value":4039},{"type":28,"value":4243},"，并在该类中定义一个类属性，用于保存电影列表，然后在",{"type":18,"tag":303,"props":4245,"children":4247},{"className":4246},[],[4248],{"type":28,"value":547},{"type":28,"value":4250},"方法中定义一个私有的实例属性，再将该属性转换为可读取、可修改（有条件进行）的属性，最后创建类的实例，并获取和修改属性值，代码如下：",{"type":18,"tag":312,"props":4252,"children":4254},{"code":4253,"language":315,"meta":7,"className":316,"style":7},"class TVshow:                                     # 定义电视节目类  \n  \n    list_film = [\"战狼2\",\"红海行动\",\"西游记女儿国\",\"熊出没・变形记\"]  \n  \n    def __init__(self,show):  \n        self.__show = show  \n  \n    @property                                     # 将方法转换为属性  \n    def show(self):                               # 定义show()方法  \n        return self.__show                        # 返回私有属性的值  \n  \n    @show.setter                                  # 设置setter方法，让属性可修改  \n    def show(self,value):  \n        if value in TVshow.list_film:             # 判断值是否在列表中  \n            self.__show = \"您选择了《\" + value + \"》，稍后将播放\"  # 返回修改的值  \n        else:  \n            self.__show = \"您点播的电影不存在\"  \n  \ntvshow = TVshow(\"战狼2\")                          # 创建类的实例  \nprint(\"正在播放：《\",tvshow.show,\"》\")             # 获取属性值  \nprint(\"您可以从\",tvshow.list_film,\"中选择要点播放的电影\")  \ntvshow.show = \"红海行动\"                           # 修改属性值  \nprint(tvshow.show)                                # 获取属性值\n",[4255],{"type":18,"tag":303,"props":4256,"children":4257},{"__ignoreMap":7},[4258,4266,4273,4281,4288,4295,4302,4309,4317,4325,4333,4340,4348,4356,4364,4372,4380,4388,4395,4403,4411,4419,4427],{"type":18,"tag":322,"props":4259,"children":4260},{"class":324,"line":325},[4261],{"type":18,"tag":322,"props":4262,"children":4263},{},[4264],{"type":28,"value":4265},"class TVshow:                                     # 定义电视节目类  \n",{"type":18,"tag":322,"props":4267,"children":4268},{"class":324,"line":334},[4269],{"type":18,"tag":322,"props":4270,"children":4271},{},[4272],{"type":28,"value":846},{"type":18,"tag":322,"props":4274,"children":4275},{"class":324,"line":343},[4276],{"type":18,"tag":322,"props":4277,"children":4278},{},[4279],{"type":28,"value":4280},"    list_film = [\"战狼2\",\"红海行动\",\"西游记女儿国\",\"熊出没・变形记\"]  \n",{"type":18,"tag":322,"props":4282,"children":4283},{"class":324,"line":744},[4284],{"type":18,"tag":322,"props":4285,"children":4286},{},[4287],{"type":28,"value":846},{"type":18,"tag":322,"props":4289,"children":4290},{"class":324,"line":753},[4291],{"type":18,"tag":322,"props":4292,"children":4293},{},[4294],{"type":28,"value":4079},{"type":18,"tag":322,"props":4296,"children":4297},{"class":324,"line":763},[4298],{"type":18,"tag":322,"props":4299,"children":4300},{},[4301],{"type":28,"value":4087},{"type":18,"tag":322,"props":4303,"children":4304},{"class":324,"line":985},[4305],{"type":18,"tag":322,"props":4306,"children":4307},{},[4308],{"type":28,"value":846},{"type":18,"tag":322,"props":4310,"children":4311},{"class":324,"line":994},[4312],{"type":18,"tag":322,"props":4313,"children":4314},{},[4315],{"type":28,"value":4316},"    @property                                     # 将方法转换为属性  \n",{"type":18,"tag":322,"props":4318,"children":4319},{"class":324,"line":1002},[4320],{"type":18,"tag":322,"props":4321,"children":4322},{},[4323],{"type":28,"value":4324},"    def show(self):                               # 定义show()方法  \n",{"type":18,"tag":322,"props":4326,"children":4327},{"class":324,"line":1011},[4328],{"type":18,"tag":322,"props":4329,"children":4330},{},[4331],{"type":28,"value":4332},"        return self.__show                        # 返回私有属性的值  \n",{"type":18,"tag":322,"props":4334,"children":4335},{"class":324,"line":1020},[4336],{"type":18,"tag":322,"props":4337,"children":4338},{},[4339],{"type":28,"value":846},{"type":18,"tag":322,"props":4341,"children":4342},{"class":324,"line":1029},[4343],{"type":18,"tag":322,"props":4344,"children":4345},{},[4346],{"type":28,"value":4347},"    @show.setter                                  # 设置setter方法，让属性可修改  \n",{"type":18,"tag":322,"props":4349,"children":4350},{"class":324,"line":1535},[4351],{"type":18,"tag":322,"props":4352,"children":4353},{},[4354],{"type":28,"value":4355},"    def show(self,value):  \n",{"type":18,"tag":322,"props":4357,"children":4358},{"class":324,"line":1544},[4359],{"type":18,"tag":322,"props":4360,"children":4361},{},[4362],{"type":28,"value":4363},"        if value in TVshow.list_film:             # 判断值是否在列表中  \n",{"type":18,"tag":322,"props":4365,"children":4366},{"class":324,"line":1818},[4367],{"type":18,"tag":322,"props":4368,"children":4369},{},[4370],{"type":28,"value":4371},"            self.__show = \"您选择了《\" + value + \"》，稍后将播放\"  # 返回修改的值  \n",{"type":18,"tag":322,"props":4373,"children":4374},{"class":324,"line":1827},[4375],{"type":18,"tag":322,"props":4376,"children":4377},{},[4378],{"type":28,"value":4379},"        else:  \n",{"type":18,"tag":322,"props":4381,"children":4382},{"class":324,"line":1836},[4383],{"type":18,"tag":322,"props":4384,"children":4385},{},[4386],{"type":28,"value":4387},"            self.__show = \"您点播的电影不存在\"  \n",{"type":18,"tag":322,"props":4389,"children":4390},{"class":324,"line":1845},[4391],{"type":18,"tag":322,"props":4392,"children":4393},{},[4394],{"type":28,"value":846},{"type":18,"tag":322,"props":4396,"children":4397},{"class":324,"line":1854},[4398],{"type":18,"tag":322,"props":4399,"children":4400},{},[4401],{"type":28,"value":4402},"tvshow = TVshow(\"战狼2\")                          # 创建类的实例  \n",{"type":18,"tag":322,"props":4404,"children":4405},{"class":324,"line":1863},[4406],{"type":18,"tag":322,"props":4407,"children":4408},{},[4409],{"type":28,"value":4410},"print(\"正在播放：《\",tvshow.show,\"》\")             # 获取属性值  \n",{"type":18,"tag":322,"props":4412,"children":4413},{"class":324,"line":1872},[4414],{"type":18,"tag":322,"props":4415,"children":4416},{},[4417],{"type":28,"value":4418},"print(\"您可以从\",tvshow.list_film,\"中选择要点播放的电影\")  \n",{"type":18,"tag":322,"props":4420,"children":4421},{"class":324,"line":1881},[4422],{"type":18,"tag":322,"props":4423,"children":4424},{},[4425],{"type":28,"value":4426},"tvshow.show = \"红海行动\"                           # 修改属性值  \n",{"type":18,"tag":322,"props":4428,"children":4429},{"class":324,"line":1890},[4430],{"type":18,"tag":322,"props":4431,"children":4432},{},[4433],{"type":28,"value":4434},"print(tvshow.show)                                # 获取属性值\n",{"type":18,"tag":31,"props":4436,"children":4437},{},[4438],{"type":28,"value":4439},"运行结果如下所示：",{"type":18,"tag":312,"props":4441,"children":4443},{"code":4442},"正在播放：《 战狼2 》\n您可以从 ['战狼2', '红海行动', '西游记女儿国', '熊出没・变形记'] 中选择要点播放的电影\n您选择了《红海行动》，稍后将播放\n",[4444],{"type":18,"tag":303,"props":4445,"children":4446},{"__ignoreMap":7},[4447],{"type":28,"value":4442},{"type":18,"tag":31,"props":4449,"children":4450},{},[4451],{"type":28,"value":4452},"如果将第17行代码中的“红海行动”修改为“流浪地球”，将显示如下所示的效果。",{"type":18,"tag":312,"props":4454,"children":4456},{"code":4455},"正在播放：《 战狼2 》\n您可以从 ['战狼2', '红海行动', '西游记女儿国', '熊出没・变形记'] 中选择要点播放的电影\n您点播的电影不存在\n",[4457],{"type":18,"tag":303,"props":4458,"children":4459},{"__ignoreMap":7},[4460],{"type":28,"value":4455},{"type":18,"tag":23,"props":4462,"children":4464},{"id":4463},"_7-继承",[4465],{"type":28,"value":4466},"7 继承",{"type":18,"tag":48,"props":4468,"children":4470},{"id":4469},"_71-继承的基本语法",[4471],{"type":28,"value":4472},"7.1 继承的基本语法",{"type":18,"tag":31,"props":4474,"children":4475},{},[4476],{"type":28,"value":4477},"在编写类时，并不是每次都要从空白开始。当要编写的类和另一个已经存在的类之间存在一定的继承关系时，就可以通过继承来达到代码重用的目的，提高开发效率。",{"type":18,"tag":31,"props":4479,"children":4480},{},[4481,4483,4488],{"type":28,"value":4482},"继承是面向对象编程最重要的特性之一，它源于人们认识客观世界的过程，是自然界普遍存在的一种现象。例如，我们每一个人都从祖辈和父母那里继承了一些体貌特征，但是每个人却又不同于父母，因为每个人都存在自己的一些特性，这些特性是独有的，在父母身上并没有体现。在程序设计中实现继承，表示这个类拥有它继承的类的所有公有成员或者受保护成员。在面向对象编程中，",{"type":18,"tag":35,"props":4484,"children":4485},{},[4486],{"type":28,"value":4487},"被继承的类称为父类或基类，新的类称为子类或派生类",{"type":28,"value":86},{"type":18,"tag":31,"props":4490,"children":4491},{},[4492,4494,4499],{"type":28,"value":4493},"通过继承不仅可以实现代码的重用，还可以通过继承来理顺类与类之间的关系。在Python中，可以在类定义语句中，",{"type":18,"tag":35,"props":4495,"children":4496},{},[4497],{"type":28,"value":4498},"类名右侧使用一对小括号将要继承的基类名称括起来，从而实现类的继承",{"type":28,"value":4500},"。具体的语法格式如下：",{"type":18,"tag":312,"props":4502,"children":4504},{"code":4503,"language":315,"meta":7,"className":316,"style":7},"class ClassName(baseclasslist):  \n    '''类的帮助信息'''                     # 类文档字符串  \n    statement                            # 类体\n",[4505],{"type":18,"tag":303,"props":4506,"children":4507},{"__ignoreMap":7},[4508,4516,4524],{"type":18,"tag":322,"props":4509,"children":4510},{"class":324,"line":325},[4511],{"type":18,"tag":322,"props":4512,"children":4513},{},[4514],{"type":28,"value":4515},"class ClassName(baseclasslist):  \n",{"type":18,"tag":322,"props":4517,"children":4518},{"class":324,"line":334},[4519],{"type":18,"tag":322,"props":4520,"children":4521},{},[4522],{"type":28,"value":4523},"    '''类的帮助信息'''                     # 类文档字符串  \n",{"type":18,"tag":322,"props":4525,"children":4526},{"class":324,"line":343},[4527],{"type":18,"tag":322,"props":4528,"children":4529},{},[4530],{"type":28,"value":4531},"    statement                            # 类体\n",{"type":18,"tag":31,"props":4533,"children":4534},{},[4535],{"type":28,"value":354},{"type":18,"tag":356,"props":4537,"children":4538},{},[4539,4553,4577,4597],{"type":18,"tag":360,"props":4540,"children":4541},{},[4542,4547,4548,4552],{"type":18,"tag":303,"props":4543,"children":4545},{"className":4544},[],[4546],{"type":28,"value":368},{"type":28,"value":370},{"type":18,"tag":35,"props":4549,"children":4550},{},[4551],{"type":28,"value":375},{"type":28,"value":86},{"type":18,"tag":360,"props":4554,"children":4555},{},[4556,4562,4563,4568,4570,4575],{"type":18,"tag":303,"props":4557,"children":4559},{"className":4558},[],[4560],{"type":28,"value":4561},"baseclasslist",{"type":28,"value":370},{"type":18,"tag":35,"props":4564,"children":4565},{},[4566],{"type":28,"value":4567},"指定要继承的基类",{"type":28,"value":4569},"，可以有多个，类名之间用逗号",{"type":18,"tag":303,"props":4571,"children":4573},{"className":4572},[],[4574],{"type":28,"value":897},{"type":28,"value":4576},"分隔。如果不指定，将使用所有Python对象的根类object。",{"type":18,"tag":360,"props":4578,"children":4579},{},[4580,4585,4586,4590,4591,4596],{"type":18,"tag":303,"props":4581,"children":4583},{"className":4582},[],[4584],{"type":28,"value":393},{"type":28,"value":370},{"type":18,"tag":35,"props":4587,"children":4588},{},[4589],{"type":28,"value":399},{"type":28,"value":401},{"type":18,"tag":303,"props":4592,"children":4594},{"className":4593},[],[4595],{"type":28,"value":407},{"type":28,"value":409},{"type":18,"tag":360,"props":4598,"children":4599},{},[4600,4605,4606,4611,4613,4618],{"type":18,"tag":303,"props":4601,"children":4603},{"className":4602},[],[4604],{"type":28,"value":418},{"type":28,"value":420},{"type":18,"tag":35,"props":4607,"children":4608},{},[4609],{"type":28,"value":4610},"类体",{"type":28,"value":4612},"，主要由类变量（或类成员）、方法和属性等定义语句组成。如果在定义类时，没想好类的具体功能，也可以在类体中直接使用",{"type":18,"tag":303,"props":4614,"children":4616},{"className":4615},[],[4617],{"type":28,"value":433},{"type":28,"value":435},{"type":18,"tag":31,"props":4620,"children":4621},{},[4622],{"type":18,"tag":35,"props":4623,"children":4624},{},[4625],{"type":28,"value":4626},"实例  创建水果基类及其派生类",{"type":18,"tag":31,"props":4628,"children":4629},{},[4630,4631,4637,4639,4645,4647,4653,4655,4661,4663,4669,4671,4676,4678,4683,4684,4689,4691,4696],{"type":28,"value":2317},{"type":18,"tag":303,"props":4632,"children":4634},{"className":4633},[],[4635],{"type":28,"value":4636},"fruit.py",{"type":28,"value":4638},"的文件，然后在该文件中定义一个水果类",{"type":18,"tag":303,"props":4640,"children":4642},{"className":4641},[],[4643],{"type":28,"value":4644},"Fruit",{"type":28,"value":4646},"（作为基类），并在该类中定义一个类属性（用于保存水果默认的颜色）和一个",{"type":18,"tag":303,"props":4648,"children":4650},{"className":4649},[],[4651],{"type":28,"value":4652},"harvest()",{"type":28,"value":4654},"方法，然后创建",{"type":18,"tag":303,"props":4656,"children":4658},{"className":4657},[],[4659],{"type":28,"value":4660},"Apple",{"type":28,"value":4662},"类和",{"type":18,"tag":303,"props":4664,"children":4666},{"className":4665},[],[4667],{"type":28,"value":4668},"Orange",{"type":28,"value":4670},"类，都继承自",{"type":18,"tag":303,"props":4672,"children":4674},{"className":4673},[],[4675],{"type":28,"value":4644},{"type":28,"value":4677},"类，最后创建",{"type":18,"tag":303,"props":4679,"children":4681},{"className":4680},[],[4682],{"type":28,"value":4660},{"type":28,"value":4662},{"type":18,"tag":303,"props":4685,"children":4687},{"className":4686},[],[4688],{"type":28,"value":4668},{"type":28,"value":4690},"类的实例，并调用",{"type":18,"tag":303,"props":4692,"children":4694},{"className":4693},[],[4695],{"type":28,"value":4652},{"type":28,"value":4697},"方法（在基类中编写），代码如下：",{"type":18,"tag":312,"props":4699,"children":4701},{"code":4700,"language":315,"meta":7,"className":316,"style":7},"class Fruit:                                           # 定义水果类（基类）  \n    color = \"绿色\"                                     # 定义类属性  \n    def harvest(self, color):  \n        print(\"水果是：\" + color + \"的！\")              # 输出的是形式参数color  \n        print(\"水果已经收获……\")  \n        print(\"水果原来是：\" + Fruit.color + \"的！\")     # 输出的是类属性color  \n  \nclass Apple(Fruit):                                    # 定义苹果类（派生类)  \n    color = \"红色\"  \n    def __init__(self):  \n        print(\"我是苹果\")  \n  \nclass Orange(Fruit):                                   # 定义橘子类（派生类）  \n    color = \"橙色\"  \n    def __init__(self):  \n        print(\"\\n我是橘子\")  \n  \napple = Apple()                                         # 创建类的实例（苹果）  \napple.harvest(apple.color)                              # 调用基类的harvest()方法  \norange = Orange()                                       # 创建类的实例（橘子）  \norange.harvest(orange.color)                            # 调用基类的harvest()方法\n",[4702],{"type":18,"tag":303,"props":4703,"children":4704},{"__ignoreMap":7},[4705,4713,4721,4729,4737,4745,4753,4760,4768,4776,4783,4791,4798,4806,4814,4821,4829,4836,4844,4852,4860],{"type":18,"tag":322,"props":4706,"children":4707},{"class":324,"line":325},[4708],{"type":18,"tag":322,"props":4709,"children":4710},{},[4711],{"type":28,"value":4712},"class Fruit:                                           # 定义水果类（基类）  \n",{"type":18,"tag":322,"props":4714,"children":4715},{"class":324,"line":334},[4716],{"type":18,"tag":322,"props":4717,"children":4718},{},[4719],{"type":28,"value":4720},"    color = \"绿色\"                                     # 定义类属性  \n",{"type":18,"tag":322,"props":4722,"children":4723},{"class":324,"line":343},[4724],{"type":18,"tag":322,"props":4725,"children":4726},{},[4727],{"type":28,"value":4728},"    def harvest(self, color):  \n",{"type":18,"tag":322,"props":4730,"children":4731},{"class":324,"line":744},[4732],{"type":18,"tag":322,"props":4733,"children":4734},{},[4735],{"type":28,"value":4736},"        print(\"水果是：\" + color + \"的！\")              # 输出的是形式参数color  \n",{"type":18,"tag":322,"props":4738,"children":4739},{"class":324,"line":753},[4740],{"type":18,"tag":322,"props":4741,"children":4742},{},[4743],{"type":28,"value":4744},"        print(\"水果已经收获……\")  \n",{"type":18,"tag":322,"props":4746,"children":4747},{"class":324,"line":763},[4748],{"type":18,"tag":322,"props":4749,"children":4750},{},[4751],{"type":28,"value":4752},"        print(\"水果原来是：\" + Fruit.color + \"的！\")     # 输出的是类属性color  \n",{"type":18,"tag":322,"props":4754,"children":4755},{"class":324,"line":985},[4756],{"type":18,"tag":322,"props":4757,"children":4758},{},[4759],{"type":28,"value":846},{"type":18,"tag":322,"props":4761,"children":4762},{"class":324,"line":994},[4763],{"type":18,"tag":322,"props":4764,"children":4765},{},[4766],{"type":28,"value":4767},"class Apple(Fruit):                                    # 定义苹果类（派生类)  \n",{"type":18,"tag":322,"props":4769,"children":4770},{"class":324,"line":1002},[4771],{"type":18,"tag":322,"props":4772,"children":4773},{},[4774],{"type":28,"value":4775},"    color = \"红色\"  \n",{"type":18,"tag":322,"props":4777,"children":4778},{"class":324,"line":1011},[4779],{"type":18,"tag":322,"props":4780,"children":4781},{},[4782],{"type":28,"value":3399},{"type":18,"tag":322,"props":4784,"children":4785},{"class":324,"line":1020},[4786],{"type":18,"tag":322,"props":4787,"children":4788},{},[4789],{"type":28,"value":4790},"        print(\"我是苹果\")  \n",{"type":18,"tag":322,"props":4792,"children":4793},{"class":324,"line":1029},[4794],{"type":18,"tag":322,"props":4795,"children":4796},{},[4797],{"type":28,"value":846},{"type":18,"tag":322,"props":4799,"children":4800},{"class":324,"line":1535},[4801],{"type":18,"tag":322,"props":4802,"children":4803},{},[4804],{"type":28,"value":4805},"class Orange(Fruit):                                   # 定义橘子类（派生类）  \n",{"type":18,"tag":322,"props":4807,"children":4808},{"class":324,"line":1544},[4809],{"type":18,"tag":322,"props":4810,"children":4811},{},[4812],{"type":28,"value":4813},"    color = \"橙色\"  \n",{"type":18,"tag":322,"props":4815,"children":4816},{"class":324,"line":1818},[4817],{"type":18,"tag":322,"props":4818,"children":4819},{},[4820],{"type":28,"value":3399},{"type":18,"tag":322,"props":4822,"children":4823},{"class":324,"line":1827},[4824],{"type":18,"tag":322,"props":4825,"children":4826},{},[4827],{"type":28,"value":4828},"        print(\"\\n我是橘子\")  \n",{"type":18,"tag":322,"props":4830,"children":4831},{"class":324,"line":1836},[4832],{"type":18,"tag":322,"props":4833,"children":4834},{},[4835],{"type":28,"value":846},{"type":18,"tag":322,"props":4837,"children":4838},{"class":324,"line":1845},[4839],{"type":18,"tag":322,"props":4840,"children":4841},{},[4842],{"type":28,"value":4843},"apple = Apple()                                         # 创建类的实例（苹果）  \n",{"type":18,"tag":322,"props":4845,"children":4846},{"class":324,"line":1854},[4847],{"type":18,"tag":322,"props":4848,"children":4849},{},[4850],{"type":28,"value":4851},"apple.harvest(apple.color)                              # 调用基类的harvest()方法  \n",{"type":18,"tag":322,"props":4853,"children":4854},{"class":324,"line":1863},[4855],{"type":18,"tag":322,"props":4856,"children":4857},{},[4858],{"type":28,"value":4859},"orange = Orange()                                       # 创建类的实例（橘子）  \n",{"type":18,"tag":322,"props":4861,"children":4862},{"class":324,"line":1872},[4863],{"type":18,"tag":322,"props":4864,"children":4865},{},[4866],{"type":28,"value":4867},"orange.harvest(orange.color)                            # 调用基类的harvest()方法\n",{"type":18,"tag":31,"props":4869,"children":4870},{},[4871,4873,4878,4879,4884,4886,4891,4893,4898],{"type":28,"value":4872},"执行上面的代码，将显示如图所示的运行结果。从该运行结果中可以看出，虽然在",{"type":18,"tag":303,"props":4874,"children":4876},{"className":4875},[],[4877],{"type":28,"value":4660},{"type":28,"value":4662},{"type":18,"tag":303,"props":4880,"children":4882},{"className":4881},[],[4883],{"type":28,"value":4668},{"type":28,"value":4885},"类中没有",{"type":18,"tag":303,"props":4887,"children":4889},{"className":4888},[],[4890],{"type":28,"value":4652},{"type":28,"value":4892},"方法，但是",{"type":18,"tag":35,"props":4894,"children":4895},{},[4896],{"type":28,"value":4897},"Python允许派生类访问基类的方法",{"type":28,"value":86},{"type":18,"tag":31,"props":4900,"children":4901},{},[4902],{"type":18,"tag":63,"props":4903,"children":4906},{"alt":4904,"src":4905},"Pasted image 20250809213928","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250809213928.png",[],{"type":18,"tag":48,"props":4908,"children":4910},{"id":4909},"_72-方法重写",[4911],{"type":28,"value":4912},"7.2 方法重写",{"type":18,"tag":31,"props":4914,"children":4915},{},[4916],{"type":28,"value":4917},"基类的成员都会被派生类继承，当基类中的某个方法不完全适用于派生类时，就需要在派生类中重写父类的这个方法，这和Java语言中的方法重写是一样的。",{"type":18,"tag":31,"props":4919,"children":4920},{},[4921,4923,4928,4930,4935,4937,4942,4944,4949],{"type":28,"value":4922},"在实例中，基类中定义的",{"type":18,"tag":303,"props":4924,"children":4926},{"className":4925},[],[4927],{"type":28,"value":4652},{"type":28,"value":4929},"方法，无论派生类是什么水果都显示“水果……”，如果想要针对不同水果给出不同的提示，可以在派生类中重写",{"type":18,"tag":303,"props":4931,"children":4933},{"className":4932},[],[4934],{"type":28,"value":4652},{"type":28,"value":4936},"方法。例如，在创建派生类",{"type":18,"tag":303,"props":4938,"children":4940},{"className":4939},[],[4941],{"type":28,"value":4668},{"type":28,"value":4943},"时，重写",{"type":18,"tag":303,"props":4945,"children":4947},{"className":4946},[],[4948],{"type":28,"value":4652},{"type":28,"value":4950},"方法的代码如下：",{"type":18,"tag":312,"props":4952,"children":4954},{"code":4953,"language":315,"meta":7,"className":316,"style":7},"class Orange(Fruit):                                    # 定义橘子类（派生类)  \n    color = \"橙色\"  \n    def __init__(self):  \n        print(\"\\n我是橘子\")  \n  \n    def harvest(self, color):  \n        print(\"橘子是：\" + color + \"的！\")               # 输出的是形式参数color  \n        print(\"橘子已经收获……\")  \n        print(\"橘子原来是：\" + Fruit.color + \"的！\")      # 输出的是类属性color\n",[4955],{"type":18,"tag":303,"props":4956,"children":4957},{"__ignoreMap":7},[4958,4966,4973,4980,4987,4994,5001,5009,5017],{"type":18,"tag":322,"props":4959,"children":4960},{"class":324,"line":325},[4961],{"type":18,"tag":322,"props":4962,"children":4963},{},[4964],{"type":28,"value":4965},"class Orange(Fruit):                                    # 定义橘子类（派生类)  \n",{"type":18,"tag":322,"props":4967,"children":4968},{"class":324,"line":334},[4969],{"type":18,"tag":322,"props":4970,"children":4971},{},[4972],{"type":28,"value":4813},{"type":18,"tag":322,"props":4974,"children":4975},{"class":324,"line":343},[4976],{"type":18,"tag":322,"props":4977,"children":4978},{},[4979],{"type":28,"value":3399},{"type":18,"tag":322,"props":4981,"children":4982},{"class":324,"line":744},[4983],{"type":18,"tag":322,"props":4984,"children":4985},{},[4986],{"type":28,"value":4828},{"type":18,"tag":322,"props":4988,"children":4989},{"class":324,"line":753},[4990],{"type":18,"tag":322,"props":4991,"children":4992},{},[4993],{"type":28,"value":846},{"type":18,"tag":322,"props":4995,"children":4996},{"class":324,"line":763},[4997],{"type":18,"tag":322,"props":4998,"children":4999},{},[5000],{"type":28,"value":4728},{"type":18,"tag":322,"props":5002,"children":5003},{"class":324,"line":985},[5004],{"type":18,"tag":322,"props":5005,"children":5006},{},[5007],{"type":28,"value":5008},"        print(\"橘子是：\" + color + \"的！\")               # 输出的是形式参数color  \n",{"type":18,"tag":322,"props":5010,"children":5011},{"class":324,"line":994},[5012],{"type":18,"tag":322,"props":5013,"children":5014},{},[5015],{"type":28,"value":5016},"        print(\"橘子已经收获……\")  \n",{"type":18,"tag":322,"props":5018,"children":5019},{"class":324,"line":1002},[5020],{"type":18,"tag":322,"props":5021,"children":5022},{},[5023],{"type":28,"value":5024},"        print(\"橘子原来是：\" + Fruit.color + \"的！\")      # 输出的是类属性color\n",{"type":18,"tag":31,"props":5026,"children":5027},{},[5028,5030,5035],{"type":28,"value":5029},"添加",{"type":18,"tag":303,"props":5031,"children":5033},{"className":5032},[],[5034],{"type":28,"value":4652},{"type":28,"value":5036},"方法后（即在实例中添加上面代码中的05~08行代码），再次运行实例，将显示如图所示的运行结果。",{"type":18,"tag":31,"props":5038,"children":5039},{},[5040],{"type":18,"tag":63,"props":5041,"children":5044},{"alt":5042,"src":5043},"Pasted image 20250809215755","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250809215755.png",[],{"type":18,"tag":48,"props":5046,"children":5048},{"id":5047},"_73-派生类中调用基类的__init__方法",[5049],{"type":28,"value":5050},"7.3 派生类中调用基类的__init__()方法",{"type":18,"tag":31,"props":5052,"children":5053},{},[5054,5056,5061,5063,5068,5070,5075,5077,5082,5084,5090,5092,5097,5099,5104,5106,5111,5113,5118,5120,5125,5126,5131,5132,5137],{"type":28,"value":5055},"在派生类中定义",{"type":18,"tag":303,"props":5057,"children":5059},{"className":5058},[],[5060],{"type":28,"value":547},{"type":28,"value":5062},"方法时，不会自动调用基类的",{"type":18,"tag":303,"props":5064,"children":5066},{"className":5065},[],[5067],{"type":28,"value":547},{"type":28,"value":5069},"方法。例如，定义一个",{"type":18,"tag":303,"props":5071,"children":5073},{"className":5072},[],[5074],{"type":28,"value":4644},{"type":28,"value":5076},"类，在",{"type":18,"tag":303,"props":5078,"children":5080},{"className":5079},[],[5081],{"type":28,"value":547},{"type":28,"value":5083},"方法中创建类属性",{"type":18,"tag":303,"props":5085,"children":5087},{"className":5086},[],[5088],{"type":28,"value":5089},"color",{"type":28,"value":5091},"，然后在",{"type":18,"tag":303,"props":5093,"children":5095},{"className":5094},[],[5096],{"type":28,"value":4644},{"type":28,"value":5098},"类中定义一个",{"type":18,"tag":303,"props":5100,"children":5102},{"className":5101},[],[5103],{"type":28,"value":4652},{"type":28,"value":5105},"方法，在该方法中输出类属性",{"type":18,"tag":303,"props":5107,"children":5109},{"className":5108},[],[5110],{"type":28,"value":5089},{"type":28,"value":5112},"的值，再创建继承自",{"type":18,"tag":303,"props":5114,"children":5116},{"className":5115},[],[5117],{"type":28,"value":4644},{"type":28,"value":5119},"类的",{"type":18,"tag":303,"props":5121,"children":5123},{"className":5122},[],[5124],{"type":28,"value":4660},{"type":28,"value":4677},{"type":18,"tag":303,"props":5127,"children":5129},{"className":5128},[],[5130],{"type":28,"value":4660},{"type":28,"value":4690},{"type":18,"tag":303,"props":5133,"children":5135},{"className":5134},[],[5136],{"type":28,"value":4652},{"type":28,"value":712},{"type":18,"tag":312,"props":5139,"children":5141},{"code":5140,"language":315,"meta":7,"className":316,"style":7},"class Fruit:                             # 定义水果类（基类）  \n  \n    def __init__(self,color = \"绿色\"):  \n        Fruit.color = color              # 定义类属性  \n  \n    def harvest(self):  \n        print(\"水果原来是：\" + Fruit.color + \"的！\")    # 输出的是类属性color  \n  \nclass Apple(Fruit):                      # 定义苹果类（派生类）  \n  \n    def __init__(self):  \n        print(\"我是苹果\")  \n  \napple = Apple()                          # 创建类的实例（苹果）  \napple.harvest()                          # 调用基类的harvest()方法\n",[5142],{"type":18,"tag":303,"props":5143,"children":5144},{"__ignoreMap":7},[5145,5153,5160,5168,5176,5183,5191,5199,5206,5214,5221,5228,5235,5242,5250],{"type":18,"tag":322,"props":5146,"children":5147},{"class":324,"line":325},[5148],{"type":18,"tag":322,"props":5149,"children":5150},{},[5151],{"type":28,"value":5152},"class Fruit:                             # 定义水果类（基类）  \n",{"type":18,"tag":322,"props":5154,"children":5155},{"class":324,"line":334},[5156],{"type":18,"tag":322,"props":5157,"children":5158},{},[5159],{"type":28,"value":846},{"type":18,"tag":322,"props":5161,"children":5162},{"class":324,"line":343},[5163],{"type":18,"tag":322,"props":5164,"children":5165},{},[5166],{"type":28,"value":5167},"    def __init__(self,color = \"绿色\"):  \n",{"type":18,"tag":322,"props":5169,"children":5170},{"class":324,"line":744},[5171],{"type":18,"tag":322,"props":5172,"children":5173},{},[5174],{"type":28,"value":5175},"        Fruit.color = color              # 定义类属性  \n",{"type":18,"tag":322,"props":5177,"children":5178},{"class":324,"line":753},[5179],{"type":18,"tag":322,"props":5180,"children":5181},{},[5182],{"type":28,"value":846},{"type":18,"tag":322,"props":5184,"children":5185},{"class":324,"line":763},[5186],{"type":18,"tag":322,"props":5187,"children":5188},{},[5189],{"type":28,"value":5190},"    def harvest(self):  \n",{"type":18,"tag":322,"props":5192,"children":5193},{"class":324,"line":985},[5194],{"type":18,"tag":322,"props":5195,"children":5196},{},[5197],{"type":28,"value":5198},"        print(\"水果原来是：\" + Fruit.color + \"的！\")    # 输出的是类属性color  \n",{"type":18,"tag":322,"props":5200,"children":5201},{"class":324,"line":994},[5202],{"type":18,"tag":322,"props":5203,"children":5204},{},[5205],{"type":28,"value":846},{"type":18,"tag":322,"props":5207,"children":5208},{"class":324,"line":1002},[5209],{"type":18,"tag":322,"props":5210,"children":5211},{},[5212],{"type":28,"value":5213},"class Apple(Fruit):                      # 定义苹果类（派生类）  \n",{"type":18,"tag":322,"props":5215,"children":5216},{"class":324,"line":1011},[5217],{"type":18,"tag":322,"props":5218,"children":5219},{},[5220],{"type":28,"value":846},{"type":18,"tag":322,"props":5222,"children":5223},{"class":324,"line":1020},[5224],{"type":18,"tag":322,"props":5225,"children":5226},{},[5227],{"type":28,"value":3399},{"type":18,"tag":322,"props":5229,"children":5230},{"class":324,"line":1029},[5231],{"type":18,"tag":322,"props":5232,"children":5233},{},[5234],{"type":28,"value":4790},{"type":18,"tag":322,"props":5236,"children":5237},{"class":324,"line":1535},[5238],{"type":18,"tag":322,"props":5239,"children":5240},{},[5241],{"type":28,"value":846},{"type":18,"tag":322,"props":5243,"children":5244},{"class":324,"line":1544},[5245],{"type":18,"tag":322,"props":5246,"children":5247},{},[5248],{"type":28,"value":5249},"apple = Apple()                          # 创建类的实例（苹果）  \n",{"type":18,"tag":322,"props":5251,"children":5252},{"class":324,"line":1818},[5253],{"type":18,"tag":322,"props":5254,"children":5255},{},[5256],{"type":28,"value":5257},"apple.harvest()                          # 调用基类的harvest()方法\n",{"type":18,"tag":31,"props":5259,"children":5260},{},[5261],{"type":28,"value":5262},"执行上面的代码后，将显示如图所示的异常信息：",{"type":18,"tag":31,"props":5264,"children":5265},{},[5266],{"type":18,"tag":63,"props":5267,"children":5270},{"alt":5268,"src":5269},"Pasted image 20250809220147","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250809220147.png",[],{"type":18,"tag":31,"props":5272,"children":5273},{},[5274,5276,5281,5283,5289,5291,5296],{"type":28,"value":5275},"因此，要让派生类调用基类的",{"type":18,"tag":303,"props":5277,"children":5279},{"className":5278},[],[5280],{"type":28,"value":547},{"type":28,"value":5282},"方法进行必要的初始化，需要在派生类使用",{"type":18,"tag":303,"props":5284,"children":5286},{"className":5285},[],[5287],{"type":28,"value":5288},"super()",{"type":28,"value":5290},"函数调用基类的",{"type":18,"tag":303,"props":5292,"children":5294},{"className":5293},[],[5295],{"type":28,"value":547},{"type":28,"value":5297},"方法。例如，在上面代码的第8行代码的下方添加以下代码：",{"type":18,"tag":312,"props":5299,"children":5301},{"code":5300,"language":315,"meta":7,"className":316,"style":7},"super().__init__()      # 调用基类的__init__()方法\n",[5302],{"type":18,"tag":303,"props":5303,"children":5304},{"__ignoreMap":7},[5305],{"type":18,"tag":322,"props":5306,"children":5307},{"class":324,"line":325},[5308],{"type":18,"tag":322,"props":5309,"children":5310},{},[5311],{"type":28,"value":5300},{"type":18,"tag":110,"props":5313,"children":5314},{},[5315],{"type":18,"tag":31,"props":5316,"children":5317},{},[5318],{"type":28,"value":5319},"注意：在添加上面的代码时，一定要注意缩进的正确性。",{"type":18,"tag":31,"props":5321,"children":5322},{},[5323],{"type":28,"value":5324},"运行后将显示以下正常的运行结果：",{"type":18,"tag":312,"props":5326,"children":5328},{"code":5327},"我是苹果\n水果原来是：绿色的！\n",[5329],{"type":18,"tag":303,"props":5330,"children":5331},{"__ignoreMap":7},[5332],{"type":28,"value":5327},{"type":18,"tag":31,"props":5334,"children":5335},{},[5336,5338,5343],{"type":28,"value":5337},"下面通过一个具体实例演示派生类中调用基类的",{"type":18,"tag":303,"props":5339,"children":5341},{"className":5340},[],[5342],{"type":28,"value":547},{"type":28,"value":5344},"方法的具体的应用。",{"type":18,"tag":31,"props":5346,"children":5347},{},[5348],{"type":18,"tag":35,"props":5349,"children":5350},{},[5351],{"type":28,"value":5352},"实例  在派生类中调用基类的__init__()方法定义类属性",{"type":18,"tag":31,"props":5354,"children":5355},{},[5356,5357,5362,5363,5368,5370,5375,5377,5382,5383,5388,5390,5395,5396,5402,5403,5408,5409,5414,5415,5420,5421,5426],{"type":28,"value":2317},{"type":18,"tag":303,"props":5358,"children":5360},{"className":5359},[],[5361],{"type":28,"value":4636},{"type":28,"value":4638},{"type":18,"tag":303,"props":5364,"children":5366},{"className":5365},[],[5367],{"type":28,"value":4644},{"type":28,"value":5369},"（作为基类），并在该类中定义",{"type":18,"tag":303,"props":5371,"children":5373},{"className":5372},[],[5374],{"type":28,"value":547},{"type":28,"value":5376},"方法，在该方法中定义一个类属性（用于保存水果默认的颜色），然后在",{"type":18,"tag":303,"props":5378,"children":5380},{"className":5379},[],[5381],{"type":28,"value":4644},{"type":28,"value":5098},{"type":18,"tag":303,"props":5384,"children":5386},{"className":5385},[],[5387],{"type":28,"value":4652},{"type":28,"value":5389},"方法，再创建",{"type":18,"tag":303,"props":5391,"children":5393},{"className":5392},[],[5394],{"type":28,"value":4660},{"type":28,"value":4662},{"type":18,"tag":303,"props":5397,"children":5399},{"className":5398},[],[5400],{"type":28,"value":5401},"Sapodilla",{"type":28,"value":4670},{"type":18,"tag":303,"props":5404,"children":5406},{"className":5405},[],[5407],{"type":28,"value":4644},{"type":28,"value":4677},{"type":18,"tag":303,"props":5410,"children":5412},{"className":5411},[],[5413],{"type":28,"value":4660},{"type":28,"value":4662},{"type":18,"tag":303,"props":5416,"children":5418},{"className":5417},[],[5419],{"type":28,"value":5401},{"type":28,"value":4690},{"type":18,"tag":303,"props":5422,"children":5424},{"className":5423},[],[5425],{"type":28,"value":4652},{"type":28,"value":4697},{"type":18,"tag":312,"props":5428,"children":5430},{"code":5429,"language":315,"meta":7,"className":316,"style":7},"class Fruit:                                            # 定义水果类（基类）  \n  \n    def __init__(self, color=\"绿色\"):  \n        Fruit.color = color                             # 定义类属性  \n  \n    def harvest(self, color):  \n        print(\"水果是：\" + self.color + \"的！\")           # 输出的是形式参数color  \n        print(\"水果已经收获……\")  \n        print(\"水果原来是：\" + Fruit.color + \"的！\")      # 输出的是类属性color  \n  \nclass Apple(Fruit):                                     # 定义苹果类（派生类）  \n    color = \"红色\"  \n    def __init__(self):  \n        print(\"我是苹果\")  \n        super().__init__()                              # 调用基类的__init__()方法  \n  \nclass Sapodilla(Fruit):                                 # 定义人参果类（派生类）  \n  \n    def __init__(self, color):  \n        print(\"\\n我是人参果\")  \n        super().__init__(color)                         # 调用基类的__init__()方法  \n  \n    # 重写harvest()方法的代码  \n    def harvest(self, color):  \n        print(\"人参果是：\" + color + \"的！\")              # 输出的是形式参数color  \n        print(\"人参果已经收获……\")  \n        print(\"人参果原来是：\" + Fruit.color + \"的！\")    # 输出的是类属性color  \n  \napple = Apple()                                         # 创建类的实例（苹果）  \napple.harvest(apple.color)                              # 调用harvest()方法  \nsapodilla = Sapodilla(\"白色\")                            # 创建类的实例（人参果）  \nsapodilla.harvest(\"金黄色带紫色条纹\")                     # 调用harvest()方法\n",[5431],{"type":18,"tag":303,"props":5432,"children":5433},{"__ignoreMap":7},[5434,5442,5449,5457,5465,5472,5479,5487,5494,5502,5509,5517,5524,5531,5538,5546,5553,5561,5568,5576,5584,5592,5599,5607,5614,5622,5630,5638,5645,5653,5662,5671],{"type":18,"tag":322,"props":5435,"children":5436},{"class":324,"line":325},[5437],{"type":18,"tag":322,"props":5438,"children":5439},{},[5440],{"type":28,"value":5441},"class Fruit:                                            # 定义水果类（基类）  \n",{"type":18,"tag":322,"props":5443,"children":5444},{"class":324,"line":334},[5445],{"type":18,"tag":322,"props":5446,"children":5447},{},[5448],{"type":28,"value":846},{"type":18,"tag":322,"props":5450,"children":5451},{"class":324,"line":343},[5452],{"type":18,"tag":322,"props":5453,"children":5454},{},[5455],{"type":28,"value":5456},"    def __init__(self, color=\"绿色\"):  \n",{"type":18,"tag":322,"props":5458,"children":5459},{"class":324,"line":744},[5460],{"type":18,"tag":322,"props":5461,"children":5462},{},[5463],{"type":28,"value":5464},"        Fruit.color = color                             # 定义类属性  \n",{"type":18,"tag":322,"props":5466,"children":5467},{"class":324,"line":753},[5468],{"type":18,"tag":322,"props":5469,"children":5470},{},[5471],{"type":28,"value":846},{"type":18,"tag":322,"props":5473,"children":5474},{"class":324,"line":763},[5475],{"type":18,"tag":322,"props":5476,"children":5477},{},[5478],{"type":28,"value":4728},{"type":18,"tag":322,"props":5480,"children":5481},{"class":324,"line":985},[5482],{"type":18,"tag":322,"props":5483,"children":5484},{},[5485],{"type":28,"value":5486},"        print(\"水果是：\" + self.color + \"的！\")           # 输出的是形式参数color  \n",{"type":18,"tag":322,"props":5488,"children":5489},{"class":324,"line":994},[5490],{"type":18,"tag":322,"props":5491,"children":5492},{},[5493],{"type":28,"value":4744},{"type":18,"tag":322,"props":5495,"children":5496},{"class":324,"line":1002},[5497],{"type":18,"tag":322,"props":5498,"children":5499},{},[5500],{"type":28,"value":5501},"        print(\"水果原来是：\" + Fruit.color + \"的！\")      # 输出的是类属性color  \n",{"type":18,"tag":322,"props":5503,"children":5504},{"class":324,"line":1011},[5505],{"type":18,"tag":322,"props":5506,"children":5507},{},[5508],{"type":28,"value":846},{"type":18,"tag":322,"props":5510,"children":5511},{"class":324,"line":1020},[5512],{"type":18,"tag":322,"props":5513,"children":5514},{},[5515],{"type":28,"value":5516},"class Apple(Fruit):                                     # 定义苹果类（派生类）  \n",{"type":18,"tag":322,"props":5518,"children":5519},{"class":324,"line":1029},[5520],{"type":18,"tag":322,"props":5521,"children":5522},{},[5523],{"type":28,"value":4775},{"type":18,"tag":322,"props":5525,"children":5526},{"class":324,"line":1535},[5527],{"type":18,"tag":322,"props":5528,"children":5529},{},[5530],{"type":28,"value":3399},{"type":18,"tag":322,"props":5532,"children":5533},{"class":324,"line":1544},[5534],{"type":18,"tag":322,"props":5535,"children":5536},{},[5537],{"type":28,"value":4790},{"type":18,"tag":322,"props":5539,"children":5540},{"class":324,"line":1818},[5541],{"type":18,"tag":322,"props":5542,"children":5543},{},[5544],{"type":28,"value":5545},"        super().__init__()                              # 调用基类的__init__()方法  \n",{"type":18,"tag":322,"props":5547,"children":5548},{"class":324,"line":1827},[5549],{"type":18,"tag":322,"props":5550,"children":5551},{},[5552],{"type":28,"value":846},{"type":18,"tag":322,"props":5554,"children":5555},{"class":324,"line":1836},[5556],{"type":18,"tag":322,"props":5557,"children":5558},{},[5559],{"type":28,"value":5560},"class Sapodilla(Fruit):                                 # 定义人参果类（派生类）  \n",{"type":18,"tag":322,"props":5562,"children":5563},{"class":324,"line":1845},[5564],{"type":18,"tag":322,"props":5565,"children":5566},{},[5567],{"type":28,"value":846},{"type":18,"tag":322,"props":5569,"children":5570},{"class":324,"line":1854},[5571],{"type":18,"tag":322,"props":5572,"children":5573},{},[5574],{"type":28,"value":5575},"    def __init__(self, color):  \n",{"type":18,"tag":322,"props":5577,"children":5578},{"class":324,"line":1863},[5579],{"type":18,"tag":322,"props":5580,"children":5581},{},[5582],{"type":28,"value":5583},"        print(\"\\n我是人参果\")  \n",{"type":18,"tag":322,"props":5585,"children":5586},{"class":324,"line":1872},[5587],{"type":18,"tag":322,"props":5588,"children":5589},{},[5590],{"type":28,"value":5591},"        super().__init__(color)                         # 调用基类的__init__()方法  \n",{"type":18,"tag":322,"props":5593,"children":5594},{"class":324,"line":1881},[5595],{"type":18,"tag":322,"props":5596,"children":5597},{},[5598],{"type":28,"value":846},{"type":18,"tag":322,"props":5600,"children":5601},{"class":324,"line":1890},[5602],{"type":18,"tag":322,"props":5603,"children":5604},{},[5605],{"type":28,"value":5606},"    # 重写harvest()方法的代码  \n",{"type":18,"tag":322,"props":5608,"children":5609},{"class":324,"line":1899},[5610],{"type":18,"tag":322,"props":5611,"children":5612},{},[5613],{"type":28,"value":4728},{"type":18,"tag":322,"props":5615,"children":5616},{"class":324,"line":1907},[5617],{"type":18,"tag":322,"props":5618,"children":5619},{},[5620],{"type":28,"value":5621},"        print(\"人参果是：\" + color + \"的！\")              # 输出的是形式参数color  \n",{"type":18,"tag":322,"props":5623,"children":5624},{"class":324,"line":1916},[5625],{"type":18,"tag":322,"props":5626,"children":5627},{},[5628],{"type":28,"value":5629},"        print(\"人参果已经收获……\")  \n",{"type":18,"tag":322,"props":5631,"children":5632},{"class":324,"line":1924},[5633],{"type":18,"tag":322,"props":5634,"children":5635},{},[5636],{"type":28,"value":5637},"        print(\"人参果原来是：\" + Fruit.color + \"的！\")    # 输出的是类属性color  \n",{"type":18,"tag":322,"props":5639,"children":5640},{"class":324,"line":1933},[5641],{"type":18,"tag":322,"props":5642,"children":5643},{},[5644],{"type":28,"value":846},{"type":18,"tag":322,"props":5646,"children":5648},{"class":324,"line":5647},29,[5649],{"type":18,"tag":322,"props":5650,"children":5651},{},[5652],{"type":28,"value":4843},{"type":18,"tag":322,"props":5654,"children":5656},{"class":324,"line":5655},30,[5657],{"type":18,"tag":322,"props":5658,"children":5659},{},[5660],{"type":28,"value":5661},"apple.harvest(apple.color)                              # 调用harvest()方法  \n",{"type":18,"tag":322,"props":5663,"children":5665},{"class":324,"line":5664},31,[5666],{"type":18,"tag":322,"props":5667,"children":5668},{},[5669],{"type":28,"value":5670},"sapodilla = Sapodilla(\"白色\")                            # 创建类的实例（人参果）  \n",{"type":18,"tag":322,"props":5672,"children":5674},{"class":324,"line":5673},32,[5675],{"type":18,"tag":322,"props":5676,"children":5677},{},[5678],{"type":28,"value":5679},"sapodilla.harvest(\"金黄色带紫色条纹\")                     # 调用harvest()方法\n",{"type":18,"tag":31,"props":5681,"children":5682},{},[5683],{"type":28,"value":5684},"执行上面的代码，将显示如下图所示的运行结果：",{"type":18,"tag":31,"props":5686,"children":5687},{},[5688],{"type":18,"tag":63,"props":5689,"children":5692},{"alt":5690,"src":5691},"Pasted image 20250809220748","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250809220748.png",[],{"type":18,"tag":5694,"props":5695,"children":5696},"style",{},[5697],{"type":28,"value":5698},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":7,"searchDepth":334,"depth":334,"links":5700},[5701,5706,5710,5720,5724,5725,5729],{"id":25,"depth":334,"text":29,"children":5702},[5703,5704,5705],{"id":50,"depth":343,"text":53},{"id":120,"depth":343,"text":123},{"id":156,"depth":343,"text":159},{"id":282,"depth":334,"text":285,"children":5707},[5708,5709],{"id":293,"depth":343,"text":296},{"id":474,"depth":343,"text":477},{"id":628,"depth":334,"text":5711,"children":5712},"3 创建__init__()方法",[5713,5715,5717,5719],{"id":1092,"depth":343,"text":5714},"1. __init__：实例的 “构造初始化方法”",{"id":1298,"depth":343,"text":5716},"2. __enter__：上下文管理器的 “进入方法”",{"id":1553,"depth":343,"text":5718},"3. __exit__：上下文管理器的 “退出方法”",{"id":1942,"depth":343,"text":1942},{"id":2130,"depth":334,"text":2133,"children":5721},[5722,5723],{"id":2141,"depth":343,"text":2144},{"id":2517,"depth":343,"text":2520},{"id":3226,"depth":334,"text":3229},{"id":3684,"depth":334,"text":3687,"children":5726},[5727,5728],{"id":3708,"depth":343,"text":3711},{"id":4007,"depth":343,"text":4010},{"id":4463,"depth":334,"text":4466,"children":5730},[5731,5732,5733],{"id":4469,"depth":343,"text":4472},{"id":4909,"depth":343,"text":4912},{"id":5047,"depth":343,"text":5050},"markdown","content:blog:2025-07-13-python-面向对象编程.md","content","blog\u002F2025-07-13-python-面向对象编程.md","blog\u002F2025-07-13-python-面向对象编程","md",1780801018306]