# -*- coding: utf-8 -*-
"""
Created on Mon Mar 11 20:09:54 2019@author: Administrator
""""""测试题:
0. 以下代码体现了面向对象编程的什么特征?>>> "FishC.com".count('o')
1
>>> [1, 1, 2, 3, 5, 8].count(1)
2
>>> (0, 2, 4, 8, 12, 18).count(1)
0多态1. 当程序员不想把同一段代码写几次,他们发明了函数解决了这种情况。当程序员已经有了一个类,而又想建立一个非常相近的新类,他们会怎么做呢?继承---重写2. self参数的作用是什么?指向当前对象3. 如果我们不希望对象的属性或方法被外部直接引用,我们可以怎么做?__func_name__member_namePython内部的name magling机制会将在头部带两个下划线的方法名或属性名改名,以实现对外隐蔽的效果4. 类在实例化后哪个方法会被自动调用?第一个是 __new__    申请内存第二个是 __init__   构造函数5. 请解释下边代码错误的原因:
class MyClass:name = 'FishC'def myFun(self):print("Hello FishC!")>>> MyClass.name
'FishC'
>>> MyClass.myFun()
Traceback (most recent call last):File "<pyshell#6>", line 1, in <module>MyClass.myFun()
TypeError: myFun() missing 1 required positional argument: 'self'未实例化对象,self还未分配空间
>>>""""""
动动手:
0. 按照以下要求定义一个游乐园门票的类,并尝试计算2个成人+1个小孩平日票价。
"""class TickitPrice():def __init__(self,price=100,isweekend = 0,ischild = 0):self.price = priceself.isweekend = isweekendself.ischild = ischilddef calc_price(self):if self.isweekend == 1:self.price *= 1.2if self.ischild == 1:self.price /= 2def get_price(self):self.calc_price()return self.price#parent1 = TickitPrice(100,0,0)
#parent2 = TickitPrice(100,0,0)
#child   = TickitPrice(100,0,1)
#print(parent1.get_price() + parent2.get_price() + child.get_price())"""
1. 游戏编程:按以下要求定义一个乌龟类和鱼类并尝试编写游戏。(初学者不一定可以完整实现,但请务必先自己动手,你会从中学习到很多知识的^_^)
假设游戏场景为范围(x, y)为0<=x<=10,0<=y<=10
游戏生成1只乌龟和10条鱼
它们的移动方向均随机
乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),鱼儿的最大移动能力是1
当移动到场景边缘,自动向反方向移动
乌龟初始化体力为100(上限)
乌龟每移动一次,体力消耗1
当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
鱼暂不计算体力
当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束
""""""
这个程序没有实现每次移动全随机
"""import random as rd
direction_list = [ 'up' , 'down' , 'left' , 'right']
x_min = 0
x_max = 10
y_min = 0
y_max = 10
class fish():def __init__(self):self.pos_x = rd.randint(0,10)self.pos_y = rd.randint(0,10)self.direction = direction_list[rd.randint(0,3)]def move(self):if self.direction == direction_list[0]:if self.pos_y != y_max:self.pos_y += 1else:self.direction = direction_list[1]self.pos_y -= 1elif self.direction == direction_list[1]:if self.pos_y != y_min:self.pos_y -= 1else:self.direction == direction_list[0]self.pos_y += 1elif self.direction == direction_list[2]:if self.pos_x != x_min:self.pos_x -= 1else:self.direction = direction_list[3]self.pos_x += 1elif self.direction == direction_list[3]:if self.pos_x != x_max:self.pos_x += 1else:self.direction = direction_list[2]self.pos_x -= 1class tortoise():def __init__(self):self.pos_x = rd.randint(0,10)self.pos_y = rd.randint(0,10)self.hp    = 100self.direction = direction_list[rd.randint(0,3)]def  move(self):steps = rd.randint(1,2)self.hp -= 1if self.direction == direction_list[0]:if self.pos_y < y_max - 1:self.pos_y += steps;elif self.pos_y == y_max - 1:if steps == 1:self.pos_y += stepselse:self.direction = direction_list[1]elif self.pos_y == y_max:self.pos_y -= stepsself.direction = direction_list[1]elif self.direction == direction_list[1]:if self.pos_y > y_min + 1:self.pos_y -= stepselif self.pos_y == y_min + 1:if steps == 1:self.pos_y -= stepselse:self.direction = direction_list[0]elif self.pos_y == y_min:self.pos_y += stepsself.direction = direction_list[0]elif self.direction == direction_list[2]:if self.pos_x > x_min + 1:self.pos_x -= stepselif self.pos_x == x_min+1:if steps == 1:self.pos_x -= stepselse:self.direction = direction_list[3]elif self.pos_x == x_min:self.pos_x += steps;self.direction = direction_list[3]elif self.direction == direction_list[3]:if self.pos_x < x_max - 1:self.pos_x += stepselif self.pos_x == x_max - 1:if steps == 1:self.pos_x += steps;else:self.direction = direction_list[2]elif self.pos_x == x_max:self.pos_x -= steps;self.direction = direction_list[2]class Game():def __init__(self):self.fishs = [ fish() for i in range(10) ]self.tortoise = tortoise()def run(self):while True:for each in self.fishs:each.move()self.tortoise.move()func = lambda temp_fish,pos_x = self.tortoise.pos_x,pos_y=self.tortoise.pos_y : True if temp_fish.pos_x != pos_x or temp_fish.pos_y != pos_y else Falseself.fishs = list(filter(func,self.fishs))if self.tortoise.hp == 0 or len(self.fishs) == 0:breakprint("self.tortoise.hp = %d" % self.tortoise.hp )print("len(self.fishc) = %d" % len(self.fishs))game = Game()
game.run()

  

转载于:https://www.cnblogs.com/alimy/p/10514201.html

[Python]小甲鱼Python视频第037课(类和对象:面向对象编程 )课后题及参考解答相关推荐

  1. [Python]小甲鱼Python视频第003课(插曲之变量和字符串)课后题及参考解答

    # -*- coding: utf-8 -*- """ Created on Mon Mar 4 22:09:32 2019@author: fengs "&q ...

  2. 小甲鱼python课后题007_[Python]小甲鱼Python视频第007-008课(了不起的分支和循环)课后题及参考解答...

    # -*- coding: utf-8 -*- """ Created on Mon Mar 4 23:35:19 2019 @author: fengs "& ...

  3. [Python]小甲鱼Python视频第048课(魔法方法:迭代器) )课后题及参考解答

    # -*- coding: utf-8 -*- """ Created on Sun Mar 24 20:24:02 2019@author: fengs "& ...

  4. python小课文件_[Python]小甲鱼Python视频第030课(文件系统:介绍一个高大上的东西)课后题及参考解答...

    # -*- coding: utf-8 -*- """ Created on Fri Mar 8 15:49:32 2019 @author: Administrator ...

  5. [Python]小甲鱼Python视频第002课(第一个游戏)课后题及参考解答

    # -*- coding: utf-8 -*- """ Created on Mon Mar 4 11:19:54 2019@author: Administrator ...

  6. [Python]小甲鱼Python视频第019课(函数:我的地盘听我的)课后题及参考解答

    # -*- coding: utf-8 -*- """ Created on Thu Mar 7 16:41:50 2019@author: Administrator ...

  7. 小甲鱼python猜题_[Python]小甲鱼Python视频第033课(except)课后题及参考解答

    # -*- coding: utf-8 -*- """ Spyder Editor This is a temporary script file. "&quo ...

  8. python 小甲鱼 好不好_[Python]小甲鱼Python视频第025课(字典:当索引不好用时)课后题及参考解答...

    # -*- coding: utf-8 -*- """ Created on Fri Mar 8 10:04:08 2019 @author: Administrator ...

  9. python小课文件_[Python]小甲鱼Python视频第028课(文件:因为懂你,所以永恒)课后题及参考解8...

    # -*- coding: utf-8 -*- """ Created on Fri Mar 8 11:52:02 2019 @author: Administrator ...

