C#--属性三两事

在介绍属性之前,我想问问各位有经验的大牛,属性的作用这么多,被外部访问的字段是不是都需要将它设为属性?


C#中的属性,体现了C#作为面向对象语言的封装性。它避免了外部类字段的直接访问、使得代码的安全性得到了加强。

属性与字段的区别:
     属性是逻辑字段;属性是字段的扩展,源于字段;
     属性并不占用实际的内存,字段占内存位置及空间。
     属性可以被其它类访问,而大部分字段不能直接访问。
     属性可以对接收的数据范围作限定,而字段不能。
     
最直接的说:属性是被“外部使用”,字段是被“内部使用”。

下面来看看,属性这种看起来麻烦的东西,到底能够带来什么样的好处:     

1、可以提供get-only或者set-only版本,甚至可以给读、写以不同的访问权限(C# 2.0支持)

2、可以对赋值做校验、或者额外的处理

3、可以做线程同步

4、可以将属性置于interface中

5、可以使用虚属性、或者抽象属性



下面来看最简单的例子:
class Person
    {
        private string sex;

        public string Sex
        {
            get { return sex; }
            set { sex = value; }
        }

    }

可以通过使用以下代码操作:
            Person x = new Person();
            x .Sex = "boy";
            Console.WriteLine( "sex = {0}", x. Sex);     //sex = boy

显然,get为写操作,set为读操作,当然可以设置为只读,只写属性。
当然,正常情况下,sex 的值只有boy跟girl,所以输入的其他情况均为异常情况,需避免。属性的作用之一就是可以提供这种约束:
class Person
    {
        private string sex;

        public string Sex
        {
            get { return sex; }
            set
            {
                if (value != "boy" && value != "girl" )
                {
                    Console.WriteLine( "sex  输入异常!!" );
                }
                else
                { this.sex = value ;  }
            }
        }
    }

               Person x = new Person ();
            x . Sex = "boyORgirl";
            Console .WriteLine( "sex = {0}" , x. Sex);     //sex  输入异常!!  

接口属性:
interface IEmployee
{
    string Name
    {
        get;
        set;
    }

    int Counter
    {
        get;
    }
}

public class Employee : IEmployee
{
    public static int numberOfEmployees;

    private string name;
    public string Name  // read-write instance property
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }

    private int counter;
    public int Counter  // read-only instance property
    {
        get
        {
            return counter;
        }
    }

    public Employee()  // constructor
    {
        counter = ++counter + numberOfEmployees;
    }
}

class TestEmployee
{
    static void Main()
    {
        System.Console.Write("Enter number of employees: ");
        Employee.numberOfEmployees = int.Parse(System.Console.ReadLine());

        Employee e1 = new Employee();
        System.Console.Write("Enter the name of the new employee: ");
        e1.Name = System.Console.ReadLine();

        System.Console.WriteLine("The employee information:");
        System.Console.WriteLine("Employee number: {0}", e1.Counter);
        System.Console.WriteLine("Employee name: {0}", e1.Name);
    }
}
示例输出:

Enter number of employees: 210

Enter the name of the new employee: Hazem Abolrous

The employee information:

Employee number: 211

Employee name: Hazem Abolrous




下面讨论一下虚属性和抽象属性(个人认为的属性的最大优点):

虚属性:
    public class Base
    {
        public virtual int Count       //虚属性
        {
            get { return 1; }
        }
        public int Compute()
        {
            return Count;
        }
    }

    public class Derived : Base
    {
        public override int Count     //重载虚属性
        {
            get { return 5; }
        }
    }

Test:
            Derived b = new Derived();
            Console.WriteLine("Compute is {0}\n", b.Compute());
            Console.ReadLine(); 
Output:
           Compute is 5     

抽象属性的实现基本与上面相似,只要将Count 的限定符vitual改为abstract


看完了属性的作用,你是否觉得它很强大?你觉得它应该替代public字段吗?
或者,在你的工作中,你是如何处理考虑这个问题的?



期待你的回答,谢谢!









--------------------------------------------------------------------2012年9月13日21:47:56
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值