C#--属性三两事
来源:互联网 发布:淘宝无线直通车怎么开 编辑:程序博客网 时间:2024/04/28 11:14
在介绍属性之前,我想问问各位有经验的大牛,属性的作用这么多,被外部访问的字段是不是都需要将它设为属性?
属性与字段的区别:
属性是逻辑字段;属性是字段的扩展,源于字段;
属性并不占用实际的内存,字段占内存位置及空间。
属性可以被其它类访问,而大部分字段不能直接访问。
属性可以对接收的数据范围作限定,而字段不能。
最直接的说:属性是被“外部使用”,字段是被“内部使用”。
下面来看看,属性这种看起来麻烦的东西,到底能够带来什么样的好处:
1、可以提供get-only或者set-only版本,甚至可以给读、写以不同的访问权限(C# 2.0支持)
2、可以对赋值做校验、或者额外的处理
3、可以做线程同步
3、可以做线程同步
4、可以将属性置于interface中
下面来看最简单的例子:
class Person
{
private string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
}
可以通过使用以下代码操作:
Person x = new Person();
x .Sex = "boy";
Console.WriteLine( "sex = {0}", x. Sex); //sex = boy
显然,get为写操作,set为读操作,当然可以设置为只读,只写属性。
当然,正常情况下,sex 的值只有boy跟girl,所以输入的其他情况均为异常情况,需避免。属性的作用之一就是可以提供这种约束:
class Person
{
private string sex;
public string Sex
{
get { return sex; }
set
{
if (value != "boy" && value != "girl" )
{
Console.WriteLine( "sex 输入异常!!" );
}
else
{ this.sex = value ; }
}
}
}
Person x = new Person ();
x . Sex = "boyORgirl";
Console .WriteLine( "sex = {0}" , x. Sex); //sex 输入异常!!
接口属性:
interface IEmployee{ string Name { get; set; } int Counter { get; }}public class Employee : IEmployee{ public static int numberOfEmployees; private string name; public string Name // read-write instance property { get { return name; } set { name = value; } } private int counter; public int Counter // read-only instance property { get { return counter; } } public Employee() // constructor { counter = ++counter + numberOfEmployees; }}class TestEmployee{ static void Main() { System.Console.Write("Enter number of employees: "); Employee.numberOfEmployees = int.Parse(System.Console.ReadLine()); Employee e1 = new Employee(); System.Console.Write("Enter the name of the new employee: "); e1.Name = System.Console.ReadLine(); System.Console.WriteLine("The employee information:"); System.Console.WriteLine("Employee number: {0}", e1.Counter); System.Console.WriteLine("Employee name: {0}", e1.Name); }}
示例输出:
Enter number of employees: 210
Enter the name of the new employee: Hazem Abolrous
The employee information:
Employee number: 211
Employee name: Hazem Abolrous
下面讨论一下虚属性和抽象属性(个人认为的属性的最大优点):
虚属性:
public class Base
{
public virtual int Count //虚属性
{
get { return 1; }
}
public int Compute()
{
return Count;
}
}
public class Derived : Base
{
public override int Count //重载虚属性
{
get { return 5; }
}
}
{
public virtual int Count //虚属性
{
get { return 1; }
}
public int Compute()
{
return Count;
}
}
public class Derived : Base
{
public override int Count //重载虚属性
{
get { return 5; }
}
}
Test:
Derived b = new Derived();
Console.WriteLine("Compute is {0}\n", b.Compute());
Console.ReadLine();
Console.WriteLine("Compute is {0}\n", b.Compute());
Console.ReadLine();
Output:
Compute is 5
抽象属性的实现基本与上面相似,只要将Count 的限定符vitual改为abstract
看完了属性的作用,你是否觉得它很强大?你觉得它应该替代public字段吗?
或者,在你的工作中,你是如何处理考虑这个问题的?
期待你的回答,谢谢!
--------------------------------------------------------------------2012年9月13日21:47:56
- C#--属性三两事
- Win32 API 三两事
- 算法三两事
- RFID破解三两事
- java开篇三两事
- GitHub三两事
- Win32 API 三两事
- 关于ReadDirectoryChangesW的三两事
- 马云之三两事
- 【Java基础】那三两事
- java基础:Map三两事
- Limbo模拟器的三两事
- Python类库三两事
- 关于集合的三两事
- iOS 内存泄漏三两事
- iOS 内存泄漏三两事
- Postgresql注入研究三两事(续集)
- RFID破解三两事(原作者:YunFree)
- 按照客户端的大小来自动改变部局
- [图]iPhone5为首款搭载ARM Cortex A15芯片的手机
- 统一建模语言UML基础专题之组件图与部署图
- 没有搞懂的问题
- 性能测试常见术语
- C#--属性三两事
- 在centos中 将apache httpd 服务加入系统服务
- 自己有时候用到的网站
- 第二章 (第二节 企业战略环境分析)
- 《Java数据结构和算法》第二版 Robert lafore 编程作业 第十一章
- typedef 关键字
- java环境变量配置
- Apache Address already in use: make_sock: could not bind to port 80 or 443 error and solution
- 自定义UITableViewCell上的delete按钮