初探c#(十三)属性(Properties)
添加时间: 2007-7-13 4:04:53 作者: C#教程 阅读次数:117 来源: http://www.d9soft.com
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”; // 读取 & 设置
上下文章:
上一篇文章: C#编程入门三部曲:第三步 增加响应用户事件代码 下一篇文章: C#积木模块ABC(1)

