中级实训 Part4 问题回答

中级实训 Part4 问题回答

    • The Critter Class
    • Default Critter Behavior
    • Another Critter

The Critter Class

Set 7

  • 1.What methods are implemented in Critter?
    回答: 有act, getActors, processActors, getMoveLocations, selectMoveLocation, makeMove六种方法

  • 2.What are the five basic actions common to all critters when they act?
    回答: getActors, processActors, getMoveLocations, selectMoveLocation, makeMove五种基本动作

  • 3.Should subclasses of Critter override the getActors method? Explain.
    回答: 需要,如果在子类中需要使用getActor方法来得到不同位置上的对象,那就需要重载这一函数来实现,因为在Critter类中该方法的位置是确定的。

  • 4.Describe the way that a critter could process actors.
    回答: 它可以清空列表中的所有对象,也可以控制不同的对象有不同的行为(例如选择一只虫子吃掉,其余的虫子就会开始“逃跑”),也可以让所有的虫子都变色或者移动等等。

  • 5.What three methods must be invoked to make a critter move? Explain each of these methods.
    回答: 共有以下三个方法:getMoveLocations, selectMoveLocation, makeMove(源码来自Crriter.java)

    • getMoveLocations:调用此方法,返回crriter周围所有空位置的列表
    • selectMoveLocation: 从列表中随机选择一个位置,作为将要移动到的位置
    • makeMove: 移动到想要去的位置,如果该位置为空,移除该小动物;如果位置合法,调用moveTo方法移动
  • 6.Why is there no Critter constructor?
    回答: 因为Critter 扩展了 Actor类,在Actor类中有默认构造函数。所以就算在Critter中没有构造函数,Java也会自动构造一个默认构造函数,它会去调用super(),后者又调用Actor默认构造函数,最终创建一个蓝色的、面向北方的小动物。

Default Critter Behavior

set8

  • 1.Why does act cause a ChameleonCritter to act differently from a Critter even though ChameleonCritter does not override act?
    回答: 因为act方法调用getActors,processActors,getMoveLocations,selectMoveLocation和makeMove。 在ChameleonCritter类中重写了processActors和makeMove方法。 因此,调用ChameleonCritter行为将与调用Critter行为产生不同的行为。
    这个不同具体表现在:

    • Critter 通过移除不是岩石或小动物的任何邻居来处理actor;但是ChameleonCritter通过随机选择其邻居之一,获取邻居的颜色,然后将其自己的颜色更改为邻居的颜色来处理actor。
    • Critter在调用makeMove方法进行移动时,不会改变自己的方向;但是ChameleonCritter会先面向下一个位置,然后再移动。
  • 2.Why does the makeMove method of ChameleonCritter call super.makeMove?
    回答: 在ChameleonCritter 中重写makeMove 方法时,首先需要修改方向,然后再执行与父类Critter 中相通的makeMove 方法,所以这里需要用到super调用父类中的makeMove 方法,如果不采用super调用的话,就会出现子类在makeMove 中死循环的现象。

  • 3.How would you make the ChameleonCritter drop flowers in its old location when it moves?
    回答: 需要修改makeMove 方法。具体思路是用一个变量记住当前小动物所在的位置,然后再小动物移动之后,在这个位置上放一朵花。
    修改后的方法如下:

  • 4.Why doesn’t ChameleonCritter override the getActors method?
    回答: 因为ChameleonCritter 所需要的getActors 方法与Critter 是一样的,都是返回邻居位置的列表,而ChameleonCritter 本身就算扩展了Critter ,可以使用Critter 中的getActors 方法,就不需要重载了。

  • 5.Which class contains the getLocation method?
    回答: Actor类中实现了getLocation 方法,所以它的所有子类都继承了这个方法。

  • 6.How can a Critter access its own grid?
    回答: 调用从Actor继承来的getGrid方法。如上面重新实现makeMove方法时调用一致。

Another Critter

Set 9

  • 1.Why doesn’t CrabCritter override the processActors method?
    回答: 因为CrabCritter 处理actor的方法是把除了石头和小动物以外的对象都“吃掉”,也是就是从grid中移除,这与Crriter类是一致的,所以不需要重载。

  • 2.Describe the process a CrabCritter uses to find and eat other actors. Does it always eat all neighboring actors? Explain.
    回答: CrabCritter 会调用getActors方法查找螃蟹小动物前面,右前和左前位置的邻居。 当调用processActors方法时,在这些位置中发现的所有邻居都会被“吃掉”。 其他相邻位置的actor不会受到影响

  • 3.Why is the getLocationsInDirections method used in CrabCritter?
    回答: 对于CrabCritter,getLocationsInDirections 方法引入一个方向数组作为参数,此数组包含该螃蟹可以吃的可能邻居的方向。 方法getLocationsInDirections使用此数组来确定并返回由数组参数指定的方向的此生物有效的相邻位置。

  • 4.If a CrabCritter has location (3, 4) and faces south, what are the possible locations for actors that are returned by a call to the getActors method?
    回答: (4,4),(4,3),(4,5)

  • 5.What are the similarities and differences between the movements of a CrabCritter and a Critter?
    回答:

    • 相似:CrabCritter 和 Critter 在移动的时候都不会改变自身方向;二者都是从位置列表中随机选择一个位置来进行移动。
    • 区别: CrabCritter 只能向左或者向右移动,而Critter 可以在8个邻居方向中任意选择移动方向; CrabCritter 在不能移动时,会转动,但是Critter 不会有其他操作。
  • 6.How does a CrabCritter determine when it turns instead of moving?
    回答: 它会将当前位置与makeMove 参数中的位置(即目标位置)比较,如果相同,代表不能移动,需要转动。

  • 7.Why don’t the CrabCritter objects eat each other?
    回答: CrabCritter 继承了Critter 中的 processActors方法,在这个方法中Critter不会吃掉石头和其他小动物,当然,CrabCritter也不会吃掉石头和其他小动物

本文地址:https://blog.csdn.net/weixin_46092070/article/details/109146912

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