public override void UnexpectedResult() { // Note the different data types! Man jakeRef = new Jake(); Woman maryRef = new Mary(); maryRef.Accept(jakeRef); /* The previous code will print: * * > Jake loves Mary. * * As expected! */ }
public static void MainMethod() { var michael = new Mary("Michael"); var elliott = new Mary("Elliott"); var gertie = new Mary("Gertie"); Console.WriteLine($"\nMary has {Mary._numberOfChildren} children."); Console.WriteLine($"Elliot is child number {elliott._childNumber}.\n"); Mary.Teach(michael); michael.Learn(); Mary.Feed(elliott); elliott.Eat(); Mary.PlayWith(gertie); gertie.Play(); }
public override void UnexpectedResult() { // Note the different data types! Man jakeRef = new Jake(); Woman maryRef = new Mary(); jakeRef.Loves(maryRef); /* The previous code will print: * * > Jake loves every woman! * * instead of * * > Jake loves Mary. * * Runtime chooses method dynamically based on jakeRef ONLY! */ }
public override void ExpectedResult() { Man theMan = new Man(); Jake jake = new Jake(); Woman theWoman = new Woman(); Mary mary = new Mary(); theMan.Loves(theWoman); theMan.Loves(mary); jake.Loves(theWoman); jake.Loves(mary); /* The previous code will print: * * > Every man loves every woman! * > Every man loves Mary. * > Jake loves every woman! * > Jake loves Mary. * * As expected! */ }
public override void Loves(Mary mary) { Console.WriteLine("Jake loves Mary."); }
public virtual void Loves(Mary mary) { Console.WriteLine("Every man loves Mary."); }