c# 类的属性生字段的意义和写法

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

namespace xuexi
{
    internal class Ps
    {
        //字段 _
        public string _name;

        //属性的本质就是2个方法  get{}  set{}
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string _age;
        public string Age
        {
            get { return _age; }
            set { _age = value; }
        }

        public void getns() {
            Console.WriteLine("我的名字{0},今年{1}",this._name,this._age);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;


namespace xuexi
{
    class Program
    {

        static void Main(string[] args)
        {
            Ps p =new Ps();
            p.Name="Test";
            p.Age = "大约40岁了";

            p.getns();

        }
    }

}
分类:

发表评论