示例#1
0
        static void Main(string[] args)
        {
            // Method 2 using initialization
            Employee Employee = new Employee()
            {
                Age = 45,
                Name = "Dave",
                Salary = 50000.00,
                StartDate = new DateTime(2012, 7, 10),
                PhoneNumber = "408-555-1234"
            };
            Console.WriteLine("{0}'s age is {1}, started on {2} and makes {3}", Employee.Name, Employee.Age, Employee.StartDate, Employee.Salary); // before bonus
            Employee.Bonus(0.05);
            Console.WriteLine("{0}'s age is {1}, started on {2} and makes {3} with bonus", Employee.Name, Employee.Age, Employee.StartDate, Employee.Salary); // after bonus

            // Method 3 using contructor
            Employee Employee2 = new Employee(25, "Mary", 60000.00, new DateTime(2010, 2, 28), "408-321-5555");
            Console.WriteLine("{0}'s age is {1}, started on {2} and makes {3}", Employee2.Name, Employee2.Age, Employee2.StartDate, Employee2.Salary);
        }
示例#2
0
        static void Main(string[] args)
        {
            //there are three ways to get data into a new employee instance

            //1. assign the data
            Employee Dave = new Employee();
            Dave.Age = 35;
            Console.WriteLine("Dave's age is {0}", Dave.Age);

            //2. initialize any or all of the properties
            Employee Amy = new Employee()
            {
                Age = 22,
                Name = "Amy Simmons",
                Salary = 200000,
                StartingDate = new DateTime(2015, 7, 20),
                PhoneNumber = "made up phone no"
            };
            Console.WriteLine("Amy's age is {0}, she started on {1}, her salary is {2}",
                Amy.Age, Amy.StartingDate, Amy.Salary);

            //3a. use a standard method
            Employee Tom = new Employee()
            {
                Age = 50,
                Name = "Tom",
                Salary = 100,
                StartingDate = new DateTime(2015, 7, 20),
                PhoneNumber = "made up phone no"
            };
            Console.WriteLine("Tom's age is {0}, he started on {1}, his salary is {2}",
                Tom.Age, Tom.StartingDate, Tom.Salary);
            Tom.Bonus(0.05);
            Console.WriteLine("Tom's age is {0}, he started on {1}, his salary is {2}",
                Tom.Age, Tom.StartingDate, Tom.Salary);

            //3b. use a constructor method
            Employee Mary = new Employee(25, "Mary Jones", 60000, new DateTime(2015, 7, 26));
            Console.WriteLine("Mary's age is {0}, her name is {1}, her salary is {2}, she started on {3}",
                Mary.Age, Mary.Name, Mary.Salary, Mary.StartingDate);
        }