Java创建一个LOL中的英雄类和怪物类,要求: 怪物类中加入生命值属、等级属性,生命值=等级*1000 英雄类中加入“经验值”,“等级”,“攻击力”等属性变量,加入“击打”方法,击打的目标是怪物,击打的结果是怪物的生命值-英雄攻击力
时间: 2023-05-14 12:06:45 浏览: 191
Java实现不同的类的属性之间相互赋值
5星 · 资源好评率100%
以下是 Java 创建一个 LOL 中的英雄类和怪物类的代码:
怪物类:
public class Monster {
private int health;
private int level;
public Monster(int level) {
this.level = level;
this.health = level * 1000;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}
英雄类:
public class Hero {
private int experience;
private int level;
private int attack;
public Hero(int experience, int level, int attack) {
this.experience = experience;
this.level = level;
this.attack = attack;
}
public int getExperience() {
return experience;
}
public void setExperience(int experience) {
this.experience = experience;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public void hit(Monster monster) {
int damage = this.attack;
monster.setHealth(monster.getHealth() - damage);
}
}
注意:以上代码仅供参考,可能存在错误或不完整的地方,需要根据实际情况进行修改和完善。
阅读全文