财产与公共变量 [英] Property vs public variables

查看:110
本文介绍了财产与公共变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是C ++/VC ++开发人员,但在某种程度上了解.Net.

在.Net 3.5中,允许使用以下语法.

Hi,

I am C++/VC++ developer but know .Net up to some extent.

In .Net 3.5 following syntax is permissible.

public int PropertyName {get;set}



从概念上讲,此特性与普通变量没有什么不同.


关于



Not how this property conceptually differed from normal varialble.


Regards

推荐答案

这个问题已经被回答了很多次,Google对此有很多很好的解释.很少有链接是:

http://www.codinghorror.com/blog/2006/08/properties- vs-public-variables.html [ ^ ]

http://channel9.msdn.com/forums/Coffeehouse/253880-GetSet-vs -Public-Variables/ [ ^ ]
This has been answered so many times and Google has tons of very good explanations about it. Few links are:

http://www.codinghorror.com/blog/2006/08/properties-vs-public-variables.html[^]

http://channel9.msdn.com/forums/Coffeehouse/253880-GetSet-vs-Public-Variables/[^]


与公共变量相比,属性有很多优点,以至于公共变量被认为是非常不好的做法.

问题在于,如果您声明一个公共变量,则使外部类可以随时以不受管制的方式访问您的类的内部.反过来,这意味着当您要修改或维护一个类时,您必须在每个阶段考虑对公共变量的所有可能的外部影响,否则就有可能因所做的更改而破坏外部代码.

属性具有以下优点:
1)您可以为Getter和Setter分别设置访问级别-外部类只能读取该值,而派生类可以对其进行修改.
2)您可以在设置器中包含代码,以在设置数据之前验证数据是否有效-可以更改它,否则抛出错误-这使外部类可以在出现问题时发现问题,而不是半小时后,当您尝试在代码中使用错误值时.
3)框架对属性的处理方式大不相同.这种差异意味着,当您在设计器中添加(例如)用户控件时,实例的属性"窗格中只会显示属性,而公共变量则不会.
4)属性可让您将实现隐藏在外界之外:它所看到的只是一个整数,它告诉您您持有多少个项目-它不必知道您随时都不会保留该值,但当他要价时,您可以计算出该数字.这使您有更大的自由来重新设计代码的内部结构,而又不影响外部应用程序.
5)这迫使您考虑班级需要公开的内容-因为建立财产稍微麻烦一些,您必须考虑他是否需要此信息?" -并且如果他不这样做,那么它应该仅是内部的.
Properties have a lot of advantages over public variables, so much so that public variables are considered very bad practice.

The problem is that if you declare a public variable, you are letting an external class have access to your class internals at any time, in a unregulated way. This in turn means that when you come to modify or maintain a class, you have to consider all possible external effects on the public variables at every stage, or risk breaking external code with your changes.

Properties have these advantages:
1) You can have separate access levels for Getters and Setters - so external classes can only read the value, while derived classes can modify it.
2) You can include code in the setter which verifies that the data is valid before setting it - and either change it so it is, or throw an error - this allows the external class to spot a problem at the point when it occurs, rather than half an hour later when you try to use the bad value in your code.
3) Properties are handled very differently by the framework. This difference means that when you add (for example) a user control in the designer, only properties appear in the Property Pane for the instance - public variables don''t.
4) Properties allow you to hide the implementation from the outside world: all it sees is an integer that tells it how many items you are holding - it doesn''t have to know that you don''t keep that value at any time, but that when he asks for it, you calculate the number. This lets you have a lot greater freedom to redesign the internals of your code without affecting outside applications.
5) It forces you to think about what your class needs to expose - because it is slightly more cumbersome to establish a property, you have to think "does he need this information?" - and if he doesn''t, then it should be internal only.


public int PropertyName {get;set}


auto implemented property,将由compiler 展开,然后将创建the back end store,如


is an auto implemented property, which will be expanded by the compiler and the back end store will be created like

//Some name will be given by the compiler for the back end store
//here I have used propertyName only for illustration
private int propertyName
public int PropertyName {
    get { return propertyName; }
    set { propertyName = value;}
}


因此,对于执行代码,auto implemented propertynormal property,并且仅需要对程序员when no other actions like validations 的快捷方式才能执行该属性的within the getter and setter.

现在,变量和属性之间的区别在于变量的值为directly set and retrieved,而对于类似Property it is set in setter and retrieved in getter which permits to perform extra operations 的验证.

.NET Framework设计指南does not recommend the usage of public variable.

此外,在.NET data binding cannot be done with variable中,但可以在with property中完成数据绑定.


So, an auto implemented property is a normal property for the executing code and its only a shortcut to the programmer when no other actions like validations are required to be performed within the getter and setter of the property.

Now, the difference between a variable and a property is that the value of the variable is directly set and retrieved, whereas in case of Property it is set in setter and retrieved in getter which permits to perform extra operations like validations.

The .NET Framework design guideline does not recommend the usage of public variable.

Further, in .NET data binding cannot be done with variable but the data binding can be done with property.


这篇关于财产与公共变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