属性

来源:互联网 发布:xampp 数据库密码 编辑:程序博客网 时间:2024/04/30 00:16
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication10
{
    class Program
    {
        private int month = 7;
        public int Month
        {
            get
            {
                return month;
            }
            set
            {
                if((value>0) && (value<13)) //只有是正确的月份才可以分配新值
                {
                    month = value;
                }
            }
        }
        static void Main(string[] args)
        {

        }
    }
}
注:属性与方法平级,故不可放在方法内声明
原创粉丝点击