[英]Sealing abstract class or interface in .NET class
In Scala, it is possible to have define a base class or trait(interface) sealed, so that the only classes which are allowed to extend that class must be placed in the same class. 在Scala中,可以定义一个密封的基类或trait(interface),以便仅允许将扩展该类的类放在同一类中。
This is a useful pattern when coding libraries, is there any equivalent in .NET? 在对库进行编码时,这是一种有用的模式。
The only way to simulate that is to have a private constructor in the abstract class and provide implementation as nested classes. 模拟的唯一方法是在抽象类中具有私有构造函数,并以嵌套类的形式提供实现。
Example 例
public abstract class Foo
{
private Foo(int k) {}
public class Bar : Foo
{
public Bar() : base(10) {}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.