C# - 类设计&访问修饰符
鉴于以下情况:
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();
}
}
我需要从两个不同的上下文(不同的程序集)使用 PrivateMethod() 。一次调用 Base.StaticMethod()
,第二次使用派生类 d.InstanceMethod();
的实例。
我正在寻找一种如何在基类中设计 PrivateMethod() 的方法。当然,PrivateMethod() 在基类和派生类之外不应该是可见的。
我正在考虑“受保护的静态 PrivateMethod() {}”,但我读到我不应该这样做......
你们有什么建议?
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). Once calling Base.StaticMethod()
, and the second time by using an instance of the Derived class d.InstanceMethod();
.
I am looking for a way how to design PrivateMethod() inside the Base class. Of course PrivateMethod() should not be visible outside the Base and Derived classes.
I was thinking something about "protected static PrivateMethod() {}" but I read I should not do that...
What do you recommend guys?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可以(除了名称)并且可以满足您的要求。从 Derived 调用它时,您不需要
base.
。Is OK (apart form the name) and does what you require. You won't need
base.
when calling it from Derived.我以前从未听说过这个,所以我去寻找符合你所描述的内容。我找到了这篇文章:新设计指南:避免受保护的静电< /a>.然而,它只讨论受保护的静态场。
我认为这篇文章实际上并没有很好地说明它想要表达的内容。它不只是描述受保护的静态如何导致复杂性,而是使用一个非常简单的示例,即基类设计器没有为不应该被所有人访问的内容设置正确的访问标志。
话虽这么说,但仍有一点是受保护的静电可能会导致并发症。受保护的静态意味着任何子类都可以随时调用方法。如果简单地编写该方法,这可能会导致线程安全问题。这篇文章的写作方式似乎是在传达“不要这样做”,而不是“如果需要这样做,请小心”。
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."
您可以只从派生类的 InstanceMethod() 调用公共
StaticMethod()
...,因为它无论如何都会间接返回到PrivateMethod()
。这样您就可以将PrivateMethod()
保留为私有。实现将类似于:PS:如果在 StaticMethod 期间需要区分公共调用者或派生类调用者(来自 InstanceMethod),则可以将其作为参数传递,或通过反射确定。
You could just call the public
StaticMethod()
from your derived class'sInstanceMethod()
... since it indirects back toPrivateMethod()
anyway. That way you can leavePrivateMethod()
private. The implementation would be something like: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.