C#属性
本節(jié)課將介紹C#的屬性,其目的包括:
1.理解什么是屬性
2.如何實(shí)現(xiàn)屬性
3.創(chuàng)建一個(gè)只讀屬性
4.創(chuàng)建一個(gè)只寫(xiě)屬性
屬性是C#中獨(dú)具特色的新功能。通過(guò)屬性來(lái)讀寫(xiě)類中的域,這具有一定的保護(hù)功能。在其它語(yǔ)言中,這是通過(guò)實(shí)現(xiàn)特定的getter和setter方法來(lái)實(shí)現(xiàn)的。C#的屬性具有保護(hù)功能,可以讓你就象訪問(wèn)域一樣訪問(wèn)屬性。要了解屬性的用法,我們先來(lái)看看如何用傳統(tǒng)的方法對(duì)域進(jìn)行封裝。
1.清單 10-1. 傳統(tǒng)的訪問(wèn)類的域的例子: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;
}
}
說(shuō)明
1.清單 10-1 演示了用傳統(tǒng)方法訪問(wèn)類的域的例子。
PropertyHolder類有個(gè)我們感興趣的域someProperty, PropertyHolder類帶有兩個(gè)方法:getSomeProperty和setSomeProperty。getSomeProperty方法返回someProperty域的值。SetSomeProperty方法設(shè)置域someProperty的值。
2.類PropertyTester使用類PropertyHolder中的方法來(lái)獲取someProperty域的值。
Main方法中新創(chuàng)建了一個(gè)PropertyHolder對(duì)象,之后通過(guò)使用setSomeProperty方法,調(diào)用propHold對(duì)象的setSomeProperty方法,設(shè)置其值為5。之后,調(diào)用Console.WriteLine方法輸出屬性值。對(duì)propHold對(duì)象的getSomeProperty的調(diào)用,是用來(lái)獲取屬性值的。它輸出"Property Value: 5"到控制臺(tái)。
3.這種傳統(tǒng)的訪問(wèn)域的信息的方法是很好的,因?yàn)樗С置嫦驅(qū)ο蟮姆庋b的概念。
如果在對(duì)域someProperty的實(shí)現(xiàn)中,域的類型從int 類型變?yōu)閎yte類型,上述的方法仍然適用。現(xiàn)在,如果采用屬性的話,其實(shí)現(xiàn)會(huì)做得更為平滑。
2.清單 10-2. 使用屬性訪問(wèn)類的域: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;
}
}
說(shuō)明
1.清單 10-2 演示了如何創(chuàng)建和使用屬性。
PropertyHolder類中有個(gè)"SomeProperty" 屬性的實(shí)現(xiàn)。注意:屬性名的首字母必須大寫(xiě),這是屬性名"SomeProperty"和域名"someProperty"的唯一區(qū)別。屬性有兩種訪問(wèn)操作:get和set。Get訪問(wèn)操作返回的是someProperty域的值。Set訪問(wèn)操作是設(shè)置someProperty域的值,其值為"value"的內(nèi)容。Set訪問(wèn)符號(hào)后面的"value"是C#中的保留字。通常,在其他場(chǎng)合下使用"value"關(guān)鍵字會(huì)出錯(cuò)。。
2.PropertyTester 類使用PropertyHolder類中的SomeProperty屬性。
在Main方法的第一行中, 創(chuàng)建了PropertyHolder對(duì)象propHold。之后,把propHold對(duì)象的 someProperty 域的值設(shè)置為5,很簡(jiǎn)單,就象對(duì)域賦值一樣,給屬性賦值。
3.Console.WriteLine方法輸出 propHold對(duì)象的someProperty域的值。
這是通過(guò)使用propHold對(duì)象的SomeProperty屬性來(lái)完成的。很簡(jiǎn)單,就象對(duì)域賦值一樣,賦值給屬性。屬性可以設(shè)置為只讀的,這可以通過(guò)在屬性的實(shí)現(xiàn)中只設(shè)一個(gè)get訪問(wèn)符號(hào)來(lái)實(shí)現(xiàn)。
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;
}
}
說(shuō)明
1.清單10-3 演示了如何實(shí)現(xiàn)只讀屬性。
PropertyHolder類中,SomeProperty 屬性只有一個(gè)get訪問(wèn)操作,沒(méi)有用到set訪問(wèn)操作。PropertyHolder類中還有個(gè)接受整型參數(shù)的構(gòu)造函數(shù)。
2.在PropertyTester類的Main方法中,創(chuàng)建了新名為propHold的PropertyHolder類的對(duì)象。
propHold對(duì)象在實(shí)例化時(shí),調(diào)用了帶參數(shù)的PropertyHolder構(gòu)造函數(shù)。在本例中,參數(shù)值為5,這對(duì)propHold 對(duì)象的someProperty域的值進(jìn)行了初始化。
3.因?yàn)镻ropertyHolder 類的SomeProperty屬性是只讀的,所以沒(méi)有其他的方法來(lái)設(shè)置someProperty域的值。
如果你插入了"propHold.SomeProperty = 7"語(yǔ)句到程序清單中,該程序編譯將不會(huì)通過(guò),因?yàn)镾omeProperty是只讀屬性。在Console.WriteLine 方法中使用SomeProperty屬性時(shí),程序執(zhí)行正常。這是因?yàn)樵摲椒ㄕ{(diào)用了SomeProperty屬性的get訪問(wèn)操作,這是個(gè)只讀操作。
4.清單 10-4. 只寫(xiě)屬性: 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;
}
}
說(shuō)明
1.清單 10-4 演示了如何創(chuàng)建和使用只寫(xiě)屬性。
這一次,在PropertyHolder類中的SomeProperty屬性中,去掉了get訪問(wèn)操作,而加上了set訪問(wèn)操作。其功能是輸出someProperty域的值。
2.在PropertyTester 類中的Main方法中,用缺省的構(gòu)造函數(shù)對(duì)PropertyTester類進(jìn)行初始化。
之后,使用propHold 對(duì)象的SomeProperty屬性,設(shè)置該域的值為5。這就調(diào)用了propHold 對(duì)象的set訪問(wèn)操作, 把someProperty 域的值設(shè)置為5,最后,把"someProperty is equal to 5"的信息輸出到控制臺(tái)。
小結(jié)
現(xiàn)在,你已經(jīng)了解了什么是屬性,以及屬性的使用方法,你也了解了使用屬性和使用傳統(tǒng)的類的方法之間的區(qū)別。屬性可以是只讀的,也可以是只寫(xiě)的,每種場(chǎng)合下的使用方法,你都有所了解。
?
1.理解什么是屬性
2.如何實(shí)現(xiàn)屬性
3.創(chuàng)建一個(gè)只讀屬性
4.創(chuàng)建一個(gè)只寫(xiě)屬性
屬性是C#中獨(dú)具特色的新功能。通過(guò)屬性來(lái)讀寫(xiě)類中的域,這具有一定的保護(hù)功能。在其它語(yǔ)言中,這是通過(guò)實(shí)現(xiàn)特定的getter和setter方法來(lái)實(shí)現(xiàn)的。C#的屬性具有保護(hù)功能,可以讓你就象訪問(wèn)域一樣訪問(wèn)屬性。要了解屬性的用法,我們先來(lái)看看如何用傳統(tǒng)的方法對(duì)域進(jìn)行封裝。
1.清單 10-1. 傳統(tǒng)的訪問(wèn)類的域的例子: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;
}
}
說(shuō)明
1.清單 10-1 演示了用傳統(tǒng)方法訪問(wèn)類的域的例子。
PropertyHolder類有個(gè)我們感興趣的域someProperty, PropertyHolder類帶有兩個(gè)方法:getSomeProperty和setSomeProperty。getSomeProperty方法返回someProperty域的值。SetSomeProperty方法設(shè)置域someProperty的值。
2.類PropertyTester使用類PropertyHolder中的方法來(lái)獲取someProperty域的值。
Main方法中新創(chuàng)建了一個(gè)PropertyHolder對(duì)象,之后通過(guò)使用setSomeProperty方法,調(diào)用propHold對(duì)象的setSomeProperty方法,設(shè)置其值為5。之后,調(diào)用Console.WriteLine方法輸出屬性值。對(duì)propHold對(duì)象的getSomeProperty的調(diào)用,是用來(lái)獲取屬性值的。它輸出"Property Value: 5"到控制臺(tái)。
3.這種傳統(tǒng)的訪問(wèn)域的信息的方法是很好的,因?yàn)樗С置嫦驅(qū)ο蟮姆庋b的概念。
如果在對(duì)域someProperty的實(shí)現(xiàn)中,域的類型從int 類型變?yōu)閎yte類型,上述的方法仍然適用。現(xiàn)在,如果采用屬性的話,其實(shí)現(xiàn)會(huì)做得更為平滑。
2.清單 10-2. 使用屬性訪問(wèn)類的域: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;
}
}
說(shuō)明
1.清單 10-2 演示了如何創(chuàng)建和使用屬性。
PropertyHolder類中有個(gè)"SomeProperty" 屬性的實(shí)現(xiàn)。注意:屬性名的首字母必須大寫(xiě),這是屬性名"SomeProperty"和域名"someProperty"的唯一區(qū)別。屬性有兩種訪問(wèn)操作:get和set。Get訪問(wèn)操作返回的是someProperty域的值。Set訪問(wèn)操作是設(shè)置someProperty域的值,其值為"value"的內(nèi)容。Set訪問(wèn)符號(hào)后面的"value"是C#中的保留字。通常,在其他場(chǎng)合下使用"value"關(guān)鍵字會(huì)出錯(cuò)。。
2.PropertyTester 類使用PropertyHolder類中的SomeProperty屬性。
在Main方法的第一行中, 創(chuàng)建了PropertyHolder對(duì)象propHold。之后,把propHold對(duì)象的 someProperty 域的值設(shè)置為5,很簡(jiǎn)單,就象對(duì)域賦值一樣,給屬性賦值。
3.Console.WriteLine方法輸出 propHold對(duì)象的someProperty域的值。
這是通過(guò)使用propHold對(duì)象的SomeProperty屬性來(lái)完成的。很簡(jiǎn)單,就象對(duì)域賦值一樣,賦值給屬性。屬性可以設(shè)置為只讀的,這可以通過(guò)在屬性的實(shí)現(xiàn)中只設(shè)一個(gè)get訪問(wèn)符號(hào)來(lái)實(shí)現(xiàn)。
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;
}
}
說(shuō)明
1.清單10-3 演示了如何實(shí)現(xiàn)只讀屬性。
PropertyHolder類中,SomeProperty 屬性只有一個(gè)get訪問(wèn)操作,沒(méi)有用到set訪問(wèn)操作。PropertyHolder類中還有個(gè)接受整型參數(shù)的構(gòu)造函數(shù)。
2.在PropertyTester類的Main方法中,創(chuàng)建了新名為propHold的PropertyHolder類的對(duì)象。
propHold對(duì)象在實(shí)例化時(shí),調(diào)用了帶參數(shù)的PropertyHolder構(gòu)造函數(shù)。在本例中,參數(shù)值為5,這對(duì)propHold 對(duì)象的someProperty域的值進(jìn)行了初始化。
3.因?yàn)镻ropertyHolder 類的SomeProperty屬性是只讀的,所以沒(méi)有其他的方法來(lái)設(shè)置someProperty域的值。
如果你插入了"propHold.SomeProperty = 7"語(yǔ)句到程序清單中,該程序編譯將不會(huì)通過(guò),因?yàn)镾omeProperty是只讀屬性。在Console.WriteLine 方法中使用SomeProperty屬性時(shí),程序執(zhí)行正常。這是因?yàn)樵摲椒ㄕ{(diào)用了SomeProperty屬性的get訪問(wèn)操作,這是個(gè)只讀操作。
4.清單 10-4. 只寫(xiě)屬性: 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;
}
}
說(shuō)明
1.清單 10-4 演示了如何創(chuàng)建和使用只寫(xiě)屬性。
這一次,在PropertyHolder類中的SomeProperty屬性中,去掉了get訪問(wèn)操作,而加上了set訪問(wèn)操作。其功能是輸出someProperty域的值。
2.在PropertyTester 類中的Main方法中,用缺省的構(gòu)造函數(shù)對(duì)PropertyTester類進(jìn)行初始化。
之后,使用propHold 對(duì)象的SomeProperty屬性,設(shè)置該域的值為5。這就調(diào)用了propHold 對(duì)象的set訪問(wèn)操作, 把someProperty 域的值設(shè)置為5,最后,把"someProperty is equal to 5"的信息輸出到控制臺(tái)。
小結(jié)
現(xiàn)在,你已經(jīng)了解了什么是屬性,以及屬性的使用方法,你也了解了使用屬性和使用傳統(tǒng)的類的方法之間的區(qū)別。屬性可以是只讀的,也可以是只寫(xiě)的,每種場(chǎng)合下的使用方法,你都有所了解。
?
總結(jié)
- 上一篇: 求猪蹄的高压锅做法????
- 下一篇: “鸟解高飞岂触罗”上一句是什么