属性的运用和理解

发布于:2025-08-02 ⋅ 阅读:(13) ⋅ 点赞:(0)

1:属性快捷方式

//输入:prop,回车键,TAB键
 public int MyProperty { get; set; }
 //输入:propfull,回车键,TAB键
private int myVar;
        public int MyProperty
        {
            get { return myVar; }
            set { myVar = value; }
        }

2:定义属性()

namespace ConsoleApp2
{
    class student
    {
        
        public string name { get; set; }

        private int age;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }


    }
}

3:打开反汇编工具,将生成的exe文件拖入即可

4:含义: 

 5:断点调试属性是如何赋值的(1)

namespace ConsoleApp2
{
    class student
    {
        private int age=0;
        public int Age
        {
            get { return age; }
            set { age = value; }
        } 


    }
}

    class Program
    {
        static void Main(string[] args)
        {

            student student = new student();
            student.Age = 10;
        }
    }
}

a:

 b:

c: 

d:

6: 断点调试属性是如何赋值的(2)

namespace ConsoleApp2
{
    class student
    {
        int age = 10;
        public int Age { get; set; } = 30;
    }
}
namespace ConsoleApp2
{

    class Program
    {
        static void Main(string[] args)
        {

            student student = new student();
            student.Age = 10;
        }
    }
}

a:

b: 

c:

 

6:属性扩展方法

a:增加业务判度逻辑


namespace ConsoleApp2
{
    class student
    {
        private int age;

        public int Age
        {
            get { return age; }
            set {
                if (age<0)
                {
                    age = 1;
                }
                else
                {
                    age = value;
                }               
                }
        }

    }
}

 b:设置只读字段功能(无私有字段)

namespace ConsoleApp2
{
    class student
    {
      
        //无私有字段,外界读取数据的入口
        public int Age
        {
            get 
            {
                int result = 2;//此处的值可以是别的地方获取来的
                return result;
            }           
           
        }

    }
}

7:属性的初始化

namespace ConsoleApp2
{
    class student
    {
        public int Age { get; set; } = 10;
        public string  Name { get;  } = "苏苏";
        public DateTime time
        {
            get { return DateTime.Now; }
        }
        public DateTime time2 { get => DateTime.Now; }

        public DateTime time3=> DateTime.Now;
    }
}

8:构造方法和对象初始化顺序(先初始化字段和显示赋值的属性,原因:如果字段没有初始化,属性和私有字段在构造方法中无法调用),再构造方法

9:对象初始化器和构造方法对比 

对象初始化器:只能初始化属性


namespace ConsoleApp2
{

    class Program
    {
        static void Main(string[] args)
        {

            student student = new student
            {
                Age = 10,
                MyProperty=1
            } ;
          
        }
    }
}

构造方法:初始化成员变量,属性,调用方法等

namespace ConsoleApp2
{
    class student
    {

        public student()
        {
             
        }
        public student(string name)
        {
            this.name = name;
        }
       public student(string name,int age):this(name)
        {
            this.age = age;
        }
        private string name = "苏苏";
        private int age=20;
        public int Age
        {
            get { return age; }
            set { age = value; }
        }

        public int MyProperty { get; set; } = 20;

    }
}