C# 属性

来源:互联网 发布:js模块化编程原理 编辑:程序博客网 时间:2024/05/17 06:13
参考视频同上
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace attribute{    class Program    {        static void Main(string[] args)        {            Person p = new Person();            p.Age = 27;            Console.WriteLine(p.Age);            Console.Read();        }    }    class Person    {        private int age;        public int Age //属性首字母大写  值被保存在age中 Age中没有保存数据        {            set            {                if (value < 0) //public字段和属性的区别,可以进行非法设置值的判断                {                    return;                }                this.age = value;//value代表用户赋值过来的值(好像是特定的)            }            get             {                return this.age;            }        }    }}

0 0