1.清单 10-1. 传统的访问类的域的例子: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; } } |
说明 |
2.清单 10-2. 使用属性访问类的域: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; } } |
说明 |
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; } } |
说明 |
4.清单 10-4. 只写属性: 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; } } |
说明 |
相关视频
相关阅读 Windows错误代码大全 Windows错误代码查询激活windows有什么用Mac QQ和Windows QQ聊天记录怎么合并 Mac QQ和Windows QQ聊天记录Windows 10自动更新怎么关闭 如何关闭Windows 10自动更新windows 10 rs4快速预览版17017下载错误问题Win10秋季创意者更新16291更新了什么 win10 16291更新内容windows10秋季创意者更新时间 windows10秋季创意者更新内容kb3150513补丁更新了什么 Windows 10补丁kb3150513是什么
热门文章 去除winrar注册框方法
最新文章
比特币病毒怎么破解 比去除winrar注册框方法
华为无线路由器HG522-C破解教程(附超级密码JEB格式文件京东电子书下载和阅读限制破解教UltraISO注册码全集(最新)通过Access破解MSSQL获得数据
人气排行 华为无线路由器HG522-C破解教程(附超级密码JEB格式文件京东电子书下载和阅读限制破解教UltraISO注册码全集(最新)qq相册密码破解方法去除winrar注册框方法(适应任何版本)怎么用手机破解收费游戏华为无线猫HG522破解如何给软件脱壳基础教程
查看所有0条评论>>