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

1 / 2
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功