Quiz 15: Object-Oriented Programming | Python for Everybody 配套练习_解题记录


Python for Everybody


课程简介

Python for Everybody 零基础程序设计(Python 入门)

  • This course aims to teach everyone the basics of programming computers using Python. 本课程旨在向所有人传授使用 Python 进行计算机编程的基础知识。
  • We cover the basics of how one constructs a program from a series of simple instructions in Python. 我们介绍了如何通过 Python 中的一系列简单指令构建程序的基础知识。
  • The course has no pre-requisites and avoids all but the simplest mathematics. Anyone with moderate computer experience should be able to master the materials in this course. 该课程没有任何先决条件,除了最简单的数学之外,避免了所有内容。任何具有中等计算机经验的人都应该能够掌握本课程中的材料。
  • This course will cover Chapters 1-5 of the textbook “Python for Everybody”. Once a student completes this course, they will be ready to take more advanced programming courses. 本课程将涵盖《Python for Everyday》教科书的第 1-5 章。学生完成本课程后,他们将准备好学习更高级的编程课程。
  • This course covers Python 3.

在这里插入图片描述

coursera

Python for Everybody 零基础程序设计(Python 入门)

Charles Russell Severance
Clinical Professor

在这里插入图片描述

个人主页
Twitter

在这里插入图片描述

University of Michigan


课程资源

coursera原版课程视频
coursera原版视频-中英文精校字幕-B站
Dr. Chuck官方翻录版视频-机器翻译字幕-B站

PY4E-课程配套练习
Dr. Chuck Online - 系列课程开源官网



Quiz 15: Object-Oriented Programming

We do a quick look at how Python supports the Object-Oriented programming pattern.


单选题(1-11)

  1. Which came first, the instance or the class?
  • method
  • class
  • instance
  • function
  1. In Object Oriented Programming, what is another name for the attributes of an object?
  • portions
  • messages
  • fields
  • forms
  • methods
  1. Which of the following is NOT a good synonym for “class” in Python?
  • blueprint
  • template
  • pattern
  • direction
  1. What does this Python statement do if PartyAnimal is a class?
    zap = PartyAnimal()
  • Use the PartyAnimal template to make a new object and assign it to zap
  • Copy the value from the PartyAnimal variable to the variable zap
  • Subtract the value of the zap variable from the value in the PartyAnimal variable and put the difference in zap
  • Clear out all the data in the PartyAnimal variable and put the old values for the data in zap
  1. What is the syntax to look up the fullname attribute in an object stored in the variable colleen?
  • $colleen->fullname
  • c o l l e e n − > colleen-> colleen>fullname
  • $colleen::fullname
  • colleen.fullname
  • $colleen[“fullname”]
  1. Which of the following statements are used to indicate that class A will inherit all the features of class B?
  • class A(B):
  • A=B++;
  • class A extends B:
  • class A inherits B:
  • class A instanceOf B;
  1. How is “self” typically used in a Python method within a class?
  • The number of parameters to the method
  • To set the residual value in an expression where the method is used
  • To terminate a loop
  • To refer to the instance in which the method is being called
  1. What does the Python dir() function show when we pass an object into it as a parameter?
  • It shows the methods and attributes of an object
  • It shows the type of the object
  • It shows the parent class
  • It shows the number of parameters to the constructor
  1. Which of the following is rarely used in Object Oriented Programming?
  • Attribute
  • Inheritance
  • Destructor
  • Method
  • Constructor

Multiple instances

So far, we have defined a class, constructed a single object, used that object, and then thrown the object away. However, the real power in object-oriented programming happens when we construct multiple instances of our class.

When we construct multiple objects from our class, we might want to set up different initial values for each of the objects. We can pass data to the constructors to give each object a different initial value:

class PartyAnimal:
   x = 0
   name = ''
   def __init__(self, nam):
     self.name = nam
     print(self.name,'constructed')

   def party(self) :
     self.x = self.x + 1
     print(self.name,'party count',self.x)

s = PartyAnimal('Sally')
j = PartyAnimal('Jim')

s.party()
j.party()
s.party()

# Code: http://www.py4e.com/code3/party5.py

The constructor has both a self parameter that points to the object instance and additional parameters that are passed into the constructor as the object is constructed:

s = PartyAnimal('Sally')

Within the constructor, the second line copies the parameter (nam) that is passed into the name attribute within the object instance.

self.name = nam

The output of the program shows that each of the objects (s and j) contain their own independent copies of x and nam:

Sally constructed
Jim constructed
Sally party count 1
Jim party count 1
Sally party count 2

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰.封万里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值