用java 定义两个类,一个叫Hero类,一个叫Boss类 Hero类的属性:血量、攻击力、名称,行为:开大、吃药 Boss类的属性:血量、攻击力、名称,行为:开大 完成以下步骤: 1 调用Hero类生成两个对象,分别叫【李白】【剑圣】 2 调用Boss类生成一个对象,叫【黑暗暴君】 3 李白调用吃药技能,输出李白的血量 4 李白对黑暗暴君开大,输出黑暗暴君的血量 5 李白对剑圣开大,输出剑圣的血量
时间: 2023-05-21 18:03:32 浏览: 244
java-面向对象的英雄类
以下是Java代码:
```
class Hero {
int hp;
int attack;
String name;
public Hero(int hp, int attack, String name) {
this.hp = hp;
this.attack = attack;
this.name = name;
}
public void eat() {
this.hp += 50;
System.out.println(this.name + "吃了一颗药,血量增加50,当前血量为:" + this.hp);
}
public void skill(Boss boss) {
boss.hp -= this.attack * 2;
System.out.println(this.name + "对" + boss.name + "使用了技能," + boss.name + "的血量减少" + this.attack * 2 + ",当前血量为:" + boss.hp);
}
}
class Boss {
int hp;
int attack;
String name;
public Boss(int hp, int attack, String name) {
this.hp = hp;
this.attack = attack;
this.name = name;
}
public void skill() {
System.out.println(this.name + "使用了大招!");
}
}
public class Main {
public static void main(String[] args) {
Hero liBai = new Hero(100, 20, "李白");
Hero jianSheng = new Hero(120, 18, "剑圣");
Boss darkLord = new Boss(500, 50, "黑暗暴君");
liBai.eat();
liBai.skill(darkLord);
liBai.skill(jianSheng);
System.out.println("黑暗暴君的血量:" + darkLord.hp);
System.out.println("剑圣的血量:" + jianSheng.hp);
}
}
```
输出结果:
```
李白吃了一颗药,血量增加50,当前血量为:150
李白对黑暗暴君使用了技能,黑暗暴君的血量减少40,当前血量为:460
李白对剑圣使用了技能,剑圣的血量减少40,当前血量为:80
黑暗暴君的血量:460
剑圣的血量:80
```
阅读全文