初探c#(十三)属性(Properties)

发表于:2007-06-21来源:作者:点击数: 标签:

   1.14 属性(Properties)

  关于属性就不用多说了。可能有点特别的是如何得到一个属性和设置一个属性。请诸位看下例:*/

public class Button: Control
{
private string caption;
public string Caption {
get {

return caption;
}
set {
caption = value;
Repaint();
}
}
}

/*
有了上面的定义,我们就可以对Button进行读取和设置它的Caption属性:*/

Button b = new Button();
b.Caption = "ABC"; // 设置
string s = b.Caption; // 读取
b.Caption += "DEF”; // 读取 & 设置

原文转自:http://www.ltesting.net