Ejemplo n.º 1
0
        // 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();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** The Employee Class Hierarchy *****\n");
            SalesPerson fred = new SalesPerson
            {
                Age         = 31,
                Name        = "Fred",
                SalesNumber = 50
            };

            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();

            double cost = chucky.GetBenefitCost();

            Console.WriteLine($"Benefit Cost: {cost}");

            Employee.BenefitPackage.BenefitPackageLevel myBenefitLevel =
                Employee.BenefitPackage.BenefitPackageLevel.Platinum;

            CastingExamples();
            Console.ReadLine();
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** The Employee Class Hierarchy *****\n");
            SalesPerson danny = new SalesPerson();

            danny.Name        = "Danny";
            danny.Age         = 31;
            danny.SalesNumber = 50;

            Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000);

            //错误!不能从对象实例中直接访问受保护数据
            //Employee emp = new Employee();
            //emp.empName = "Fred";

            double cost = chucky.GetBenefitCost();

            //  定义福利等级
            Employee.BenefitPackage.BenefitPackageLevel myBendfitLevel = Employee.BenefitPackage.BenefitPackageLevel.Platinum;

            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();

            Console.ReadLine();
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** The Employee Class Hierarchy *****\n");
            SalesPerson beedle = new SalesPerson();

            beedle.Age         = 28;
            beedle.Name        = "Beedle";
            beedle.SalesNumber = 38;

            // A better bonus system!
            Manager ganondorf = new Manager("Ganondorf", 200, 4, 181000, "320-10-2923", 15000);
            double  cost      = ganondorf.GetBenefitCost();

            ganondorf.GiveBonus(3000);
            ganondorf.DisplayStats();
            Console.WriteLine();

            SalesPerson impa = new SalesPerson("Impa", 67, 21, 90000, "281-23-2180", 17);

            impa.GiveBonus(1000);
            impa.DisplayStats();

            // Define my benefit level.
            Employee.BenefitPackage.BenefitPackageLevel myBenefitLevel =
                Employee.BenefitPackage.BenefitPackageLevel.Platinum;

            Console.ReadLine();
        }
Ejemplo n.º 5
0
        static void Containment()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("=> Containment and Delegation");

            Manager david = new Manager("David", 51, 91, 100001, "333221111", 9001);
            double  cost  = david.GetBenefitCost();

            Console.WriteLine($"cost: {cost}");
        }
Ejemplo n.º 6
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            SalesPerson fred = new SalesPerson("Brenda", 35, 287, 18000, "JN8276172A", 247);

            fred.Age         = 31;
            fred.Name        = "Fred";
            fred.SalesNumber = 50;

            Manager chucky = new Manager("Chucky", 50, 92, 10000, "333-23-2322", 9000);

            double cost = chucky.GetBenefitCost();

            Console.WriteLine("Benefits Cost:{0}", cost.ToString());
            Employee.BenefitsPackage.BenefitPackageLevel myBenefitLevel = Employee.BenefitsPackage.BenefitPackageLevel.Platinum;

            chucky.GiveBonus(300);
            chucky.DisplayStats();

            SalesPerson fran = new SalesPerson("Fran", 43, 93, 30000, "832-32-3232", 31);

            fran.GiveBonus(200);
            fran.DisplayStats();
            object  frank = new Manager("Frank Zappa", 9, 3000, 40000, "111-11-1111", 5);
            Hexagon hex;

            try { hex = (Hexagon)frank; }
            catch (InvalidCastException ex)
            { Console.WriteLine(ex.Message); }

            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();
                }
            }

            CastingExamples();
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            Console.WriteLine("yet another header");
            SalesPerson fred = new SalesPerson();

            fred.Age         = 31;
            fred.Name        = "Fred";
            fred.SalesNumber = 50;

            Manager chucky = new Manager("Chucky", 50, 92, 100000, "111-22-3333", 9000);

            Console.WriteLine("Chucky's benefits cost {0}", chucky.GetBenefitCost());
            Console.ReadLine();
        }
