不知通过以上例子,你是否想通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 访问器:
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 访问器必须在 return 或 throw 语句中终止,并且控制不能超出访问器体。
set 访问器set 访问器与返回 void 的方法类似。它使用称为 value 的隐式参数,此参数的类型是属性的类型。在下例中,set 访问器被添加到 Name
属性:
{
get
{
return name;
}
set
{
name = value;
}
}
当对属性赋值时,用提供新值的参数调用 set 访问器。例如:
e1.Name = "Joe"; // The set accessor is invoked here在 set 访问器中对局部变量声明使用隐式参数名 (value) 是错误的。
备注属性按如下方式,根据所使用的访问器进行分类:
- 只带有 get 访问器的属性称为只读属性。无法对只读属性赋值。
- 只带有 set 访问器的属性称为只写属性。只写属性除作为赋值的目标外,无法对其进行引用。
- 同时带有 get 和 set 访问器的属性为读写属性。
在属性声明中,get 和 set 访问器都必须在属性体的内部声明。
使用 get 访问器更改对象的状态是一种错误的编程样式。例如,以下访问器在每次访问 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 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";