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

# -*- 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_name
    
    Python内部的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 = price
        self.isweekend = isweekend
        self.ischild = ischild
        
    def calc_price(self):
        if self.isweekend == 1:
            self.price *= 1.2
        if self.ischild == 1:
            self.price /= 2
    
    def 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 += 1
            else:
                self.direction = direction_list[1]
                self.pos_y -= 1
        elif self.direction == direction_list[1]:
            if self.pos_y != y_min:
                self.pos_y -= 1
            else:
                self.direction == direction_list[0]
                self.pos_y += 1
        elif self.direction == direction_list[2]:
            if self.pos_x != x_min:
                self.pos_x -= 1
            else:
                self.direction = direction_list[3]
                self.pos_x += 1
        elif self.direction == direction_list[3]:
            if self.pos_x != x_max:
                self.pos_x += 1
            else:
                self.direction = direction_list[2]
                self.pos_x -= 1


class tortoise():
    
    def __init__(self):
        self.pos_x = rd.randint(0,10)
        self.pos_y = rd.randint(0,10)
        self.hp    = 100
        self.direction = direction_list[rd.randint(0,3)]
    def  move(self):
        steps = rd.randint(1,2)
        self.hp -= 1
        if 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 += steps
                else:
                    self.direction = direction_list[1]
            elif self.pos_y == y_max:
                self.pos_y -= steps
                self.direction = direction_list[1]
                
        elif self.direction == direction_list[1]:
            if self.pos_y > y_min + 1:
                self.pos_y -= steps
            elif self.pos_y == y_min + 1:
                if steps == 1:
                    self.pos_y -= steps
                else:
                    self.direction = direction_list[0]
            elif self.pos_y == y_min:
                self.pos_y += steps
                self.direction = direction_list[0]
        elif self.direction == direction_list[2]:
            if self.pos_x > x_min + 1:
                self.pos_x -= steps
            elif self.pos_x == x_min+1:
                if steps == 1:
                    self.pos_x -= steps
                else:
                    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 += steps
            elif 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 False
            self.fishs = list(filter(func,self.fishs))
            
            if self.tortoise.hp == 0 or len(self.fishs) == 0:
                break
            print("self.tortoise.hp = %d" % self.tortoise.hp )
            print("len(self.fishc) = %d" % len(self.fishs))
            
        
        
        
        
game = Game()
game.run()

  

posted @ 2019-03-12 00:44  Alimy  阅读(693)  评论(0编辑  收藏  举报