【发布时间】:2011-01-13 16:07:27
【问题描述】:
抽象类和只有受保护构造函数的类之间有什么区别?它们似乎与我非常相似,因为你不能实例化任何一个。
编辑:
如何在派生类中创建一个实例,其中基类具有受保护的构造函数?例如:
public class ProtectedConstructor
{
protected ProtectedConstructor()
{
}
public static ProtectedConstructor GetInstance()
{
return new ProtectedConstructor(); // this is fine
}
}
public class DerivedClass : ProtectedConstructor
{
public void createInstance()
{
ProtectedConstructor p = new ProtectedConstructor(); // doesn't compile
}
public static ProtectedConstructor getInstance()
{
return new ProtectedConstructor(); // doesn't compile
}
}
【问题讨论】:
-
为什么这个维基被标记?这里有明确、明确的答案……
-
我编辑了我的答案以添加对此的建议。
-
@Reed 对不起,我的错。我担心它过于开放。
标签: c# constructor abstract-class protected