1

目前正在观看 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...
        }
    }
}

这个想法是,如果所有具体类都被定义为私有嵌套类(或者可能只是内部类),那么您将被迫只使用接口。

4

2 回答 2

1

听起来很像pImpl 成语

我不喜欢欺骗开发人员或强迫他们以这些方式做一些特定的事情。在 C/C++ 中,我可以理解 pImpl 惯用语的理由,因为 C++ 缺乏你在 C# 和 .net 中获得的很多保护,但是很好。

于 2010-05-19T04:25:27.303 回答
0

不,这不是存在类型。在关于存在类型的另一个问题的答案中,请查看 VirtualMachine 示例。

于 2011-10-11T05:13:28.327 回答