static void CastingExamples() { object frank = new Manager("Frank Zappa", 9, 3000, 40000, "111-11-1111", 5); GivePromotion((Manager)frank); Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1321", 1); GivePromotion(moonUnit); SalesPerson jill = new PtSalesPerson("Jill", 834, 3002, 100000, "111-12-1119", 90); GivePromotion(jill); }
static void CastingExamples() { // A Manager "is-a" System.Object, so we can // store a Manager reference in an object variable just fine. object frank = new Manager("Frank Zappa", 9, 3000, 40000, "111-11-1111", 5); GivePromotion((Manager)frank); // A Manager "is-an" Employee too. Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1321", 1); GivePromotion(moonUnit); // A PtSalesPerson "is-a" SalesPerson. SalesPerson jill = new PtSalesPerson("Jill", 834, 3002, 100000, "111-12-1119", 90); GivePromotion(jill); }
static void CastingExamples() { object frank = new Manager("Frank Zappa", 9, 3000, 40000, "111-11-1111", 5); Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-3234", 1); Employee jill = new PtSalesPerson("Jill", 832, 3002, 100000, "111-32-4323", 90); GivePromotion((Manager)frank); try { Hexagon hex = (Hexagon)frank; } catch (InvalidCastException ex) { Console.WriteLine(ex.Message); } Hexagon hex2 = frank as Hexagon; if(hex2 == null) Console.WriteLine("Sorry, frank is not Hexagon..."); }
public static void Main(string[] args) { SalesPerson fred = new SalesPerson { Age = 31, Name = "Fred", SalesNumber = 50 }; // extending PtSalesPerson from SalesPerson PtSalesPerson partTime1 = new PtSalesPerson { Name = "Hari", Age = 31, Id = 2342, SalesNumber = 40, Pay = 234.4F, }; //Manager Manager chucky = new Manager("Chucky", 50, 92, 1000000, "333-2332-s23", 9000); double cost = chucky.GetBefitCost(); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson frank = new SalesPerson("Fran", 43, 93, 3000, "9333-32-3232", 31); frank.GiveBonus(200); frank.DisplayStats(); Console.WriteLine(); CastingExamples(); }