static void Main(string[] args) { // We create a GameCharater Array (base class) and assign child class objects to it. // This is an example of polymorphism. var players = new GameCharacter[] { new Warrior("Utred", 7, 2, "Sword"), new Warrior("Ragnar", 5, 4, "Axe"), new Wizard("Flora", 2, 7, 8, 1), new Wizard("Fauna", 2, 8, 7, 2), new Wizard("Merryweather", 2, 8, 7, 3) }; for (int i = 0; i < players.Length; i++) { // This is the essense of Polymorphism. We can treat these as their parent (or base) class. GameCharacter character = players[i]; // But that doesn't change what they are... Warriors are always warriors. So the instantiated type (the child class) // is the one thats method is called. character.Play(); // We can access the Move method off of Game Character and it calls the Move method from the instantiated type. // even those it is not implemented in the abstract class. character.Move(1, 1); } }
static void Main(string[] args) { GameCharacter gameCharacter = new GameCharacter("John", 98, 75); gameCharacter.Play(); GameCharacter[] gameCharacters; gameCharacters = new GameCharacter[5]; gameCharacters[0] = new SubclassWarrior("Dave", 100, 60, "sword"); gameCharacters[1] = new SubclassWarrior("Paul", 100, 60, "broad sword"); gameCharacters[2] = new SubclassWizard("Steven", 60, 100, 80, 75); gameCharacters[3] = new SubclassWizard("Chris", 60, 100, 80, 75); gameCharacters[4] = new SubclassWizard("Ollie", 60, 100, 80, 75); for (int i = 0; i < gameCharacters.Length; i++) { gameCharacters[i].Play(); } }