C# 什么是属性

1.属性Aproperty)是一种用于访问对象或类型的特征的成员,特征反映了状态

2.属性是字段的自然扩展

  • 从命名上看,field更偏向于实例对象在内存中的布局,property更偏向于反映现实世界对象的特征
  • 对外:暴露数据,数据可以是存储在字段里的,也可以是动态计算出来的
  • 对内:保护字段不被非法值“污染”

3.属性由Get/Set方法对进化而来

最初的用来保护字段的Get/Set方法,如以下代码:(C++与Java语言仍然在使用Get/Set方法)

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

namespace PropertyExample
{
    internal class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Student stu1 = new Student();
                stu1.SetAge(20);//设置值
                Student stu2 = new Student();
                stu2.SetAge(20);
                Student stu3 = new Student();
                stu3.SetAge(20);
                int avgAge = (stu1.GetAge() + stu2.GetAge() + stu3.GetAge()) / 3;//获取值
                Console.WriteLine(avgAge);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }

    class Student
    {
        private int age;//私有字段,从外界不能直接访问该字段

        //用一对方法将该字段保护起来,一个方法获取值(Get),另一个为字段设置值(Set)
        public int GetAge()
        {
            return this.age;
        }
        public void SetAge(int value)
        {
            if(value>=0&&value<=120)
                {
                this.age = value;
                }
            else 
            {
                throw new Exception("Age value has error.");
            }
        }
    }
}

分析代码:

  1. age字段:这是一个私有字段,用于存储年龄的值。
  2. GetAge方法:这是一个公有方法,用于获取age字段的值。
  3. SetAge方法:这是一个公有方法,用于设置age字段的值。它还包含了一些验证逻辑,确保年龄值在合理的范围内。

而属性提供了一种更简洁的方式来访问和修改字段的值,并允许您在访问或修改字段时执行额外的操作,如验证等。

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

namespace PropertyExample
{
    internal class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Student stu1 = new Student();
                stu1.Age = 20;
                Student stu2 = new Student();
                stu2.Age = 20;
                Student stu3 = new Student();
                stu3.Age = 20;
                int avgAge = (stu1.Age + stu2.Age + stu3.Age) / 3;
                Console.WriteLine(avgAge);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }

    class Student
    {
        private int age;//私有字段,从外界不能直接访问该字段

        public int Age  //属性,首字母大写
        {
            get
            {
                return this.age;
            }
            set
            {
                //不用声明value,在特定的方法中为上下文关键字,代表传进来的值
                if (value >= 0 && value <= 120)
                {
                    this.age = value;
                }
                else
                {
                    throw new Exception("Age value has error.");
                }
            }

            
        }
    }
}

代码分析:

  • 定义了一个Student类,它包含一个私有字段age
  • 还定义了一个属性Age,它包含了getset访问器。
  • get访问器用于获取age字段的值。
  • set访问器用于设置age字段的值,并包含了一些验证逻辑,确保年龄值在合理的范围内。
  • Main方法中,我们创建了三个Student类的新实例,并通过属性Age直接设置年龄为20。
  • 接着计算了这三个学生的平均年龄,并将结果输出到了控制台。
  • 异常处理逻辑用于捕获可能发生的异常。

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值