我已经实现了一个单例课程,并不断得到我正在写的一个方法是'在密封课程中声明的新保护成员'的警告。它不影响构建,但我不想忽略警告,以防万一以后出现问题?我知道一个密封的类是一个不能被继承的类 - 所以它的方法不能被覆盖,但我仍然不明白为什么下面的代码会给我警告(是否由于使用了单例设计?):'密封课程中的受保护成员'警告(单身课程)
namespace WPFSurfaceApp
{
public sealed class PresentationManager
{
PresentationManager()
{
}
protected void MethodName()
{
}
public static PresentationManager Instance
{
get
{
return Nested.instance;
}
}
class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested()
{
}
internal static readonly PresentationManager instance = new PresentationManager();
}
}
编辑:该警告是关于MethodName()方法。 编辑:修改公共无效方法名()来保护无效方法名()
如果您将'嵌套'类设为'private'会发生什么? – 2012-01-11 15:35:27
我没有看到任何保护...... – 2012-01-11 15:36:23
什么也没发生,警告仍然存在。我认为说实话,这与singleton设计不会有什么关系 - 它更多的是与密封类和访问修饰符有关,但我想我会提及它,并且包括嵌套类代码以防万一。 – 2012-01-11 15:36:40