首页 > 其他分享 >CS_61A_ant

CS_61A_ant

时间:2023-04-14 15:45:09浏览次数:31  
标签:armor self bee ant bees CS 61A class def

 

    def reduce_armor(self, amount):
        """Reduce armor by AMOUNT, and remove the FireAnt from its place if it
        has no armor remaining.

        Make sure to damage each bee in the current place, and apply the bonus
        if the fire ant dies.
        """
        # BEGIN Problem 5
        "*** YOUR CODE HERE ***"
        def reflected_damage(amount):
            remaining_bees = []
            #for bee in self.place.bees:
                #if bee.armor > amount:
                    #remaining_bees.append(bee)# 好像这三行也不需要
            for bee in self.place.bees.copy():
                Insect.reduce_armor(bee,amount)
            #self.place.bees = remaining_bees 不需要
        reflected_damage(amount)
        print("DEBUG: remaining bees armor",[bee.armor for bee in self.place.bees])
        if self.armor <= amount:
            reflected_damage(self.damage)
            print("DEBUG: remaining bees armor",[bee.armor for bee in self.place.bees])
        Ant.reduce_armor(self,amount)

 

我的困难就是不知道如何使用Insect的reduce_armor,以下是Insect中,reduce_armor的定义,可以看到,有两个参数,其中的self 是自动引用。emm其实也就是一个属于Insect类的,有两个参数的函数,只不过他在这个类中代入的self。

以及,如何在改变list 内容的时候,继续进行迭代。他的方法是创建副本,[ self.place.bees.copy() ] 进行迭代,因为我只要用到里面的bee就行了,就相当于一个普通的list,我只需要里面的数字就行了。

 

【这个时候就是要注意到,bees是一个列表,但是里面存储的是bee这个对象,而不是一个数字,所以可以直接使用Insect.reduce_armor(bee,amount),最后再更改self.place.bees】

 

可以注意到,这个函数中没有使用remove_from这个函数,是在使用reduce_armor的时候自动调用,所以如果我继续在函数中使用reduce_armor,就会产生对象不存在的问题。

    def reduce_armor(self, amount):
        """Reduce armor by AMOUNT, and remove the insect from its place if it
        has no armor remaining.

        >>> test_insect = Insect(5)
        >>> test_insect.reduce_armor(2)
        >>> test_insect.armor
        3
        """
        self.armor -= amount
        if self.armor <= 0:
            self.place.remove_insect(self)
            self.death_callback()

 

 1 class A:
 2     def ping(self):
 3         print('ping',self)
 4         
 5 class B(A):
 6     def pong(self):
 7         print('pong',self)
 8     
 9 class C(A):
10     def pong(self):
11         print("PONG",self)
12 
13 class D(B,C):
14 
15     def ping(self):
16         super().ping()
17         print('post-ping:',self)
18 
19     def pngpong(self):
20         self.ping()
21         super().ping() # A.ping()
22         self.pong() # B.pong()
23         super().pong() # B.pong()
24         C.pong(self) # C.pong()

以上是多态的调用顺序,按照申明的顺序使用

