C#例項建構函式

iamzxf發表於2015-03-21


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace instanceConstruct
{
    class Employee
    {
        private string name;
        public int Age { get; set; }
        private double salary=2000;
        public double Salary
        {
            get { return salary; }
            set { salary = value; }
        }

        public Employee(string name)
        {
            this.name = name;
        }
        public Employee(string name, int age, double salary)
        {
            this.name = name;
            this.Age = age;
            this.salary = salary;
        }
        public void display()
        {
            Console.WriteLine("{0},{1},{2}", name, Age, salary);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Employee emp = new Employee("李明");
            emp.display();

            Employee emp1 = new Employee("李明1",23,3000);
            emp1.display();

            Employee emp2 = new Employee("李明2") { Age = 23, Salary = 4000 };
            emp2.display();

            Console.ReadLine();

        }
    }
}




相關文章