Hero.java
public class Hero {
String name = "Brave";
int hp = 100;
//fight
public void attack(Matango m) {
System.out.println(this.name + "attack!");
m.hp -= 5;
System.out.println("Inflicted 5 points of damage!");
}
//escape
public void run() {
System.out.println(this.name + "Escaped!");
}
}
SuperHero.java
public class SuperHero {
String name = "Brave";
int hp = 100;
boolean flying;
//fight
public void attack(Matango m) {
System.out.println(this.name + "attack!");
m.hp -= 5;
System.out.println("Inflicted 5 points of damage!");
}
//escape
public void run() {
System.out.println(this.name + "Escaped!");
}
//jump
public void fly() {
this.flying = true;
System.out.println("I jumped up!");
}
//Land
public void land() {
this.flying = false;
System.out.println("Landed!");
}
}
Fight, run away, but overlap. If you change the Hero class, you must also change the SuperHero class.
SuperHero.java
public class SuperHero extends Hero{
boolean flying;
//jump
public void fly() {
this.flying = true;
System.out.println("I jumped up!");
}
//Land
public void land() {
this.flying = false;
System.out.println("Landed!");
}
}
ʻExtend Inherit the Hero class with the original class name`.
Recommended Posts