进一步,关于class attribute and instance attribute,class attribute 的声明,是在类中定义的,而不是在函数内部定义。instance attribute 是在initiate function——[ def __init__(self, ...] 中定义的,class attribute是所有对象所共有的,而instance是单个对象独有的,与其他对象独立。

并且如果是class attribute ,在子类中也是class attribute,重载这个class attribute只需要在类中说明一下就行了,不需要在 initial function中说明,而如果是instance attribute,则子类必须要在initial function中声明,例子: armor and food_cost

class Ant(Insect):
    """An Ant occupies a place and does work for the colony."""

    implemented = False  # Only implemented Ant classes should be instantiated
    food_cost = 0
    # ADD CLASS ATTRIBUTES HERE
    blocks_path  = True

    def __init__(self, armor=1):
        """Create an Ant with an ARMOR quantity."""
        Insect.__init__(self, armor)

class WallAnt(Ant):
    name = "Wall"
    damage = 1
    food_cost = 4 #class  attribute 
    implemented = True
    def __init__(self, armor=4):
        Ant.__init__(self, armor) # armor is instance attribute

 

标签:armor,self,bee,ant,bees,CS,61A,class,def
From: https://www.cnblogs.com/xuenima/p/17317626.html

相关文章

  • 关于 css 伪元素 content属性值 为中文字符时出现乱码问题的处理
    更多关于csscontent属性的介绍点我 场景:需要在右箭头(点击该箭头是可以跳转到详情页)的左侧补充一个“更多”的文字描述 在一些场景下,使用CSS去做改动会是最优的,比如无源码等 易出现乱码的代码示例i.common-right-arrow-icon{position:relative;}i.comm......
  • 使用css在元素的前面或者后面添加内容
    :before在元素前面添加内容:after在元素的后面添加内容<!DOCTYPEhtml><html><style>h1:after{content:'前面的内容';}h1:before{content:'后面的内容';}</style><body><h1>***h1内容****</h1></body></html......
  • Unigraphics NX(UG NX)1957 安装包下载及(UG NX)1957 安装教程
    UG(UnigraphicsNX)是SiemensPLMSoftware公司出品的一个产品工程解决方案,它为用户的产品设计及加工过程提供了数字化造型和验证手段。UnigraphicsNX针对用户的虚拟产品设计和工艺设计的需求,以及满足各种工业化需求,提供了经过实践验证的解决方案。UG同时也是用户指南(userguide)和普......
  • Unigraphics NX(UG NX)1926 安装包下载及(UG NX)1926 安装教程
    UG(UnigraphicsNX)是SiemensPLMSoftware公司出品的一个产品工程解决方案,它为用户的产品设计及加工过程提供了数字化造型和验证手段。UnigraphicsNX针对用户的虚拟产品设计和工艺设计的需求,以及满足各种工业化需求,提供了经过实践验证的解决方案。UG同时也是用户指南(userguide)和普......
  • Unigraphics NX(UG NX)1899 安装包下载及(UG NX)1899 安装教程
    UG(UnigraphicsNX)是SiemensPLMSoftware公司出品的一个产品工程解决方案,它为用户的产品设计及加工过程提供了数字化造型和验证手段。UnigraphicsNX针对用户的虚拟产品设计和工艺设计的需求,以及满足各种工业化需求,提供了经过实践验证的解决方案。UG同时也是用户指南(userguide)和普......
  • csharp上传大型视频文件到服务器,解决方案
    ​前言一、SpringMVC简介1.1、SpringMVC引言为了使Spring有可插入的MVC架构,SpringFrameWork在Spring基础上开发SpringMVC框架,从而在使用Spring进行WEB开发时可以选择使用Spring的SpringMVC框架作为web开发的控制器框架。 spring知识图谱分享:1.2、SpringMVC......
  • laravel + node 在vagrant + ubuntu18.04 部署过程
    注意:当前的操作都是基于本地mac开发坏境配置操作,遇到了很多坑,这里借此做记录:一、ubuntu18.04上部署的php环境php7.4+msyql,常用的php扩展之类的可以自行的查询安装二、ubuntu18.04上部署nodejs14.17.6开始搜索直接用命令,装的node不是我们当前所需要的最后决定使......
  • [深入推导]CS231N assignment 2#4 _ 卷积神经网络 学习笔记 & 解析
    卷积神经网络基本算法实现卷积神经网络应该算是图像处理中绝对的主流了,关于算法得基本思想我在之前也学的比较懂了,这点如果不了解网上有很多教程.不过我并没有用代码亲自实现它.我们首先确定怎么编写.前面搞全连接网络总是会想着怎么去简化运算,现在我们接触了新的网络,......
  • CSS面试题
    一、4.14基础1.1、隐藏元素的方法有哪些●display:none:渲染树不会包含该渲染对象,因此该元素不会在页面中占据位置,也不会响应绑定的监听事件。●visibility:hidden:元素在页面中仍占据空间,但是不会响应绑定的监听事件。●opacity:0:将元素的透明度设置为0,以此来实现元素......
  • 关于 Angular 12 的 inlineCriticalCss 选项
    inlineCriticalCss是Angular项目中的一个配置选项,用于指定是否将关键CSS内联到页面HTML中。通常情况下,网页中的CSS文件是由浏览器异步加载的,这意味着在浏览器加载完HTML后还需要额外的时间来加载CSS文件,这会导致页面的首次渲染时间较长,用户体验不佳。为了解决这个问......