声明了属性后,可像使用类的字段那样使用这些属性。

 

None.gif private   int  myVar;
None.gif
None.gif
public   int  MyProperty
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gifreturn myVar;}
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ myVar = value;}
ExpandedBlockEnd.gif}

这使得获取和设置属性值时都可以使用非常自然的语法,如以下语句中所示:

None.gif person.Name  =   " Joe " ;
None.gifperson.Age 
=   99 ;
None.gif
None.gif
None.gif
None.gif
class  Person
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
private string m_name = "N/A";
InBlock.gif
private int m_Age = 0;
InBlock.gif
InBlock.gif
// Declare a Name property of type string:
InBlock.gif
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
get
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return m_name;
ExpandedSubBlockEnd.gif}

InBlock.gif
set
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifm_name 
= value;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
// Declare an Age property of type int:
InBlock.gif
public int Age
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
get
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return m_Age;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
set
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifm_Age 
= value;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
public override string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return "Name = " + Name + ", Age = " + Age;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
class  TestPerson
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
// Create a new Person object:
InBlock.gif
Person person = new Person();
InBlock.gif
InBlock.gif
// Print out the name and the age associated with the person:
InBlock.gif
System.Console.WriteLine("Person details - {0}", person);
InBlock.gif
InBlock.gif
// Set some values on the person object:
InBlock.gif
person.Name = "Joe"
InBlock.gifperson.Age 
= 99;
InBlock.gifSystem.Console.WriteLine(
"Person details - {0}", person);
InBlock.gif
InBlock.gif
// Increment the Age property:
InBlock.gif
person.Age += 1;
InBlock.gifSystem.Console.WriteLine(
"Person details - {0}", person);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*输出
InBlock.gifPerson details - Name = N/A, Age = 0
InBlock.gifPerson details - Name = Joe, Age = 99
InBlock.gifPerson details - Name = Joe, Age = 100 
ExpandedBlockEnd.gif
*/

None.gif

 

转载于:https://www.cnblogs.com/jiels/archive/2006/06/20/430851.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值