您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > Python语言程序设计(美-梁勇)第7章习题解答(英文)
Chapter7ObjectsandClasses1.SeethesectionDefiningClassesforObjects.2.Definetheinitializer,createdatafields,anddefinemethods.3.Useaconstructor4.Thenameoftheinitializeris__init__.5.Theselfreferstotheobjectitself.Throughself,themembersoftheobjectcanbeaccessed.6.ThesyntaxforconstructinganobjectisClassName(arguments)Theargumentsoftheconstructormatchtheparametersinthe__init__methodwithoutself.Theconstructorfirstcreatesanobjectinthememoryandtheninvokestheinitializer.7.Initializerisaspecialmethodthatiscalledwhencreatinganobject.8.Theobjectmemberaccessoperatoristhedot(.).9.YouneedtopassanargumentintheconstructorA()toinvoketheclassA’sinitializer.10.(a)Theconstructorshouldbedefinedas__init__(self).(b)radius=3shouldbeself.radius=311.countis100timesis012.countis0nis113.__iisaprivatedatafieldandcannotbeaccessedfromoutsideoftheclass.14.Correct.TheprintoutisWelcome.15.__onisaprivatedatafieldandcannotbeaccessedoutsidetheclass.ThewaytofixitistoaddagettermethodfortheBooleanpropertyasfollows:classA:def__init__(self,on):self.__on=notondefisOn(self):returnself.__ondefmain():a=A(False)print(a.isOn())main()#Callthemainfunction16.Twobenefits:(1)forprotectingdataand(2)foreasytomaintaintheclass.InPython,privatedatafieldsaredefinedwithtwoleadingunderscores.17.Addtwounderscoresastheprefixforthemethodname.18.Seethetext
本文标题:Python语言程序设计(美-梁勇)第7章习题解答(英文)
链接地址:https://www.777doc.com/doc-5006512 .html