目前正在观看 Bart De Smet 对 IQueryable 的解释,他提到了 Existential Types(我对此一直很好奇)。在阅读了这个问题的答案后,我只是想知道这是否是一种在 C# 中构造它的方法:
public abstract class SomeExistentialType
{
private SomeExistentialType() { }
public abstract int Foo();
public ExistentialType Create()
{
return new ConcreteType1();
}
private class ConcreteType1: SomeExistentialType
{
public override int Foo()
{
//some implementation...
}
}
private class ConcreteType1: SomeExistentialType
{
public override int Foo()
{
//some implementation...
}
}
private class ConcreteType1: SomeExistentialType
{
public override int Foo()
{
//some implementation...
}
}
}
这个想法是,如果所有具体类都被定义为私有嵌套类(或者可能只是内部类),那么您将被迫只使用接口。