C#类中的get 和set 函数的具体用法

None.gif C#语言有两个函数 -- 一个赋值函数( get ),一个取值函数( set ),这从它生成的中间语言代码可以清晰地看到。C#不提倡将域的保护级别设为public而使用户在类外任意操作 -- 那样太不OO,或者具体点说太不安全!对所有有必要在类外可见的域,C#推荐采用属性来表达。属性不表示存储位置,这是属性和域的根本性的区别。下面是一个典型的属性设计: 
None.gif
None.gif
using  System; 
None.gif
class  MyClass 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
int integer; 
InBlock.gif
public int Integer 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gif{return integer;} 
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{integer=value;} 
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}
 
None.gif
None.gif
class  Test 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
public static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifMyClass MyObject
=new MyClass(); 
InBlock.gifConsole.Write(MyObject.Integer); 
InBlock.gifMyObject.Integer
++
InBlock.gifConsole.Write(MyObject.Integer); 
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}
 
None.gif
None.gif一如我们期待的那样,程序输出0 
1 。我们可以看到属性通过对方法的包装向程序员提供了一个友好的域成员的存取界面。这里的value是C#的关键字,是我们进行属性操作时的set的隐含参数,也就是我们在执行属性写操作时的右值。 
None.gif
None.gif属性提供了只读(
get ),只写( set ),读写(get和  set )三种接口操作。对域的这三种操作,我们必须在同一个属性名下声明,而不可以将它们分离,看下面的实现: 
None.gif
None.gif
class  MyClass 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
private string name; 
InBlock.gif
InBlock.gif
public string Name 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gifreturn name; } 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
public string Name 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ name = value; } 
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}
 
None.gif
None.gif上面这种分离Name属性实现的方法是错误的!我们应该像前面的例子一样将他们放在一起。值得注意的是三种属性(只读,只写,读写)被C#认为是同一个属性名,看下面的例子: 
None.gif
None.gif
class  MyClass 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
protected int num=0
InBlock.gif
public int Num 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifnum
=value; 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}
 
None.gif
class  MyClassDerived: MyClass 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
new public int Num 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return num; 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}
 
None.gif
None.gif
class  Test 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
public static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifMyClassDerived MyObject 
= new MyClassDerived(); 
InBlock.gif
//MyObject.Num= 1; //错误 ! 
InBlock.gif
((MyClass)MyObject).Num = 1
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}
 
None.gif
ExpandedBlockStart.gifContractedBlock.gif我们可以看到MyClassDerived中的属性Num
- get dot.gif {} 屏蔽了MyClass中属性Num - set dot.gif {} 的定义。 
None.gif
None.gif当然属性远远不止仅仅限于域的接口操作,属性的本质还是方法,我们可以根据程序逻辑在属性的提取或赋值时进行某些检查,警告等额外操作,看下面的例子: 
None.gif
None.gif
class  MyClass 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
private string name; 
InBlock.gif
public string Name 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gifreturn name; } 
InBlock.gif
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
if (value==null
InBlock.gifname
="Microsoft"
InBlock.gif
else 
InBlock.gifname
=value; 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}
 
None.gif
None.gif由于属性的方法的本质,属性当然也有方法的种种修饰。属性也有5种存取修饰符,但属性的存取修饰往往为public,否则我们也就失去了属性作为类的公共接口的意义。除了方法的多参数带来的方法重载等特性属性不具备外, 
virtual sealed override , abstract等修饰符对属性与方法同样的行为,但由于属性在本质上被实现为两个方法,它的某些行为需要我们注意。看下面的例子: 
None.gif
None.gif
abstract   class  A 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
int y; 
InBlock.gif
public virtual int X 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gifreturn 0; } 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
public virtual int Y 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gifreturn y; } 
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ y = value; } 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockStart.gifContractedSubBlock.gif
public abstract int Z dot.gifgetset; } 
ExpandedBlockEnd.gif}
 
None.gif
class  B: A 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
int z; 
InBlock.gif
public override int X 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gifreturn base.X + 1; } 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
public override int Y 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gifbase.Y = value < 0? 0: value; } 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
public override int Z 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gifreturn z; } 
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ z = value; } 
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}
 
None.gif
None.gif这个例子集中地展示了属性在继承上下文中的某些典型行为。这里,类A由于抽象属性Z的存在而必须声明为abstract。子类B中通过base关键字来引用父类A的属性。类B中可以只通过Y
- set便覆盖了类A中的虚属性。 
None.gif
None.gif静态属性和静态方法一样只能存取类的静态域变量。我们也可以像做外部方法那样,声明外部属性。 

转载于:https://www.cnblogs.com/nbwzy/archive/2007/08/09/849875.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值