最新文章

  1. 树莓派安装docker
  2. (学习笔记)Jupyter notebook入门
  3. 杭电1232 畅通工程
  4. 【2012百度之星/初赛上】B:小小度刷礼品
  5. Inconsistent behavior between text type in Webclient UI and backend customizing
  6. Kubernetes之Pod调度
  7. python入门——P43魔法方法:算数运算2
  8. 玩转Hook——Android权限管理功能探讨(一)
  9. 机器学习中的算法——决策树模型组合之随机森林与GBDT
  10. multisim连接MySQL_Multisim14使用multisim12元件库的方法
  11. TP Link 路由器 设置
  12. 计算机突然断电或死机 重启后,电脑重启死机故障排除
  13. 输入今天日期输出明天日期
  14. linux 服务器中文乱码问题解决
  15. STM32 ES8266上阿里云IOT MQTT实践【第一章】:物联网简介(什么是物联网)
  16. python处理脱敏问题
  17. matlab画图nan,在Matlab中过滤包含NaN的图像?
  18. 新发现的Web服务-----免费服务
  19. deepfashion(deepfashion安卓下载)
  20. EXCEL VBA小白第一课:入门

热门文章

  1. python分割压缩_Python读取分割压缩TXT文本文件实例
  2. 城轨的两类时钟系统均同步于_推介中央电视台4K IP化移动外场系统搭建中解决的主要问题...
  3. linux 自动挂载usb设备,Raspberry Pi 自动挂载USB存储设备
  4. Python爬虫编程实践 Task04
  5. 2019斯坦福CS224n深度学习自然语言处理笔记(3)反向传播与计算图
  6. SQL数据旋转的问题
  7. Android layout优化
  8. 平台的本质——保险公司互联网平台建设系列
  9. Vultr 修改 Root 密码
  10. 【跃迁之路】【590天】程序员高效学习方法论探索系列(实验阶段347-2018.09.18)...