简体   繁体   English

C# - 类设计和访问修饰符

[英]C# - Class design & access modifiers

Given the following: 鉴于以下内容:

public abstract class Base
{
  // other stuff

  public static void StaticMethod()
  {
    PrivateMethod();
  }
  // here should be PrivateMethod() declaration somehow
}
public sealed class Derived: Base
{
  // other stuff

  public void InstanceMethod()
  {
    // call somehow PrivateMethod 
    PrivateMethod(); 
  }
}

I need to make use of PrivateMethod() from 2 different contexts (different assemblies). 我需要从2个不同的上下文(不同的程序集)中使用PrivateMethod()。 Once calling Base.StaticMethod() , and the second time by using an instance of the Derived class d.InstanceMethod(); 一旦调用Base.StaticMethod() ,第二次使用Derived类的实例d.InstanceMethod(); .

I am looking for a way how to design PrivateMethod() inside the Base class. 我正在寻找一种如何在Base类中设计PrivateMethod()的方法。 Of course PrivateMethod() should not be visible outside the Base and Derived classes. 当然,PrivateMethod()不应该在Base和Derived类之外可见。

I was thinking something about "protected static PrivateMethod() {}" but I read I should not do that... 我正在考虑“protected static PrivateMethod(){}”,但我读到我不应该这样做......

What do you recommend guys? 你有什么推荐的人?

  protected static void PrivateMethod() {}

Is OK (apart form the name) and does what you require. 没问题(除名称外)并做你需要的。 You won't need base. 你不需要base. when calling it from Derived. 从Derived中调用它时。

I had never heard this before, so I went looking for something that said what you described. 我之前从未听过这个,所以我去找一些说你所描述的东西。 I found this article: New Design Guideline: Avoid Protected Static . 我发现了这篇文章: 新设计指南:避免受保护的静态 However, it only talks about protected static field. 但是,它只涉及受保护的静态字段。

I don't think the article actually makes a good case for what it is trying to say. 我认为这篇文章实际上并没有说明它想说的内容。 Rather than just describing how protected statics can lead to complications, it uses a very simple example of the base class designer not setting the right access flags for something that should not be accessed by everyone. 它不是仅描述受保护的静态可能导致复杂性的原因,而是使用一个非常简单的基类设计器示例,即不为每个人都不应访问的内容设置正确的访问标志。

That being said, there is still a point that protected static can lead to complications. 话虽如此,仍有一点保护静电会导致并发症。 Protected static means that any subclass can call a method at any time. 受保护的静态意味着任何子类都可以随时调用方法。 This can lead to thread safety concerns if the method is written naively. 如果该方法是天真地写的,这可能导致线程安全问题。 It seems like the article was written in a way that it conveys "Don't do it" rather than "If you need to do it, be careful." 看起来文章的写作方式是传达“不​​要做”,而不是“如果你需要这样做,要小心。”

You could just call the public StaticMethod() from your derived class's InstanceMethod() ... since it indirects back to PrivateMethod() anyway. 您可以从派生类的InstanceMethod()调用公共StaticMethod() ...因为它无论如何都会间接回到PrivateMethod() That way you can leave PrivateMethod() private. 这样你可以将PrivateMethod()保密。 The implementation would be something like: 实现将是这样的:

public abstract class Base
{
  // other stuff

  public static void StaticMethod()
  {
    PrivateMethod();
  }

  // here should be PrivateMethod() declaration somehow
  private static void PrivateMethod()
  {
    // do stuff
  }
}
public sealed class Derived: Base
{
  // other stuff

  public void InstanceMethod()
  {
    // call somehow PrivateMethod 
    StaticMethod(); 
  }
}

PS: If there is need during StaticMethod to differentiate between a public caller or a derived class caller (from InstanceMethod) it could be either passed as parameter, or determined via reflection. PS:如果在StaticMethod期间需要区分公共调用者或派生类调用者(来自InstanceMethod),它可以作为参数传递,也可以通过反射确定。

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

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