Wikipedia says:
Polymorphism describes the nature of the type system of a programming language, and that each element of the programming language (constants, variables, expressions, objects, functions, methods, etc.) belongs to multiple types. Refers to the property of forgiving. Also called polymorphism, polymorphism, polymorphism, diversity.
It's difficult to say that each element of a programming language belongs to multiple types.
Door dr = new Door(); // OK:The types on the left and right sides match (Note: Door is a self-made class)
Door dr2 = new String(); // NG:Compile error because the types on the left and right sides do not match
ClassA a = new ClassB(); // OK
//ClassA is a superclass and ClassB is a subclass
public class ClassB extends ClassA {
//abridgement
}
The variable players is declared as an "array of Player classes". An instance of another class (a subclass of the Player class) is assigned to the contents of the array.
This is polymorphism (common main routine). There is one way to call "players [X] .janken ()", but there are multiple methods to be called (Person's rock-paper-scissors, Robot's rock-paper-scissors).
MainClass.java
public class MainClass {
public static void main(String[] args) {
// (1)Generate a player to play rock-paper-scissors
Player[] players = new Player[3];
players[0] = new Person(); //Man
players[1] = new Robot(); //Robot A
players[2] = new Robot(); //Robot B
// (2)Let each player play rock-paper-scissors in turn
players[0].janken(); //Man
players[1].janken(); //Robot A
players[2].janken(); //Robot B
}
}
Player.java
public class Player {
private int win;
public void janken() {
//Rock-paper-scissors processing
}
public int getWin() {
return win;
}
}
Person.java
public class Person extends Player {
//abridgement
}
Robot.java
public class Robot extends Player {
//abridgement
}
In the future, even if the processing of the janken method changes, the caller does not have to change either (1) or (2). Also, even if the program is modified as shown below, the caller code only needs to be changed in (1).
Suppose there is a specification change in the rock-paper-scissors program. I added the code of "[Add]" to MainClass.java earlier.
MainClass.java
public class MainClass {
public static void main(String[] args) {
// (1)Generate a player to play rock-paper-scissors
Player[] players = new Player[3];
players[0] = new Person(); //Man
players[1] = new Robot(); //Robot A
players[2] = new Robot(); //Robot B
//[Addition] Added processing to register for Janken Tournament
Entry[] entry = new Entry[3];
entry[0] = new Entry(players[0]); //Man
entry[1] = new Entry(players[1]); //Robot A
entry[2] = new Entry(players[2]); //Robot B
// (2)Let each player play rock-paper-scissors in turn
players[0].janken(); //Man
players[1].janken(); //Robot A
players[2].janken(); //Robot B
}
}
Entry.java
public class Entry {
private int win;
//constructor
public Entry(Player player) {
//Get player information (history of wins, etc.) to register for the Janken Tournament
win = player.getWin();
}
}
If you want to learn more about object-oriented programming, I recommend you to read it. You can buy it on Amazon.
Recommended Posts