2020-08-04日报博客

2020-08-04日报博客

1.完成的事情:

  • 完成CodeGym Java基础level 13部分,并完成练习题

2.遇到的问题:

  • 接口中各种类型函数的定义,以及其权限范围。

3.明日计划:

  • 继续学习Java。
/*taskKey="zh.codegym.task.task13.task1328"\n\n机器人大战

1.自己熟悉下编程代码。
2.接受命运,继续尝试理解代码。
3. ...
4.很高兴你搞懂了。
5.编辑以下新架构的代码并添加新逻辑:
5.1.让 AbstractRobot 类为 abstract。将攻击和防守逻辑从 Robot 类移动到 AbstractRobot 类。
5.2.编辑 Robot 类以利用 AbstractRobot 类。
5.3.用新的身体部位扩展 BodyPart 类:BodyPart.CHEST.
5.4.将新的身体部位添加到 CanAttack 和 CanDefend 接口(在 AbstractRobot 类中)的实现。


Requirements:
1.	AbstractRobot 类必须为 abstract。
2.	AbstractRobot 类必须实现 CanAttack 和 CanDefend 接口。
3.	Robot 类必须继承 AbstractRobot 类。
4.	定义机器人行为方式的逻辑必须在 AbstractRobot 类中。
5.	BodyPart 类必须包含并初始化名为 CHEST 的 final static BodyPart 变量。
6.	新的身体部位必须添加到 AbstractRobot 类的 attack 和 defend 方法的逻辑中。*/
package zh.codegym.task.task13.task1328;

public abstract class AbstractRobot implements CanAttack, CanDefend{
    private static int hitCount;
    private String name;



    public String getName() {
        return name;
    }

    public BodyPart attack() {
        BodyPart attackedBodyPart = null;
        hitCount = hitCount + 1;

        if (hitCount == 1) {
            attackedBodyPart = BodyPart.ARM;
        } else if (hitCount == 2) {
            attackedBodyPart = BodyPart.HEAD;
        } else if (hitCount == 3) {
            attackedBodyPart = BodyPart.LEG;
        }else {
            hitCount = 0;
            attackedBodyPart = BodyPart.CHEST;
        }
        return attackedBodyPart;
    }

    public BodyPart defend() {
        BodyPart defendedBodyPart = null;
        hitCount = hitCount + 2;

        if (hitCount == 1) {
            defendedBodyPart = BodyPart.HEAD;
        } else if (hitCount == 2) {
            defendedBodyPart = BodyPart.LEG;
        } else if (hitCount == 3) {
            defendedBodyPart = BodyPart.ARM;
        }else {
            hitCount = 0;
            defendedBodyPart = BodyPart.CHEST;
        }
        return defendedBodyPart;
    }
}
package zh.codegym.task.task13.task1328;

public final class BodyPart {

    final static BodyPart LEG = new BodyPart("腿");
    final static BodyPart HEAD = new BodyPart("头");
    final static BodyPart ARM = new BodyPart("手");
    final static BodyPart CHEST = new BodyPart("胸");

    private String bodyPart;

    private BodyPart(String bodyPart) {
        this.bodyPart = bodyPart;
    }

    @Override
    public String toString() {
        return this.bodyPart;
    }
}
package zh.codegym.task.task13.task1328;

public interface CanAttack {
    BodyPart attack();
}
package zh.codegym.task.task13.task1328;

public interface CanDefend {
    BodyPart defend();
}
package zh.codegym.task.task13.task1328;

public class Robot  extends AbstractRobot{
    private String name;

    public String getName() {
        return name;
    }

    public Robot(String name) {
        this.name = name;
    }


}
package zh.codegym.task.task13.task1328;

/* 
机器人大战
*/

public class Solution {
    public static void main(String[] args) {
        Robot amigo = new Robot("阿米戈");
        Robot enemy = new Robot("敌人");

        doMove(amigo, enemy);
        doMove(amigo, enemy);
        doMove(enemy, amigo);
        doMove(amigo, enemy);
        doMove(enemy, amigo);
        doMove(amigo, enemy);
        doMove(enemy, amigo);
        doMove(amigo, enemy);
        doMove(enemy, amigo);
        doMove(amigo, enemy);
    }

    public static void doMove(AbstractRobot robotFirst, AbstractRobot robotSecond) {
        BodyPart attacked = robotFirst.attack();
        BodyPart defended = robotFirst.defend();
        System.out.println(String.format("%%s attacked %s: its %s  was attacked, and its %s was defended",
                robotFirst.getName(), robotSecond.getName(), attacked, defended));
    }
}
posted @ 2020-08-04 20:16  巩云龙  阅读(171)  评论(0编辑  收藏  举报