c# 字段(field) 和 属性(property) 重要

namespace ConsoleApp1
{
    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 e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }

    class Student
    {
        private int age;

        public int GetAge()
        {
            return this.age;
        }

        public void SetAge(int value)
        {
            if (value >= 0 && value <= 120)
            {
                this.age = value;
            }
            else
            {
                throw new Exception("Age参数范围错误");
            }
        }
    }
}
namespace ConsoleApp1
{
    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 e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }

    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参数范围错误");
                }
            }
        }
    }
}

快速生成完成属性

visual Studio快速生成属性
在类中,输入propfull ,接着按2下tab

    class Student
    {
        private int myVar;

        public int MyProperty
        {
            get { return myVar; }
            set { myVar = value; }
        }
    }

简略声明

visual Studio快速生成属性
在类中,输入prop ,接着按2下tab

    class Student
    {
        public int Age { get; set; }
    }

私有set,只能在类中进行赋值

    class Student
    {
        private int age;

        public int Age
        {
            get { return age; }
            // 只能在类中进行赋值
            private set { age = value; }
        }


        public void say()
        {
            this.Age = 200;
        }
    }

练习

Age满足16岁可以工作,调用CanWork判断是否满足条件

每次访问CanWork时,计算age是否满足要求

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Student stu = new Student();
                stu.Age = 16;

                Console.WriteLine(stu.CanWork);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }

    class Student
    {
        private int age;

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

        public bool CanWork
        {
            get
            {
                if (this.age >= 16)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
}

在setAge时,计算age是否满足要求

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Student stu = new Student();
                stu.Age = 16;

                Console.WriteLine(stu.CanWork);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }

    class Student
    {
        private int age;

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

                // 每次赋值后都调用CalculateCanWork()进行判断
                this.CalculateCanWork();
            }
        }

        private bool canWork;

        public bool CanWork
        {
            get
            {
                return canWork;
            }
        }

        private void CalculateCanWork()
        {
            if (this.age >= 16)
            {
                this.canWork = true;
            }
            else
            {
                this.canWork = false;
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值