关于{Get;Set;}的使用

在C#中可以自定义变量,可以定义public、private、protected等类型,那为什么要用Get和Set去定义属性呢,贴上下面的代码略作讲解。
    public class CG
    {
       protected string _cource;
       protected int _grade;
       public CG(string cource, int grade)
       {
           _cource = cource;
           _grade = grade;
       }
       public CG(int grade)
       {
           _grade = grade;
       }
       public int Grade
       {
           get
           {
               switch (_grade / 10)
               {
                   case 9: return 1;  //90分以上为1等
                   case 8: return 2;  //80分以上,90以下为2等
                   case 7: return 3;  //70分以上,80分以下为3等
                   case 6: return 4;  //60分以上,70分以下为4等
                   default: return 5;  //60分以下为5等
               }
           }
       }

       public string Cource
       {
           get
           {
               if (_cource == null)
               {
                   return "平均分";
               }
               else
               {
                   return _cource;
               }
           }
           set
           {
               if (value == "")
               {
                   _cource = "平均分";
               }
               else
               {
                   _cource = value;
               }
           }
       }
       public object _obj
       {
           get;
           set;
       }
    }

不知通过以上例子,你是否想通get和set的意义。
(1)变量取值和设置值,只能给它什么就是什么,一点逻辑判断也没有,而我们可以通过get和set在内部做一些处理,过滤掉一些不合理的数据,也减少异常的发生。
(2)有时,我们并不想要原模原样的变量值,可能需要对它加工的数据,如以上Grade方法,我们可以用get,当然,这种加工后的数据,是不允许用户人为改变的(是通过算法算出来的),所以不能开放给他set方法。

总之,有了get和set方法,我们调用起来才能更安全,否则,private和protected就没有意义了,因为在类我外部(包括对象调用)都无法调用到private和protected属性。如果把属性都定义成pubic,就太不安全了。



二、其他解释

属性的访问器包含与获取(读取或计算)或设置(写)属性有关的可执行语句。访问器声明可以包含 get 访问器或 set 访问器,或者两者均包含。声明采用下列形式之一:

get {}

set {}

get 访问器

get 访问器体与方法体相似。它必须返回属性类型的值。执行 get 访问器相当于读取字段的值。以下是返回私有字段 name 的值的 get 访问器:

private string name;    // the name field
public string Name    // the Name property
{
    get
    {
       return name;
    }
}

当引用属性时,除非该属性为赋值目标,否则将调用 get 访问器读取该属性的值。例如:

Employee e1 = new Employee();
...
Console.Write(e1.Name);    // The get accessor is invoked here

get 访问器必须在 returnthrow 语句中终止,并且控制不能超出访问器体。

set 访问器

set 访问器与返回 void 的方法类似。它使用称为 value 的隐式参数,此参数的类型是属性的类型。在下例中,set 访问器被添加到 Name 属性:

public string Name
{
    get
    {
       return name;
    }
    set
    {
       name = value;
    }
}

当对属性赋值时,用提供新值的参数调用 set 访问器。例如:

e1.Name = "Joe";    // The set accessor is invoked here

set 访问器中对局部变量声明使用隐式参数名 (value) 是错误的。

备注

属性按如下方式,根据所使用的访问器进行分类:

  • 只带有 get 访问器的属性称为只读属性。无法对只读属性赋值。
  • 只带有 set 访问器的属性称为只写属性。只写属性除作为赋值的目标外,无法对其进行引用。
  • 同时带有 getset 访问器的属性为读写属性。

在属性声明中,getset 访问器都必须在属性体的内部声明。

使用 get 访问器更改对象的状态是一种错误的编程样式。例如,以下访问器在每次访问 number 字段时都产生更改对象状态的副作用。

public int Number
{
    get
    {
       return number++;    // Don't do this
    }
}

可以将 get 访问器用于返回字段值,或用于计算字段值并将其返回。例如:

public string Name
{
    get
    {
       return name != null ? name : "NA";
    }
}

在上述代码段中,如果不对 Name 属性赋值,它将返回值 NA

示例 1
此例说明如何访问基类中被派生类中具有同一名称的另一个属性隐藏的属性。
// property_hiding.cs
// Property hiding
using System;
public class BaseClass
{
    private string name;
    public string Name
    {
       get
       {
          return name;
       }
       set
       {
          name = value;
       }
    }
}

public class DerivedClass : BaseClass
{
    private string name;
    public new string Name    // Notice the use of the new modifier
    {
       get
       {
          return name;
       }
       set
       {
          name = value;
       }
    }
}

public class MainClass
{
    public static void Main()
    {
       DerivedClass d1 = new DerivedClass();
       d1.Name = "John";   // Derived class property
       Console.WriteLine("Name in the derived class is: {0}",d1.Name);
       ((BaseClass)d1).Name = "Mary"; // Base class property
       Console.WriteLine("Name in the base class is: {0}",
          ((BaseClass)d1).Name);   
    }
}
输出
Name in the derived class is: John
Name in the base class is: Mary
以下是上例中显示的重点:
派生类中的属性 Name 隐藏基类中的属性 Name。在这种情况下,派生类的该属性声明使用 new 修饰符:
    public new string Name
    {
    ...
转换 (BaseClass) 用于访问基类中的隐藏属性:
    ((BaseClass)d1).Name = "Mary";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值