Ejemplo n.º 8
0
        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();
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** The Employee Class Hierarchy *****\n");

            // Create a subclass object and access base class functionality.
            SalesPerson fred = new SalesPerson();

            fred.Age         = 31;
            fred.Name        = "Fred";
            fred.SalesNumber = 50;

            Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000);
            double  cost   = chucky.GetBenefitCost();

            // Create and use the public inner class. OK!
            OuterClass.PublicInnerClass inner;
            inner = new OuterClass.PublicInnerClass();

            // Compiler Error! Cannot access the private class.
            //OuterClass.PrivateInnerClass inner2;
            //inner2 = new OuterClass.PrivateInnerClass();

            // Define my benefit level.
            Employee.BenefitPackage.BenefitPackageLevel myBenefitLevel = Employee.BenefitPackage.BenefitPackageLevel.Platinum;
            Console.WriteLine("BenefitPackageLevel: {0}", myBenefitLevel);
            Console.WriteLine();

            // Give each employee a bonus?
            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();

            // Error! Cannot create an instance of an abstract class!
            //Employee X = new Employee();

            CastingExamples();

            Console.ReadLine();
        }
Ejemplo n.º 10
0
        static void Main(string[] args)
        {
            Salesperson fred = new Salesperson();

            fred.Age         = 31;
            fred.Name        = "Fred";
            fred.SalesNumber = 50;
            PTSalesperson genry = new PTSalesperson("Genry", 19, 2, 16000, "652114", 0);

            genry.DisplayStats();
            genry.ReductionSalary();
            genry.DisplayStats();
            Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000);
            double  cost   = chucky.GetBenefitCost();

            Console.WriteLine("Benefits: {0}", cost);
            chucky.DisplayStats();
            Console.ReadLine();
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Fun with Inheritance****\n");
            SalePersons Fred = new SalePersons();

            Fred.Name       = "Fred";
            Fred.Age        = 31;
            Fred.SaleNumber = 50;

            Console.WriteLine("My name is {0}", Fred.Name);
            Console.WriteLine("My age is {0}", Fred.Age);
            Console.WriteLine("I sale {0}", Fred.SaleNumber);

            // (string fullName, int age, int empID, float currPay,string ssn, int numbofopts);
            Manager chuck = new Manager("Chuck", 50, 90, 10000, "", 9000);
            double  cost  = chuck.GetBenefitCost();

            Employees.BenefitPackage.BenefitPackageLevel myBenefitPackageLevel =
                Employees.BenefitPackage.BenefitPackageLevel.Platinum;
            Console.ReadLine();
        }
Ejemplo n.º 12
0
        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;

            Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000);
            double  cost   = chucky.GetBenefitCost();

            Console.WriteLine("Chucky benefit cost: {0}", cost);

            PTSalesPerson TestPTSP = new PTSalesPerson("Alex", 24, 93, 500000, "1337-1337", 4000, 4);

            Console.WriteLine("SalesPerson {0} works {1} hours / day", TestPTSP.Name, TestPTSP.WorkHours);


            Console.ReadLine();
        }
Ejemplo n.º 13
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Yhe employee class hierarchy ****");
            SalesPerson fred = new SalesPerson();

            fred.EmpName     = "Fred";
            fred.EmpAge      = 32;
            fred.SalesNumber = 210;
            fred.CurrPay     = 100;
            fred.GiveBonus(1000);
            fred.DisplayStatus();
            Console.WriteLine();

            Manager chuky = new Manager("Chuky", 42, 10010, 30000, 52);

            Console.WriteLine("Benefit: {0}", chuky.GetBenefitCost().ToString());
            chuky.GiveBonus(300);
            chuky.DisplayStatus();
            Console.WriteLine();

            Console.ReadLine();
        }
