0
点赞
收藏
分享

微信扫一扫

springboot整合logback进行日志管理(下篇)

Separes 2024-09-03 阅读 25

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.");
            }
        }
    }
}

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

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.");
                }
            }

            
        }
    }
}

举报

相关推荐

0 条评论