C#中的属性get set value

C#语言在面向对象设计和编程中对数据安全提出了严格的要求,其中一个重要的原则就是数据封装。根据这一原则,C#程序设计中要求开发人员对特定类的数据字段尽量不以公有方式提供给外界。因此在类内部多数字段的访问权限被限定为private或是public,而这些字段与外界的交流经常采用属性来进行。

属性使类能够以一种公开的方法获取和设置值,同时隐藏实现或验证代码。

属性是这样的成员:它们提供灵活的机制来读取、编写或计算私有字段的值。

可以像使用公共数据成员一样使用属性,但实际上它们是称作“访问器”的特殊方法。这 使得可以轻松访问数据,此外还有助于提高方法的安全性和灵活性。

属性使类能够以一种公开的方法获取和设置值,同时隐藏实现或验证代码。

get 属性访问器用于返回属性值,而 set 访问器用于分配新值。 这些访问器可以有不同的访问级别。

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

get 访问器必须以 return 或 throw 语句终止,并且控制权不能离开访问器体。

value 关键字用于定义由 set 取值函数分配的值。

不实现 set 取值函数的属性是只读的。

不实现 set 取值函数的属性是只读的。

属性的定义通常由以下两部分组成: 
1需要封装的专用数据成员

private int _nValue = 1;
private double _dValue = 10.101;
private char _chValue = 'a';
  • 1
  • 2
  • 3

2向外界提供访问的公共属性:

//读写属性nValue:
public int nValue
{
    get
    {
        return _nValue;
    }
    set
    {
        _nValue = value;
    }   
}
//只读属性dValue:
public double dValue
{
    get
    {
        return _dValue;
    }
}
//只写属性chValue:
public char chValue
{
    set
    {
        _chValue = value;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

当属性的访问器中不需要其他逻辑时,自动实现的属性可使属性声明更加简洁。客户端还可以通过这些属性创建对象,例如下面的代码,编译器将创建一个私有的匿名支持字段,该字段只能通过属性的get和set访问器进行访问。

class Customer
{   // Auto-Impl Properties for trivial get and set
    public double TotalPurchases { get; set; }
    public string Name { get; set; }
    public int CustomerID { get; set; }
    // Constructor
    public Customer(double purchases, string name, int ID)
    {
        TotalPurchases = purchases;
        Name = name;
        CustomerID = ID;
    }
    // Methods
    public string GetContactInfo() {return "ContactInfo";}
    public string GetTransactionHistory() {return "History";}
    // .. Additional methods, events, etc.
}
class Program
{
    static void Main()
    {
        // Intialize a new object.
        Customer cust1 = new Customer ( 4987.63, "Northwind",90108 );
        //Modify a property
        cust1.TotalPurchases += 499.99;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

下面讲一个如何使用自动实现的属性实现轻量类: 
此示例演示如何创建一个不可变轻量类,用于仅封装一组自动实现的属性。当您必须使用引用类型语义时,请使用此种构造而不是结构。 
请注意:对于自动实现的属性,需要 get 和 set 访问器。 要使该类不可变,请将 set 访问器声明为 private。 但是,声明私有 set 访问器时,不能使用对象初始值来初始化属性。 
下面的示例演示两种方法来实现具有自动实现属性的不可变类。第一个类使用构造函数初始化属性,第二个类使用静态工厂方法。

class Contact
      {
          // Read-only properties.

          public string Name { get; private set; }
          public string Address { get; private set; }

          // Public constructor.

          public Contact(string contactName, string contactAddress)
          {
              Name = contactName;
              Address = contactAddress;               
          }
      }

      // This class is immutable. After an object is created,

      // it cannot be modified from outside the class. It uses a

      // static method and private constructor to initialize its properties.   

      public class Contact2
      {
          // Read-only properties.

          public string Name { get; private set; }
          public string Address { get; private set; }

          // Private constructor.

          private Contact2(string contactName, string contactAddress)
          {
              Name = contactName;
              Address = contactAddress;               
          }

          // Public factory method.

          public static Contact2 CreateContact(string name, string address)
          {
              return new Contact2(name, address);
          }
      }

      public class Program
      { 
          static void Main()
          {
              // Some simple data sources.

              string[] names = {"Terry Adams","Fadi Fakhouri", "Hanying Feng", 
                                "Cesar Garcia", "Debra Garcia"};
              string[] addresses = {"123 Main St.", "345 Cypress Ave.", "678 1st Ave",
                                    "12 108th St.", "89 E. 42nd St."};

              // Simple query to demonstrate object creation in select clause.

              // Create Contact objects by using a constructor.

              var query1 = from i in Enumerable.Range(0, 5)
                          select new Contact(names[i], addresses[i]);

              // List elements cannot be modified by client code.

              var list = query1.ToList();
              foreach (var contact in list)
              {
                  Console.WriteLine("{0}, {1}", contact.Name, contact.Address);
              }

              // Create Contact2 objects by using a static factory method.

              var query2 = from i in Enumerable.Range(0, 5)
                           select Contact2.CreateContact(names[i], addresses[i]);

              // Console output is identical to query1.

              var list2 = query2.ToList();

              // List elements cannot be modified by client code.

              // CS0272:

              // list2[0].Name = "Eugene Zabokritski"; 


              // Keep the console open in debug mode.

              Console.WriteLine("Press any key to exit.");
              Console.ReadKey();                
          }
      }

  /* Output:
    Terry Adams, 123 Main St.
    Fadi Fakhouri, 345 Cypress Ave.
    Hanying Feng, 678 1st Ave
    Cesar Garcia, 12 108th St.
    Debra Garcia, 89 E. 42nd St.
*/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101

上诉中,通过get存取器和set存取器将封装好的专有数据成员和共同属性关联起来。

此时,value关键字是时候出场了。 
在普通的C#程序中,一般不能通过常用的调试手段获得value值传递的详细过程,不能像C++中一样跟踪指针的变化情况。当使用如下语句给属性赋值:

Class ValueCollector{...};
ValueCollector newValue = new ValueCollector();
newValue.nValue = 10;
  • 1
  • 2
  • 3

新对象newValue的私有数据成员_nValue通过属性nValue的set方法由原来的1改变为10; 
赋值语句的右值通过隐式参数value进入属性set方法内,成功改变整型私有变量的值。

在这一过程中,value参数的类型是整型,与属性的类型是一致的。当属性的类型改变为char,value参数的属性也相应的改变为字符型。

这种参数类型的自动转换时基于.NETFramework提供的类型转换器而实现的,CLR将C#源代码编译成中间语言IL,在这种类汇编的高级机器语言中可以发现value参数的传递机制。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值