0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

java(クラス型をフィールドに用いる)

Posted at

has-aの関係

Swordクラスを定義

public class Sword {
  String name;
  int damage;
}

Heroクラスを定義

public class Hero {
  String name;
  int hp;
  Sword sword;                //剣の情報
  public void attack() {
    System.out.println(this.name + "攻撃した!");
    System.out.println("敵に5ポイントのダメージを与えた!");
  } 
}

int型、String型ではなく、Sword型をフィールドにクラス型の変数を宣言することもできる。
has-aの関係
Hero has-a Sword(勇者は剣を持っている)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?