简体   繁体   English

为什么Bloch的Builder Pattern不能在C#中工作

[英]Why doesn't Bloch's Builder Pattern work in C#

Consider a verbatim copy of Bloch's Builder pattern (with changes made for C#'s syntax): 考虑一下Bloch的Builder模式的逐字副本(对C#的语法进行了更改):

public class NutritionFacts
{
  public int ServingSize { get; private set; }
  public int Servings { get; private set; }
  public int Calories { get; private set; }
  ...
  public class Builder
  {
    private int ServingSize { get; set; }
    private int Servings { get; set; }
    private int Calories { get; set; }

    public Builder(int servingSize, int servings)
    {
      ServingSize = servingSize;
      Servings = servings;
    }

    public Builder Calories(int calories)
    { Calories = calories; return this; }

    public NutritionFacts Build()
    {
      return new NutritionFacts(this);
    }
  }

  private NuitritionFacts(Builder builder)
  {
    ServingSize = builder.ServingSize;
    Servings = builder.Servings;
    Calories = builder.Calories;
  }
}

If you try to run this, the C# compiler will complain that it doesn't have permission to access the private properties of Builder. 如果您尝试运行此命令,C#编译器将抱怨它无权访问Builder的私有属性。 However, in Java, you can do this. 但是,在Java中,您可以这样做。 What rule is different in C# that prevents you from accessing private properties of nested classes? C#中有哪些规则阻止您访问嵌套类的私有属性?

(I realize that people have given alternatives here and that's great. What I'm interested is why you can't use the Java pattern without modification). (我意识到人们已经在这里提供了替代方案,这很棒。我感兴趣的是为什么你不能不经过修改就使用Java模式)。

In Java private members of inner/nested classes are accessible to the containing class. 在Java中,包含类的内部/嵌套类的私有成员可以访问。 In C# they aren't. 在C#中他们不是。

I don't see why that should be allowed to compile. 我不明白为什么应该允许编译。 You are trying to access the private fields of a class from outside that class. 您正尝试从该类外部访问类的私有字段。 Java, however, contains a special rule for nested classes that allows access from the outer class. 但是,Java包含一个允许从外部类进行访问的嵌套类的特殊规则。

The accessibility levels in C# are as follows: C#中的可访问性级别如下:

  • public: Access is not restricted. public:访问不受限制。
  • protected: Access is limited to the containing class or types derived from the containing class. protected:访问仅限于从包含类派生的包含类或类型。
  • internal: Access is limited to the current assembly. internal:访问仅限于当前程序集。
  • protected internal: Access is limited to the current assembly or types derived from the containing class. protected internal:访问仅限于从包含类派生的当前程序集或类型。
  • private : Access is limited to the containing type. private :访问仅限于包含类型。

There is no special case in C# for nested classes, as a result you cannot access a private member from outside that class or any class deriving from that class. C#中没有针对嵌套类的特殊情况,因此您无法从该类外部或从该类派生的任何类访问私有成员。

You can find more information in the follow MSDN article: Accessibility Levels (C#) 您可以在以下MSDN文章中找到更多信息: 辅助功能级别(C#)

Gilad Bracha 认为 ,允许外部类访问嵌套类的私有部分会破坏“OO语言类型系统的包含规则”。

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

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