public class Sword {
String name;
int damage;
}
public class Hero {
String name;
int hp;
Sword sword; //Sword information
public void attack() {
System.out.println(this.name + "I attacked!");
System.out.println("Inflicted 5 points of damage on the enemy!");
}
}
It is also possible to declare a class type variable in the field with Sword type
instead of int type and String type.
** has-a relationship **
Hero has-a Sword
Recommended Posts