public class Main {
public static void main(String[] args) {
Hero h1 = new Hero();
Hero h2 = new Hero();
Thief t1 = new Thief();
Wizard w1 = new Wizard();
Wizard w2 = new Wizard();
//Adventure begins!
//First stay at the inn
h1.hp += 50;
h2.hp += 50;
t1.hp += 50;
w1.hp += 50;
w2.hp += 50;
}
}
public class Main {
public static void main(String[] args) {
Character[] c = new Character[5];
c[0] = new Hero();
c[1] = new Hero();
c[2] = new Thief();
c[3] = new Wizard();
c[4] = new Wizard();
//Stay in an inn
for (Character ch : c) { //Take out one by one
ch.hp += 50; //Recovers 50 HP
}
}
}
public class Hero extends Character{
public void attack(Monster m) { //Attack all monsters
System.out.println(this.name + "Attack!");
System.out.println("10 points of damage to the enemy");
m.hp -= 10;
}
}
Attack everything if you inherit the monster class
public class Main {
public static void main(String[] args) {
Monster[] monsters = new Monster[3];
monsters[0] = new Slime();
monsters[1] = new Goblin();
monsters[2] = new DeathBat();
for (Monster m : monsters) {
m.run(); //Repeat the same instructions
}
}
}
Execution result Slime ran away Goblins ran away Hell bats escaped
Calling side, run away, run away, run away The called side slime ran away Goblins ran away Hell bats ran away
Recommended Posts