using
System;
using System.Collections.Generic;
public class MyClass
{
public static void Main()
{
CommissionedEmployee [] salespeople =
{
new CommissionedEmployee( " Bob " ),
new CommissionedEmployee( " Ted " ),
new CommissionedEmployee( " Sally " )
};
Employee[] employees = (Employee[])salespeople.Clone();
foreach (Employee person in employees)
{
person.Pay();
}
// roArray[1]=2;
RL();
}
#region Helper methods
private static void WL( object text, params object [] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}
public class Employee
{
public Employee( string name)
{
m_Name = name;
}
public virtual void Pay()
{
Console.WriteLine( " Paying {0} " ,m_Name);
}
private string m_Name;
}
public class CommissionedEmployee:Employee
{
public CommissionedEmployee( string name): base (name)
{
}
public override void Pay()
{
base .Pay();
Console.WriteLine( " Paying commisions " );
}
}
using System.Collections.Generic;
public class MyClass
{
public static void Main()
{
CommissionedEmployee [] salespeople =
{
new CommissionedEmployee( " Bob " ),
new CommissionedEmployee( " Ted " ),
new CommissionedEmployee( " Sally " )
};
Employee[] employees = (Employee[])salespeople.Clone();
foreach (Employee person in employees)
{
person.Pay();
}
// roArray[1]=2;
RL();
}
#region Helper methods
private static void WL( object text, params object [] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}
public class Employee
{
public Employee( string name)
{
m_Name = name;
}
public virtual void Pay()
{
Console.WriteLine( " Paying {0} " ,m_Name);
}
private string m_Name;
}
public class CommissionedEmployee:Employee
{
public CommissionedEmployee( string name): base (name)
{
}
public override void Pay()
{
base .Pay();
Console.WriteLine( " Paying commisions " );
}
}
The Output is:
Paying Bob
Paying commisions
Paying Ted
Paying commisions
Paying Sally
Paying commisions
Paying commisions
Paying Ted
Paying commisions
Paying Sally
Paying commisions
Polymorphism is employed in the foreach loop to call the Pay method of the derived class.
Why??
i'm confused. And someday maybe i find the reason of this.
The overload and override/polyorphism is difficult to me. Sometimes it seems easy, and other time it looks very difficult.
If I find some formal explanation for the Polymorphism. I will post here for my study.
o(∩_∩)o