static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); SalesPerson danny = new SalesPerson(); danny.Age = 31; danny.Name = "Danny"; danny.SalesNumber = 50; Manager chucky = new Manager("Chucky", 50, 92, 100000, "222-33-1223", 9000); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); double cost = chucky.GetBenefitsCost(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-33-1243", 31); fran.GiveBonus(200); fran.DisplayStats(); Employee.BenefitPackage.BenefitPackageLevel myBenefitLevel = Employee.BenefitPackage.BenefitPackageLevel.Platinum; CastingExamples(); Console.ReadLine(); }
// Create a subclass object and access base class functionality static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); SalesPerson fred = new SalesPerson(); fred.Age = 31; fred.Name = "Fred"; fred.SalesNumber = 50; // Assume Manager has a constructor matching this signature: // (string fullName, int age, int empID, // float currPay, string ssn, int numbofOpts) Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); double cost = chucky.GetBenefitCost(); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); // Define my benefit level. BenefitPackage.BenefitPackageLevel myBenefitLevel = BenefitPackage.BenefitPackageLevel.Platinum; SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); Console.ReadKey(); }
public 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, 3001, 100000, "111-12-1119", 90); GivePromotion(jill); }
static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); // Error! Cannot create an abstract class! //Employee X = new Employee(); // A better bonus system! Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); // ITSpecialist ITSpecialist itsepc = new ITSpecialist() { Age = 31, Education = "MSc in IT", ID = 45, Name = "Phil", SocialSecurityNumber = "444-33-2222", SpecialistKnowledge = ".NET", Pay = 90000, Benefits = new Employee.BenefitPackage() }; Console.WriteLine("FedTax -> {0}", itsepc.getFedTax(90000)); Console.WriteLine("SSTax -> {0}", itsepc.getSsTax(125, 90000)); Console.WriteLine("\r\n"); // Secretary Secretary sec = new Secretary() { Age = 23, Education = "Gymi", ID = 47, Name = "Susy", SocialSecurityNumber = "333-22-1111", LanguageKnowledge = "French", Pay = 45000, Benefits = new Employee.BenefitPackage() }; Console.WriteLine("FedTax -> {0}", sec.getFedTax(45000)); Console.WriteLine("HealthFee -> {0}", sec.getHealthFee(100, 45000)); CastingExamples(); Console.ReadLine(); }
static void CastingExamples() { object frank = new Manager("Frank Zappa", 9, 3000, 40000, "111-11-1111", 5); //GivePromotion(frank);//doesn't work bc "frank" is stored in a System.Object, which is higher up //than Employee on the inheritance chain, so an implicit cast can't happen... //instead we must EXPLICITLY cast it along the lines of (ClassIWantToCastTo)referenceIHave... //even though it makes NO sense to cast frank to a Hexagon, you can! //***Hexagon hex = (Hexagon)frank;*** and it compiles! //Nothing would blow up until runtime when you'd get an exception //When you are explicitly casting you can use a try/catch to check for compatibility... //Hexagon hex; //try //{ // hex = (Hexagon)frank; //} //catch (InvalidCastException ex) //{ // Console.WriteLine(ex.Message); } //you can also test for compatibility via the "as" keyword... //object[] things = new object[4]; //things[0] = new Hexagon(); //things[1] = false; //things[2] = new Manager(); //things[3] = "Last Thing"; //foreach (object item in things) //{ // Hexagon h = item as Hexagon; // if (h == null) // { // Console.WriteLine("Item is not a hexagon"); // } // else // h.Draw(); //} //comparisons can also be made with the "is" keyword. This checks for a return of flase, rather //than a null value as was seen with "as" keyword. "is" doesn't doesn't perform a cast, it just //checks compatibility so that you can subsequenly cast as you like(w/o a PITA try/catch) GivePromotion((Manager)frank); Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1111", 1); GivePromotion(moonUnit); SalesPerson jill = new PTSalesPerson("Jill", 834, 3002, 10000, "111-22-3333", 90); GivePromotion(jill); }
static void Main(string[] args) { SalesPerson danny = new SalesPerson(); danny.Age = 31; danny.Name = "Danny"; danny.SalesNumber = 50; Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); chucky.GiveBonus(300); double cost = chucky.GetBenefitCost(); chucky.DisplayStats(); Console.WriteLine("Benefícios: {0}", cost); Employee.BenefitPackage.BenefitPackageLevel myBenefitLevel = Employee.BenefitPackage.BenefitPackageLevel.Platinum; Console.WriteLine("Level: {0}", myBenefitLevel); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); try { PTSalesPerson ptSales = ((PTSalesPerson)danny); } catch (InvalidCastException ex) { Console.WriteLine(ex.Message); } PTSalesPerson ptCast = danny as PTSalesPerson; if (ptCast == null) { Console.WriteLine("Sorry, danny is not PTSalesPerson"); } Console.WriteLine(danny.ToString() + "&&" + danny.GetType()); Console.ReadLine(); }
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 Main( string[] args ) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); // Give each employee a bonus? Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); Console.WriteLine(); CastingExamples(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("**** The Employee Class Hierarchy ****\n"); SalesPerson fred = new SalesPerson(); fred.p_empAge = 31; fred.p_empName = "Fred"; fred.p_SalesNumber = 100; // Assume Manager has a constructor matching this signature: // ... Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); // Error! Cannot create an abstract class! //Employee X = new Employee(); // A better bonus system! Manager chucky = new Manager("Chucky", 50, 92, 0, "333-23-2322", 0); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); CastingExamples(); Console.ReadLine(); }
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..."); }
static void Main(string[] args) { SalesPerson fred = new SalesPerson(); fred.Age = 31; fred.Name = "Fred"; fred.SalesNumber = 50; Manager chucky = new Manager("Chucky", 50, 92, 100000, "3-33-11", 9000); double cost = chucky.GetBenefitsCost(); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "3-22-22", 31); fran.GiveBonus(200); fran.DisplayStats(); OuterClass.PublicInnerClass inner = new OuterClass.PublicInnerClass(); //OK //Employee e = new Employee(); ERROR, abstract class Console.ReadLine(); }
public CompDetails(Object data) : this() { this.DataContext = data; if (data is Employee) { string name = ""; string value = ""; if (data is Manager) { Manager man = (Manager)data; name = "Reports"; string options = "Stock Options"; // Set spare prop name and value man.GetSpareProp1(ref name, ref value); this.SpareProp1Name.Content = name; this.SpareProp1Value.Content = man.ReportsByName(); this.SpareProp2Name.Content = options; this.SpareProp2Value.Content = man.StockOptions; // Make visible after setting values this.SpareProp1Name.Visibility = Visibility.Visible; this.SpareProp1Value.Visibility = Visibility.Visible; this.SpareProp2Name.Visibility = Visibility.Visible; this.SpareProp2Value.Visibility = Visibility.Visible; } if (data is Executive) { Executive exe = (Executive)data; name = "Reports"; string options = "Stock Options "; // Set spare prop name and value exe.GetSpareProp1(ref name, ref value); this.SpareProp1Name.Content = name; this.SpareProp1Value.Content = exe.ReportsByName(); this.SpareProp2Name.Content = options; this.SpareProp2Value.Content = exe.StockOptions; // Make visible after setting values this.SpareProp1Name.Visibility = Visibility.Visible; this.SpareProp1Value.Visibility = Visibility.Visible; this.SpareProp2Name.Visibility = Visibility.Visible; this.SpareProp2Value.Visibility = Visibility.Visible; } if (data is Engineer) { Engineer eng = (Engineer)data; name = "Highest Degree"; // Set spare prop name and value eng.GetSpareProp1(ref name, ref value); this.SpareProp1Name.Content = name; this.SpareProp1Value.Content = eng.HighestDegree; // Make visible after setting values this.SpareProp1Name.Visibility = Visibility.Visible; this.SpareProp1Value.Visibility = Visibility.Visible; } if (data is SalesPerson) { SalesPerson sales = (SalesPerson)data; name = "Number of Sales"; // Set spare prop name and value sales.GetSpareProp1(ref name, ref value); this.SpareProp1Name.Content = name; this.SpareProp1Value.Content = sales.SalesNumber; // Make visible after setting values this.SpareProp1Name.Visibility = Visibility.Visible; this.SpareProp1Value.Visibility = Visibility.Visible; } if (data is PTSalesPerson) { PTSalesPerson pTSales = (PTSalesPerson)data; name = "Number of Sales"; // Set spare prop name and value pTSales.GetSpareProp1(ref name, ref value); this.SpareProp1Name.Content = name; this.SpareProp1Value.Content = pTSales.SalesNumber; // Make visible after setting values this.SpareProp1Name.Visibility = Visibility.Visible; this.SpareProp1Value.Visibility = Visibility.Visible; } if (data is SupportPerson) { SupportPerson support = (SupportPerson)data; name = "Shift: "; // Set spare prop name and value support.GetSpareProp1(ref name, ref value); this.SpareProp1Name.Content = name; this.SpareProp1Value.Content = support.Shift; // Make visible after setting values this.SpareProp1Name.Visibility = Visibility.Visible; this.SpareProp1Value.Visibility = Visibility.Visible; } } }