以使用一个只读属性或方法? [英] To use a read-only property or a method?

查看:170
本文介绍了以使用一个只读属性或方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要揭露映射?的一类的一个实例的状态。结果是由一个基本检查确定。它的不可以只是露出一个字段的值。 。我不能确定我是否应该使用只读属性或方法

I need to expose the "is mapped?" state of an instance of a class. The outcome is determined by a basic check. It is not simply exposing the value of a field. I am unsure as to whether I should use a read-only property or a method.

只读属性:

public bool IsMapped
{
    get
    {
        return MappedField != null;
    }
}



方法:

public bool IsMapped()
{
    return MappedField != null;
}



我看了MSDN的的属性和方法,但我仍然不确定之间进行选择。

I have read MSDN's Choosing Between Properties and Methods but I am still unsure.

推荐答案

C#的标准说

§8.7.4

A 属性是用于提供访问对象或类的特性的成员。属性的示例包括一个字符串的长度,字体的大小,一个窗口的标题,客户的名称,等等。属性是字段的自然扩展。两个被命名为具有相关类型的成员,并且用于访问字段和属性的语法是相同的。然而,与字段,属性不表示存储位置。相反,属性有当读取或写入它们的值要执行指定的语句访问。

A property is a member that provides access to a characteristic of an object or a class. Examples of properties include the length of a string, the size of a font, the caption of a window, the name of a customer, and so on. Properties are a natural extension of fields. Both are named members with associated types, and the syntax for accessing fields and properties is the same. However, unlike fields, properties do not denote storage locations. Instead, properties have accessors that specify the statements to be executed when their values are read or written.

而作为方法被定义为

§8.7.3

A 方式是该实现可以由对象或类执行的计算或操作构件。方法有形式参数(可能为空)列表,返回值(除非该方法的返回类型为void),且无论是静态还是非静态的。

A method is a member that implements a computation or action that can be performed by an object or class. Methods have a (possibly empty) list of formal parameters, a return value (unless the method’s return-type is void ), and are either static or non-static.

属性方法是用来实现的封装。属性封装数据,方法封装的逻辑。这就是为什么你应该更喜欢一个只读属性,如果你暴露的数据。你的情况是没有逻辑的修改对象的内部状态。你想的提供了访问对象的特征

Properties and methods are used to realize encapsulation. Properties encapsulate data, methods encapsulate logic. And this is why you should prefer a read-only property if you are exposing data. In your case there is no logic that modifies the internal state of your object. You want to provide access to a characteristic of an object.

无论你的对象的实例 IsMapped 与否是你的对象的特征。它包含一个检查,但是这就是为什么你有属性来访问它。属性可以用逻辑来定义,但他们不应该暴露的逻辑。就像在第一次报价提到的例子:想象一下 string.length减属性。取决于实现它可能是,该属性遍历串并计数的字符。它也不会执行某项操作,但从外面,它只是给已经结束了对象的内部状态/特性的声明。

Whether an instance of your object IsMapped or not is a characteristic of your object. It contains an check, but that's why you have properties to access it. Properties can be defined using logic, but they should not expose logic. Just like the example mentioned in the first quote: Imagine the String.Length property. Depending on the implementation it may be, that this property loops through the string and counts the characters. It also does perform an operation, but "from the outside" it just give's an statement over the internal state/characteristics of the object.

这篇关于以使用一个只读属性或方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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