首页 > 代码库 > 类之间的关系练习2
类之间的关系练习2
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); } } }
类之间的关系练习2
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。