C#中有關欄位的訪問控制

iamzxf發表於2015-03-17


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

namespace fieldDemo
{
    class Employee {
        public readonly int id;
        public string name;
        public int age = 20;
        public const int minAge = 18;
        public static string company;

        public void display()
        {
            Console.WriteLine("name:{0}, age:{1}, company:{2}",name,age,company);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Employee.company = "ldu";
            Employee emp1 = new Employee();

            emp1.name = "zxf";
            emp1.age = 25;
            if (emp1.age < Employee.minAge)
                Console.WriteLine("不符合要求");
            else
                Console.WriteLine("name:{0}, age:{1}, company:{2}",emp1.name,emp1.age, Employee.company);

            Employee emp2 = new Employee();
            emp2.display();
            Console.ReadLine();
        }
    }
}




相關文章