a********y 发帖数: 20 | | p***n 发帖数: 635 | 2 可以用来实现比较严密的封装,控制外界与类的FIELD的接触.
实际上,不管是在C++还是在JAVA(e.g.,Bean)里头,用getter/setter 函数
(accessor,mutator)来控制外界读写的方式一经很常用了,C#只不过是在语
言里直接把这个常用的模式正式吸纳而已.
譬如,如果你想让一个属性只读, 就只定义一个GET好了. 很方便. 而且如果
你想改变属性的名称, 只用在一个地方改就行了.(呵呵, 如果在JAVA里摊上
个getter/setter 离好几百行的时候, 够费眼的.)
【在 a********y 的大作中提到】 : 什么时候会用到呢?有什么好处?
| C****n 发帖数: 2324 | 3 OK.
The real world best pratice:
1. Never declare public field. All the fields should be private.
2. Declare a property for every field.
3. In property, initialize field value if necessary.
That's how/when you use property.
Some often used pattern:
private string _connectionString = null;
public string ConnectionString
{
get
{
if (_connectionString == null || _connectionString == "")
_connectionString =
ConfigurationSettings.AppSetting["ConnectionString"];
return _conn
【在 p***n 的大作中提到】 : 可以用来实现比较严密的封装,控制外界与类的FIELD的接触. : 实际上,不管是在C++还是在JAVA(e.g.,Bean)里头,用getter/setter 函数 : (accessor,mutator)来控制外界读写的方式一经很常用了,C#只不过是在语 : 言里直接把这个常用的模式正式吸纳而已. : 譬如,如果你想让一个属性只读, 就只定义一个GET好了. 很方便. 而且如果 : 你想改变属性的名称, 只用在一个地方改就行了.(呵呵, 如果在JAVA里摊上 : 个getter/setter 离好几百行的时候, 够费眼的.)
| L*******r 发帖数: 1011 | 4 还有一点,如果你用VS.Net可以很方便的显示和管理他们。
【在 p***n 的大作中提到】 : 可以用来实现比较严密的封装,控制外界与类的FIELD的接触. : 实际上,不管是在C++还是在JAVA(e.g.,Bean)里头,用getter/setter 函数 : (accessor,mutator)来控制外界读写的方式一经很常用了,C#只不过是在语 : 言里直接把这个常用的模式正式吸纳而已. : 譬如,如果你想让一个属性只读, 就只定义一个GET好了. 很方便. 而且如果 : 你想改变属性的名称, 只用在一个地方改就行了.(呵呵, 如果在JAVA里摊上 : 个getter/setter 离好几百行的时候, 够费眼的.)
| a********y 发帖数: 20 | 5 Thx.
【在 C****n 的大作中提到】 : OK. : The real world best pratice: : 1. Never declare public field. All the fields should be private. : 2. Declare a property for every field. : 3. In property, initialize field value if necessary. : That's how/when you use property. : Some often used pattern: : private string _connectionString = null; : public string ConnectionString : {
| e***g 发帖数: 158 | 6 it's about as verbose as getter/setter. the nice thing is the concept
is in the language, not by naming convention
【在 C****n 的大作中提到】 : OK. : The real world best pratice: : 1. Never declare public field. All the fields should be private. : 2. Declare a property for every field. : 3. In property, initialize field value if necessary. : That's how/when you use property. : Some often used pattern: : private string _connectionString = null; : public string ConnectionString : {
| w**w 发帖数: 5391 | 7 1 is a common sense, 3 is fine. but 2 ??
【在 C****n 的大作中提到】 : OK. : The real world best pratice: : 1. Never declare public field. All the fields should be private. : 2. Declare a property for every field. : 3. In property, initialize field value if necessary. : That's how/when you use property. : Some often used pattern: : private string _connectionString = null; : public string ConnectionString : {
| C****n 发帖数: 2324 | 8 OK, I didn't say it clearly. :-)
: 2. Declare a property for every field that needs to be accessed from
outside the class. :-)
【在 w**w 的大作中提到】 : 1 is a common sense, 3 is fine. but 2 ??
| c*o 发帖数: 70 | 9 Declare a property as PUBLIC for every ...
because private and protected can be set and got directively.
【在 C****n 的大作中提到】 : OK, I didn't say it clearly. :-) : : 2. Declare a property for every field that needs to be accessed from : outside the class. :-)
|
|