package lianxi2; public abstract class Weapon { public abstract String getName(); public abstract int getPower(); }
package lianxi2; public class Knife extends Weapon { @Override public String getName() { return "大刀"; } @Override public int getPower() { // TODO Auto-generated method stub return 10; } }
package lianxi2; public class Bow extends Weapon { public String getName() { return "后裔强弓"; } public int getPower() { return 20; } }
package lianxi2; public class Hero { Weapon weapon; String name; public Hero(Weapon weapon, String name) { super(); this.weapon = weapon; this.name = name; } public void attack(Enemy e) { e.hurt(weapon.getPower()); System.out.println(this.name+"使用"+weapon.getName()+"攻击"+e.name); e.showInfo(); } public Weapon getWeapon() { return weapon; } public void setWeapon(Weapon weapon) { this.weapon = weapon; } }
package lianxi2; public class Enemy { int power=100; String name; public Enemy(int power, String name) { super(); this.power = power; this.name = name; } public void hurt(int blood){ this.power=this.power-blood; } public void showInfo(){ if(this.power<=0){ System.out.println(this.name+"over"); }else { System.out.println(this.name+"的血量"+this.power); } } }
时间: 2024-10-14 19:13:54