把这个Attack接口编写进你写的Java代码示例里
时间: 2024-03-23 21:39:10 浏览: 9
好的,下面是一个完整的Java代码示例,包含Zombie基类、不同种类的僵尸和不同的攻击方法:
```
public class Zombie {
protected int health; // 僵尸血量
protected int attack; // 僵尸攻击力
protected double speed; // 僵尸移动速度
protected Attack attackMethod; // 僵尸攻击方法
public Zombie(int health, int attack, double speed, Attack attackMethod) {
this.health = health;
this.attack = attack;
this.speed = speed;
this.attackMethod = attackMethod;
}
public void move() {
// 僵尸移动方法
}
public void attack() {
attackMethod.attack();
}
}
public class NormalZombie extends Zombie {
public NormalZombie() {
super(100, 10, 1, new BiteAttack());
}
}
public class BucketheadZombie extends Zombie {
public BucketheadZombie() {
super(200, 20, 0.5, new HeadAttack());
}
}
public interface Attack {
void attack();
}
public class BiteAttack implements Attack {
@Override
public void attack() {
// 僵尸咬人攻击
}
}
public class HeadAttack implements Attack {
@Override
public void attack() {
// 僵尸用头撞击攻击
}
}
```
使用时可以创建不同种类的僵尸,调用它们的攻击方法:
```
Zombie zombie1 = new NormalZombie();
Zombie zombie2 = new BucketheadZombie();
zombie1.attack();
zombie2.attack();
```
这样就可以实现增加不同种类的僵尸和攻击方法了。
阅读全文