Ejemplo n.º 14
0
        static void Main(string[] args)
        {
            Console.WriteLine("****** The Employee Class Hierarchy ******\n");
            SalesPerson fred = new SalesPerson()
            {
                Age = 31, Name = "Fred", SalesNumber = 50, Pay = 6000
            };
            Manager chucky = new Manager("Chucky", 45, 1497, 102450, "1678-05-04", 9000);
            double  cost   = chucky.GetBenefitCost();

            Employee.BenefitPackage.BenefitPackageLevel myBenefitlevel = Employee.BenefitPackage.BenefitPackageLevel.Platinum;
            chucky.DisplayStats();
            chucky.GiveBonus(300);
            chucky.DisplayStats();
            Console.WriteLine();
            fred.DisplayStats();
            fred.GiveBonus(150);
            fred.DisplayStats();
            Console.WriteLine();

            CastingExamples();
            Hexagon hex;
            object  frank = new Manager();

            try
            {
                hex = (Hexagon)frank;
            }
            catch (Exception e)
            {
                ConsoleColor prevCol = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(e.Message);
                Console.ForegroundColor = prevCol;
            }

            TestAsKeyword();
            Console.ReadLine();
        }
Ejemplo n.º 15
0
        static void Main(string[] args)
        {
            // create a subclass object and access base class functionality
            Console.WriteLine("***** This Employee class Hierarchy *****\n");
            SalesPerson fred = new SalesPerson
            {
                Age         = 31,
                Name        = "Fred",
                SalesNumber = 50
            };

            // Nested Type Definitions
            // Create and use the public inner class
            // example: Employee.BenefitPackage.BenefitPackageLevel.Platinum;

            Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000);
            double  cost   = chucky.GetBenefitCost(); // calling the BenefitPackage, a class instance within the employee class with its own property

            PTSalesPerson pauly = new PTSalesPerson("Paul", 22, 298, 28_000, "444-99-4990", 1, "Fred");

            // "Paul", 22, 298, 28_000, "444-99-4990", 1, "Fred" or "Chucky"

            // display how things are going
            Console.WriteLine($"\nSalesPerson *** {fred.GetName()} ***");
            fred.DisplayStats();

            Console.WriteLine($"\nManager *** {chucky.GetName()} ***");

            chucky.DisplayStats();

            Console.WriteLine($"\nPartTime SalesPerson *** {pauly.GetName()} ***");
            pauly.DisplayStats();
            Console.Write($"{pauly.GetName()} reports to:");
            Console.Write($"{pauly.ManagerName()}\n\n");

            // give a bonus
            float bonusAmt = 2_500;

            chucky.GiveBonus(bonusAmt);
            Console.WriteLine("{0} got a bonus of {1:c}!", chucky.Name, bonusAmt);
            chucky.DisplayStats();
            Console.WriteLine();

            SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31);

            fran.GiveBonus(200);
            fran.DisplayStats();
            Console.WriteLine();

            // give a promotion or two
            GivePromotion(fran);
            GivePromotion(pauly);

            Keyword_AS_Example();

            // temporarily showing Casting Examples method()
            CastingExamples();

            // the need for an abstract class:
            // Employee emmy = new Employee("Emma", 34, 239, 47_000, "324-23-3434");
            // emmy.DisplayStats();
            // to avoid allowing instantiation of an employee with no assignment/job classification
            // MUST remove keyword abstract from the Employee Class

            // pause program executions before exit
            Console.ReadLine();
        }
Ejemplo n.º 16
0
        // 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);

            Console.WriteLine(chucky.Name);

            // Error! Can't access protected data from client code.
            //Employee emp = new Employee();
            //emp.empName = "Fred";

            double cost = chucky.GetBenefitCost();

            Console.WriteLine($"cost: {cost}");

            // Define my benefit level.
            Employee.BenefitPackage.BenefitPackageLevel myBenefitLevel =
                Employee.BenefitPackage.BenefitPackageLevel.Platinum;
            Console.WriteLine($"myBenefitLevel: {myBenefitLevel}");

            // Give each employee a bonus?
            // 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();

            // Ack! You can't cast frank to a Hexagon, but this compiles fine!
            // Catch a possible runtime exception which can be happened by invalid explicit type cast.
            object  frank = new Manager();
            Hexagon hex;

            try
            {
                hex = (Hexagon)frank;
            }
            catch (InvalidCastException ex)
            {
                Console.WriteLine(ex.Message);
            }


            // Use "as" to test compatability.
            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();
                }
            }



            Console.ReadLine();
        }