Java怪物类中加入生命值属、等级属性,生命值=等级*1000 英雄类中加入“经验值”,“等级”,“攻击力”等属性变量,加入“击打”方法,击打的目标是怪物,击打的结果是怪物的生命值-英雄攻击力 点击复制后,将打开C知道体验页 以下是 Java 创建一个 LOL 中的英雄类和怪物类的代码:
时间: 2023-05-14 07:07:21 浏览: 223
英雄类代码:
public class Hero {
private int exp;
private int level;
private int attack;
public Hero() {
this.exp = 0;
this.level = 1;
this.attack = 100;
}
public void hit(Monster monster) {
int damage = this.attack;
monster.takeDamage(damage);
}
public void gainExp(int exp) {
this.exp += exp;
if (this.exp >= 100) {
this.levelUp();
}
}
private void levelUp() {
this.level++;
this.attack += 50;
this.exp -= 100;
}
}
怪物类代码:
public class Monster {
private int level;
private int hp;
public Monster(int level) {
this.level = level;
this.hp = level * 1000;
}
public void takeDamage(int damage) {
this.hp -= damage;
if (this.hp <= 0) {
this.die();
}
}
private void die() {
System.out.println("Monster died!");
}
}
阅读全文