C#字段与属性

字段

1. 定义:

(1) 字段(成员变量)是类成员之一,是一种表示与对象或类型关联的变量

(2) 字段如果没有赋值,其默认值和字段名的数据类型有关,也可以给字段赋值,字段分为实例字段静态字段 

2. 实例字段:依靠类的对象来调用,且每个对象都会有一个独立的实例

    internal class Program 
    {
        static void Main(string[] args)
        {
            // 实例字段调用:
            // new初始化类,创建类的具体对象
            Car baoma = new Car();
            baoma.carName = "宝马";
        }
    }

    internal class Car
    {
        public string carName;
    }

3. 静态字段:一般情况下,静态字段属于独有的不会改变的量,可以先赋值,用类名来调用

    internal class Program 
    {
        static void Main(string[] args)
        {
            // 静态字段调用:类名调用
            People.name = "张三";
        }
    }

    internal class People
    {
        public static string name;
    }

4. 字段的只读特性

    internal class Program 
    {
        static void Main(string[] args)
        {
            // 由于字段是只读的,所以不能修改,只能输出
            // People.name = "李四";

            Console.WriteLine($"{People.name}");
        }
    }

    internal class People
    {
        public readonly static string name = "张三";
    }

5. 私有字段:
    私有字段的好处:隐藏类内部的细节,防止外部随意修改,对类是一种保护

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

    internal class People
    {
        private static string name; // 定义个私有字段
    }

属性:

6. 私有字段的调用需要用到属性:

属性是外界访问私有字段的入口,用属性来访问类中的私有字段

value: 作为访问器中特殊的变量,用来传值

    internal class Program 
    {
        static void Main(string[] args)
        {
            People people = new People();

            // 对私有字段赋值,就是调用属性Age的set方法
            people.Age = 20;

            // 对私有字段取值,就是调用属性Age的get方法
            Console.WriteLine(people.Age);
        }
    }

    internal class People
    {
        private int  _age; // 定义个私有字段
        
        // 属性
        public int Age {
            get
            {
                return _age;
            }
            set
            {
                _age = value;
            }
        }
    }

7. 属性的扩展

(1)增加业务逻辑,如判断

    internal class Program 
    {
        static void Main(string[] args)
        {
            People people = new People();

            // 对私有字段赋值,就是调用属性Age的set方法
            people.Age = 20;

            // 对私有字段取值,就是调用属性Age的get方法
            Console.WriteLine(people.Age);
        }
    }

    internal class People
    {
        private int  _age; // 定义个私有字段
        
        // 属性
        public int Age {
            get
            {
                return _age;
            }
            set
            {
                if (value > 18)
                {
                    _age = 25;
                }
                else
                {
                    _age = value;
                }
            }
        }
    }

(2)设置只读属性功能,在私有字段创建时赋值

    internal class Program 
    {
        static void Main(string[] args)
        {
            People people = new People();

            // 属性中只有get方法,只能读取
            Console.WriteLine(people.IsRight);
        }
    }

    internal class People
    {
        private bool _isRight = true;
        
        // 属性
        public bool IsRight
        {
            get
            {
                return _isRight;
            }
        }
    }

(3)设置只读的业务逻辑

    internal class Program 
    {
        static void Main(string[] args)
        {
            People people = new People();

            // 属性中只有get方法,只能读取
            Console.WriteLine(people.IsRight);
        }
    }

    internal class People
    {

        // 属性
        public bool IsRight
        {
            get{
                    int money = 4000;
                    if (money >= 4000)
                    {
                        string tempStr = $"恭喜你有女朋友了";
                        return tempStr;
                    }
                    else
                    {
                        return "加油单身狗";
                    }
            }
        }
    }

(4)对象类型的属性使用(只读)

     internal class Program 
    {
        static void Main(string[] args)
        {
            People people = new People();

            People.intArr = new int[3] {22,222,2222};
        }
    }

    internal class People
    {

        // 属性
        public int[] intArr { get; set; }
    }

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值