Javaでは、メソッドのオーバーロードを使うと、仮引数リストが異なれば、1つのクラスに同じ名前のメソッドを複数持たせることができます。これはコンパイル時のポリモーフィズム(静的ポリモーフィズム)に関係する仕組みです。似た処理を、異なる入力で行うメソッドを作るときに欠かせない考え方です。
メソッドをオーバーロードすると、プログラムの可読性が高まります。仮引数が異なれば、別のメソッドに同じ名前を付けることができます。仮引数の数や型に応じて、対応するメソッドが呼び出されます。
メソッドのオーバーロードでカギになるのは、メソッドのシグネチャーです。シグネチャーが異なる2つのメソッドは、別のメソッドとみなされます。メソッドをオーバーロードする方法は2つあります。
仮引数の数が違う場合: メソッドには、同じ名前で仮引数の数だけが違うものを用意できます。
public class Display {
public void show(int x) {
System.out.println("Show with int: " + x);
}
public void show(int x, int y) {
System.out.println("Show with two ints: " + x + ", " + y);
}
}
仮引数の型が違う場合: メソッドには、名前も仮引数の数も同じで、型だけが違うものを用意できます。
public class Display {
public void show(int x) {
System.out.println("Show with int: " + x);
}
public void show(String s) {
System.out.println("Show with String: " + s);
}
}
このコンセプトでは、Javaにおけるメソッドのオーバーロードのさまざまな例や細かなポイントを見ていきます。
この演習では、親友と一緒に「ウィザードとウォリアーズ」というロールプレイングゲームを遊びます。演じるのはゲームマスターで、プレイヤーにとってゲームの世界を生き生きとしたものにするのが役割です。そのために欠かせないのが、プレイヤーにゲームを説明することです。キャラクターの状態はどうなっているのか、訪れている町はどんな様子なのか、といったことです。
ゲームの一部をプレイヤーに説明する5つのタスクに取り組みます。
各キャラクターにはクラス、レベル、ヒットポイントがあり、次のように説明されます:"You're a level <LEVEL> <CLASS> with <HIT_POINTS> hit points."
Characterを唯一の入力として受け取り、その説明を返すGameMaster.describeメソッドを実装しましょう。
Character character = new Character();
character.setCharacterClass("Wizard");
character.setLevel(4);
character.setHitPoints(28);
new GameMaster().describe(character);
// => "You're a level 4 Wizard with 28 hit points."
各目的地には名前と住民の数があり、次のように説明されます:"You've arrived at <NAME>, which has <INHABITANTS> inhabitants."
Destinationを唯一の入力として受け取り、その説明を返すGameMaster.describeメソッドを実装しましょう。
Destination destination = new Destination();
destination.setName("Muros");
destination.setInhabitants(732);
new GameMaster().describe(destination);
// => "You've arrived at Muros, which has 732 inhabitants."
キャラクターは、次の2つの方法のいずれかで目的地へ移動できます:
"You're traveling to your destination by walking."
"You're traveling to your destination on horseback."
TravelMethodを唯一の入力として受け取り、その説明を返すGameMaster.describeメソッドを実装しましょう。
new GameMaster().describe(TravelMethod.HORSEBACK);
// => "You're traveling to your destination on horseback."
キャラクターが目的地へ移動しているときは、個々の説明を組み合わせて次のように説明されます:"<CHARACTER> <TRAVEL_METHOD> <DESTINATION>"
Character、Destination、TravelMethodを入力として受け取り、その説明を返すGameMaster.describeメソッドを実装しましょう。
Character character = new Character();
character.setCharacterClass("Wizard");
character.setLevel(4);
character.setHitPoints(28);
Destination destination = new Destination();
destination.setName("Muros");
destination.setInhabitants(732);
new GameMaster().describe(character, destination, TravelMethod.HORSEBACK);
// => "You're a level 4 Wizard with 28 hit points. You're traveling to your destination on horseback. You've arrived at Muros, which has 732 inhabitants."
ほとんどの場合、キャラクターは徒歩で目的地へ移動します。利便性のため、プレイヤーは移動方法の指定を省略できます。その場合、移動方法は徒歩とみなされます。CharacterとDestinationを入力として受け取り、その説明を返すGameMaster.describeメソッドを実装しましょう。
Character character = new Character();
character.setCharacterClass("Wizard");
character.setLevel(4);
character.setHitPoints(28);
Destination destination = new Destination();
destination.setName("Muros");
destination.setInhabitants(732);
new GameMaster().describe(character, destination);
// => "You're a level 4 Wizard with 28 hit points. You're traveling to your destination by walking. You've arrived at Muros, which has 732 inhabitants."