《Python语言程序设计(美-梁勇)第7章习题解答(英文)(共2页).doc》由会员分享,可在线阅读,更多相关《Python语言程序设计(美-梁勇)第7章习题解答(英文)(共2页).doc(2页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上Chapter 7 Objects and Classes1.See the section Defining Classes for Objects.2.Define the initializer, create data fields, and define methods.3.Use a constructor4.The name of the initializer is _init_.5.The self refers to the object itself. Through self, the members of the object can be
2、accessed.6.The syntax for constructing an object is ClassName(arguments)The arguments of the constructor match the parameters in the _init_ method without self. The constructor first creates an object in the memory and then invokes the initializer.7.Initializer is a special method that is called whe
3、n creating an object.8. The object member access operator is the dot (.).9.You need to pass an argument in the constructor A() to invoke the class As initializer.10.(a) The constructor should be defined as _init_(self). (b) radius = 3 should be self.radius = 311.count is 100times is 012.count is 0n
4、is 113. _i is a private data field and cannot be accessed from outside of the class.14.Correct. The printout is Welcome.15. _on is a private data field and cannot be accessed outside the class.The way to fix it is to add a getter method for the Boolean property as follows:class A: def _init_(self, o
5、n): self._on = not on def isOn(self): return self._on def main(): a = A(False) print(a.isOn()main() # Call the main function16. Two benefits: (1) for protecting data and (2) for easy to maintain the class.In Python, private data fields are defined with two leading underscores.17. Add two underscores as the prefix for the method name.18. See the text专心-专注-专业