C#教程第十課:屬性

來源:互聯網
上載者:User
教程 本節課將介紹C#的屬性,其目的包括:
1.理解什麼是屬性

2.如何?屬性

3.建立一個唯讀屬性

4.建立一個唯寫屬性

屬性是C#中獨具特色的新功能。通過屬性來讀寫類中的域,這具有一定的保護功能。在其它語言中,這是通過實現特定的getter和setter方法來實現的。C#的屬性具有保護功能,可以讓你就象訪問域一樣訪問屬性。要瞭解屬性的用法,我們先來看看如何用傳統的方法對域進行封裝。

1.清單 10-1. 傳統的訪問類的域的例子:Accessors.cs

using System;
public class PropertyHolder
{
private int someProperty = 0;
public int getSomeProperty()
{
return someProperty;
}
public void setSomeProperty(int propValue)
{
someProperty = propValue;
}
}

public class PropertyTester
{
public static int Main(string[] args)
{
PropertyHolder propHold = new PropertyHolder();
propHold.setSomeProperty(5);
Console.WriteLine("Property Value: {0}", propHold.getSomeProperty());
return 0;
}
}

說明

1.清單 10-1 示範了用傳統方法訪問類的域的例子。

PropertyHolder類有個我們感興趣的域someProperty, PropertyHolder類帶有兩個方法:getSomeProperty和setSomeProperty。getSomeProperty方法返回someProperty域的值。SetSomeProperty方法設定域someProperty的值。

2.類PropertyTester使用類PropertyHolder中的方法來擷取someProperty域的值。

Main方法中新建立了一個PropertyHolder對象,之後通過使用setSomeProperty方法,調用propHold對象的setSomeProperty方法,設定其值為5。之後,調用Console.WriteLine方法輸出屬性值。對propHold對象的getSomeProperty的調用,是用來擷取屬性值的。它輸出"Property Value: 5"到控制台。

3.這種傳統的訪問域的資訊的方法是很好的,因為它支援物件導向的封裝的概念。

如果在對域someProperty的實現中,域的類型從int 類型變為byte類型,上述的方法仍然適用。現在,如果採用屬性的話,其實現會做得更為平滑。

2.清單 10-2. 使用屬性訪問類的域:Properties.cs

using System;
public class PropertyHolder
{
private int someProperty = 0;
public int SomeProperty
{
get
{
return someProperty;
}
set
{
someProperty = value;
}
}
}

public class PropertyTester
{
public static int Main(string[] args)
{
PropertyHolder propHold = new PropertyHolder();
propHold.SomeProperty = 5;
Console.WriteLine("Property Value: {0}", propHold.SomeProperty);
return 0;
}
}

說明

1.清單 10-2 示範了如何建立和使用屬性。

PropertyHolder類中有個"SomeProperty" 屬性的實現。注意:屬性名稱的首字母必須大寫,這是屬性名稱"SomeProperty"和網域名稱"someProperty"的唯一區別。屬性有兩種訪問操作:get和set。Get訪問操作返回的是someProperty域的值。Set訪問操作是設定someProperty域的值,其值為"value"的內容。Set訪問符號後面的"value"是C#中的保留字。通常,在其他場合下使用"value"關鍵字會出錯。。

2.PropertyTester 類使用PropertyHolder類中的SomeProperty屬性。

在Main方法的第一行中, 建立了PropertyHolder對象propHold。之後,把propHold對象的 someProperty 域的值設定為5,很簡單,就象對域賦值一樣,給屬性賦值。

3.Console.WriteLine方法輸出 propHold對象的someProperty域的值。

這是通過使用propHold對象的SomeProperty屬性來完成的。很簡單,就象對域賦值一樣,賦值給屬性。屬性可以設定為唯讀,這可以通過在屬性的實現中只設一個get訪問符號來實現。

3.清單 10-3. 唯讀屬性: ReadOnlyProperty.cs

using System;
public class PropertyHolder
{
private int someProperty = 0;
public PropertyHolder(int propVal)
{
someProperty = propVal;
}
public int SomeProperty
{
get
{
return someProperty;
}
}
}

public class PropertyTester
{
public static int Main(string[] args)
{
PropertyHolder propHold = new PropertyHolder(5);
Console.WriteLine("Property Value: {0}", propHold.SomeProperty);
return 0;
}
}

說明

1.清單10-3 示範了如何?唯讀屬性。

PropertyHolder類中,SomeProperty 屬性只有一個get訪問操作,沒有用到set訪問操作。PropertyHolder類中還有個接受整型參數的建構函式。

2.在PropertyTester類的Main方法中,建立了新名為propHold的PropertyHolder類的對象。

propHold對象在執行個體化時,調用了帶參數的PropertyHolder建構函式。在本例中,參數值為5,這對propHold 對象的someProperty域的值進行了初始化。

3.因為PropertyHolder 類的SomeProperty屬性是唯讀,所以沒有其他的方法來設定someProperty域的值。

如果你插入了"propHold.SomeProperty = 7"語句到程式清單中,該程式編譯將不會通過,因為SomeProperty是唯讀屬性。在Console.WriteLine 方法中使用SomeProperty屬性時,程式執行正常。這是因為該方法調用了SomeProperty屬性的get訪問操作,這是個唯讀操作。

4.清單 10-4. 唯寫屬性: WriteOnlyProperty.cs

using System;
public class PropertyHolder
{
private int someProperty = 0;
public int SomeProperty
{
set
{
someProperty = value;
Console.WriteLine("someProperty is equal to {0}", someProperty);
}
}
}

public class PropertyTester
{
public static int Main(string[] args)
{
PropertyHolder propHold = new PropertyHolder();
propHold.SomeProperty = 5;
return 0;
}
}

說明

1.清單 10-4 示範了如何建立和使用唯寫屬性。

這一次,在PropertyHolder類中的SomeProperty屬性中,去掉了get訪問操作,而加上了set訪問操作。其功能是輸出someProperty域的值。

2.在PropertyTester 類中的Main方法中,用預設的建構函式對PropertyTester類進行初始化。

之後,使用propHold 對象的SomeProperty屬性,設定該域的值為5。這就調用了propHold 對象的set訪問操作, 把someProperty 域的值設定為5,最後,把"someProperty is equal to 5"的資訊輸出到控制台。

小結
現在,你已經瞭解了什麼是屬性,以及屬性的使用方法,你也瞭解了使用屬性和使用傳統的類的方法之間的區別。屬性可以是唯讀,也可以是唯寫的,每種場合下的使用方法,你都有所瞭解。



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.