简体   繁体   English

私有类数据设计模式很傻?

[英]Private class data design pattern is silly?

I'm reading this page 我正在读这个页面

I am confused as to what is wrong with the first example. 我对第一个例子的问题感到困惑。 Why cannot he just add readonly to the declaration of the private fields? 为什么他不能只将readonly添加到私有字段的声明中?

The keyword readonly (or its equivalent) is provided in some languages (for example, C# and VB.NET ) but not others. 关键字readonly (或其等价物)以某些语言(例如,C#和VB.NET )提供,但不提供其他语言。

C++ is an example of an object-oriented language that does not have a keyword stipulating "This can be set at any part of a constructor function, but nowhere else". C ++是面向对象语言的一个例子,它没有关键字规定“这可以在构造函数的任何部分设置,但不能在其他任何地方设置”。

Object oriented design patterns are meant to be language neutral: applying to all object-oriented languages. 面向对象的设计模式意味着语言中立:适用于所有面向对象的语言。 It follows from this that some languages might have extra features which make a particular pattern obsolete. 由此可见,某些语言可能具有使特定模式过时的额外功能。

In this instance, the presentation of the pattern appears extra silly because the pattern is introduced, then the example is provided in C# which doesn't need it. 在这种情况下,模式的呈现看起来更加愚蠢,因为引入了模式,然后在C#中提供不需要它的示例。

He very well could use readonly . 他很好地可以使用readonly It sounds like he doesn't understand what readonly does. 听起来他不明白readonly作用。 From MSDN : 来自MSDN

The readonly keyword is a modifier that you can use on fields. readonly关键字是一个可以在字段上使用的修饰符。 When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class. 当字段声明包含只读修饰符时,声明引入的字段的赋值只能作为声明的一部分或在同一个类的构造函数中出现。

You can set readonly fields in a constructor. 可以在构造函数中设置readonly字段。 In fact, you would have to -- how else are those fields going to have any meaningful value? 事实上,你必须 - 这些领域还有什么其他有意义的价值?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM