static void Main(string[] args) { // Using inheritance to declare different Member types within an array of Members Member[] clubMembers = new Member[5]; clubMembers[0] = new NormalMember("Special Rate", "James", 1, 2010); clubMembers[1] = new NormalMember("Normal Rate", "Andy", 2, 2011); clubMembers[2] = new NormalMember("Normal Rate", "Bill", 3, 2011); clubMembers[3] = new VIPMember("Carol", 4, 2012); clubMembers[4] = new VIPMember("Evelyn", 5, 2012); /* CalculateAnnualFee method was initially only in the child classes * needed to be added in the Member parent class and declared virtual * the methods in the child class needed to have the override keyword added * this is where the compiler will try to execute this method. * #Polymorphism - simply means that at run time, the program is smart enough to use * the CalculateAnnualFee() method from the correct child class even when that object * is declared to be of type Member type. * * At run time the program determines that the first three members of clubmembers * are of NormalMember type and executes the CalculateAnnualFee() method from that class. * It also determines that the last two members are of VIPMember type and executes the * method from that class. * * The runtime type of the first three elements of clubMembers is NormalMember while * the runtime type of the last two elements is VIPMember. The declared type of all 5 elements is Member. */ foreach (Member m in clubMembers) { m.CalculateAnnualFee(); Console.WriteLine(m.ToString()); } Console.WriteLine("\nGetType() and typeof() example"); if (clubMembers[0].GetType() == typeof(VIPMember)) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } /* NormalMember mem1 = new NormalMember("Special Rate", "James", 1, 2010); * VIPMember mem2 = new VIPMember("Andy", 2, 2011); * * mem1.CalculateAnnualFee(); * mem2.CalculateAnnualFee(); * Console.WriteLine(mem1.ToString()); * Console.WriteLine(mem2.ToString()); */ Console.ReadLine(); }
static void Main(string[] args) { NormalMember mem1 = new NormalMember("Special Rate", "James", 1, 2010); VIPMember mem2 = new VIPMember("Andy", 2, 2011); mem1.CalculateAnnualFee(); mem2.CalculateAnnualFee(); Console.WriteLine(mem1.ToString()); Console.WriteLine(mem2.ToString()); Console.Read(); }
static void Main(string[] args) { //NormalMember mem1 = new NormalMember("Special Rate", "James", 1, 2010); //VIPMember mem2 = new VIPMember("Andy", 2, 2011); //mem1.CalculateAnnualFee(); //mem2.CalculateAnnualFee(); //WriteLine(mem1.ToString()); //WriteLine(mem2.ToString()); //Since this array is based on the parent class, it can contain any child class. Member[] clubMembers = new Member[5]; //The runtime type of 0-2 is NormalMember, the runtime type of 3-4 is VIPMember. //The declared type of all 5 is Member. clubMembers[0] = new NormalMember("Special Rate", "James", 1, 2010); clubMembers[1] = new NormalMember("Normal Rate", "Andy", 2, 2011); clubMembers[2] = new NormalMember("Normal Rate", "Bill", 3, 2011); clubMembers[3] = new VIPMember("Carol", 4, 2012); clubMembers[4] = new VIPMember("Evelyn", 5, 2012); foreach (Member m in clubMembers) { //We can't call this method unless it exists in the parent class. m.CalculateAnnualFee(); WriteLine(m.ToString()); //The runtime type can be determined at runtime. if (m.GetType() == typeof(VIPMember)) { WriteLine("VIP"); } else { WriteLine("Normal"); } } ReadKey(); }