用java 定义两个类,一个叫Hero类,一个叫Boss类 Hero类的属性:血量、攻击力、名称,行为:开大、吃药 Boss类的属性:血量、攻击力、名称,行为:开大 完成以下步骤: 1 调用Hero类生成两个对象,分别叫【李白】【剑圣】 2 调用Boss类生成一个对象,叫【黑暗暴君】 3 李白调用吃药技能,输出李白的血量 4 李白对黑暗暴君开大,输出黑暗暴君的血量 5 李白对剑圣开大,输出剑圣的血量
时间: 2023-05-21 21:03:46 浏览: 119
JAVA类和对象
以下是Java代码:
```
public class Hero {
private int hp;
private int attack;
private String name;
public Hero(int hp, int attack, String name) {
this.hp = hp;
this.attack = attack;
this.name = name;
}
public void eatMedicine() {
this.hp += 50;
System.out.println(this.name + "吃了一颗药,血量增加50,当前血量为:" + this.hp);
}
public void useSkill(Boss boss) {
boss.beAttacked(this.attack);
System.out.println(this.name + "对" + boss.getName() + "使用了技能," + boss.getName() + "的血量为:" + boss.getHp());
}
public int getHp() {
return hp;
}
public int getAttack() {
return attack;
}
public String getName() {
return name;
}
}
public class Boss {
private int hp;
private int attack;
private String name;
public Boss(int hp, int attack, String name) {
this.hp = hp;
this.attack = attack;
this.name = name;
}
public void useSkill() {
System.out.println(this.name + "使用了技能!");
}
public void beAttacked(int attack) {
this.hp -= attack;
}
public int getHp() {
return hp;
}
public int getAttack() {
return attack;
}
public String getName() {
return name;
}
}
public class Main {
public static void main(String[] args) {
Hero liBai = new Hero(500, 100, "李白");
Hero jianSheng = new Hero(600, 80, "剑圣");
Boss darkLord = new Boss(1000, 120, "黑暗暴君");
liBai.eatMedicine();
liBai.useSkill(darkLord);
liBai.useSkill(jianSheng);
}
}
```
输出结果:
```
李白吃了一颗药,血量增加50,当前血量为:550
李白对黑暗暴君使用了技能,黑暗暴君的血量为:900
李白对剑圣使用了技能,剑圣的血量为:520
```
阅读全文