Java类的继承-成员与方法 ,代码中Hero继承Actor类,请完善代码使其能够输出样例所示内容。class Actor { protected int health; protected int damage; public int getHealth() { return health; } public void setHealth(int health) { this.health = health; } public int getDamage() { return damage; } public void setDamage(int damage) { this.damage = damage; } public void showStatus() { System.out.println("health:" + health); System.out.println("damage:" + damage); } } class Hero extends Actor { /* -------------请完善此部分代码-------------- */ public String getName() { return name; } public void setName(String name) { this.name = name; } public void showStatus() { /* -------------请完善此部分代码-------------- */ } } public class Main { public static void main(String[] args) { /* -------------请完善此部分代码-------------- */ } } 输入格式: 无 输出格式: health:100 damage:10 health:200 damage:20 name:Hero 输入样例: 在这里给出一组输入。例如: 输出样例: 在这里给出相应的输出。例如: health:100 damage:10 health:200 damage:20 name:Hero
时间: 2023-06-04 20:02:48 浏览: 1285
class Actor {
protected int health;
protected int damage;
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getDamage() {
return damage;
}
public void setDamage(int damage) {
this.damage = damage;
}
public void showStatus() {
System.out.println("health:" + health);
System.out.println("damage:" + damage);
}
}
class Hero extends Actor {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void showStatus() {
super.showStatus();
System.out.println("name:" + name);
}
}
public class Main {
public static void main(String[] args) {
Hero hero = new Hero();
hero.setHealth(100);
hero.setDamage(10);
hero.setName("Hero");
hero.showStatus();
Hero hero2 = new Hero();
hero2.setHealth(200);
hero2.setDamage(20);
hero2.setName("Hero2");
hero2.showStatus();
}
}
// 输出结果:
// health:100 damage:10 name:Hero
// health:200 damage:20 name:Hero2
阅读全文