属性和字段有什么区别? [英] What is the difference between property and field?

查看:93
本文介绍了属性和字段有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请你澄清一下abow问题。

Please could you clarify abow question.

推荐答案

一个字段是一个变量,一个属性是两个方法的语法糖 - 一个getter和一个setter - 当你访问属性时调用而不是直接访问变量的内容。



这样做的好处是双重的:首先它允许包含类隐藏来自外部世界的细节的内部实施,当它实际上是一个复杂的计算,或基于控制等时,将该属性呈现为简单变量。

其次,它允许属于只读或只写,就外部世界而言,这是不可能的领域。



基本上,如果外面的世界需要了解一个值,它应该是一个属性:字段永远不应该公开为 public 对象。



有关更全面的描述,请参阅MSDN [ ^ ]和 Google [ ^ ]
A field is a variable, a property is a syntactic sugar for two methods - a getter and a setter - which get called when you access the property instead of accessing the content of the variable directly.

The advantage of this is two-fold: firstly it allows the containing class to hide the internal implementation of details from the outside world, presenting the property as a "simple variable" when it can in fact be a complex calculation, or based on a control, etc.
Secondly, it allows the property to be "read only", or "write only" as far as the outside world is concerned which is not possible with fields.

Basically, if the outside world needs to know about a value, it should be a property: fields should never be exposed as public objects.

For a fuller description, refer to MSDN[^] and Google[^]


请参阅以下链接:

See these links:


  • https://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx - Property.
  • https://msdn.microsoft.com/en-US/library/ms173118(v=vs.80).aspx - Field.
  • http://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property-in-c


请通过以下链接

属性和字段之间的差异


这篇关于属性和字段有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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